userver: userver/utils/hedged_request.hpp File Reference
Loading...
Searching...
No Matches
hedged_request.hpp File Reference

Detailed Description

Classes and functions for performing hedged requests.

To perform hedged request you need to define RequestStrategy - a class similar to the following ExampleStrategy:

struct ExampleResponse {
std::string body;
// The server asks to retry not earlier than after this delay, e.g. via `grpc-retry-pushback-ms`
std::optional<std::chrono::milliseconds> retry_after;
};
class ExampleStrategy {
public:
explicit ExampleStrategy(std::size_t failed_attempts)
: failed_attempts_(failed_attempts)
{}
// Creates the request of an attempt. Called at the beginning of the hedged request, then every
// HedgingSettings::hedging_delay while there is no accepted reply, and after the delay returned by
// ProcessReply. Returning std::nullopt stops the attempts of the request. The awaitable token of the
// returned request must not be empty and must not change until the request is finished.
std::optional<engine::TaskWithResult<ExampleResponse>> Create(std::size_t attempt) {
const bool is_failed = attempt < failed_attempts_;
return utils::Async("example-request", [is_failed, attempt] {
if (is_failed) {
return ExampleResponse{.body = {}, .retry_after = 10ms};
}
return ExampleResponse{.body = "attempt " + std::to_string(attempt), .retry_after = std::nullopt};
});
}
// Called when some attempt has finished. Returns std::nullopt if no other attempt is required, storing
// the successful result to be returned by ExtractReply. Otherwise returns the delay of the next attempt:
// no attempts of the request, hedging ones included, are started earlier than after the delay. If the
// delay ends after HedgingSettings::timeout_all since the start of the hedged request, no more attempts
// are started, while the attempts in flight are still waited for.
std::optional<std::chrono::milliseconds> ProcessReply(engine::TaskWithResult<ExampleResponse>&& request) {
auto response = request.Get();
if (response.retry_after.has_value()) {
return response.retry_after;
}
reply_ = std::move(response.body);
return std::nullopt;
}
std::optional<std::string> ExtractReply() { return std::move(reply_); }
// Called when the request is not needed any more, e.g. another attempt has succeeded. Is also called
// for the requests already passed to ProcessReply. Should be fast: it is recommended to request
// cancellation here and to wait for it in the destructor of the request.
void Finish(engine::TaskWithResult<ExampleResponse>&& request) {
if (request.IsValid()) {
request.RequestCancel();
}
}
private:
std::size_t failed_attempts_;
std::optional<std::string> reply_;
};

Then call any of these functions:

  • HedgeRequest
  • HedgeRequestsBulk
  • HedgeRequestAsync
  • HedgeRequestsBulkAsync

Definition in file hedged_request.hpp.

Go to the source code of this file.

#include <chrono>
#include <cstddef>
#include <optional>
#include <type_traits>
#include <utility>
#include <vector>
#include <userver/compiler/impl/lifetime.hpp>
#include <userver/engine/awaitable.hpp>
#include <userver/engine/task/cancel.hpp>
#include <userver/engine/wait_any.hpp>
#include <userver/utils/assert.hpp>
#include <userver/utils/async.hpp>
#include <userver/utils/hedging_settings.hpp>
#include <userver/utils/impl/hedging_plan.hpp>
This graph shows which files directly or indirectly include this file:

Classes

struct  utils::hedging::RequestTraits< RequestStrategy >
struct  utils::hedging::HedgedRequestBulkFuture< RequestStrategy >
 Future of hedged bulk request. More...
struct  utils::hedging::HedgedRequestFuture< RequestStrategy >
 Future of hedged request. More...

Namespaces

namespace  utils
 General-purpose utilities used across userver libraries.

Functions

template<typename RequestStrategy>
auto utils::hedging::HedgeRequestsBulk (std::vector< RequestStrategy > inputs, HedgingSettings hedging_settings)
 Synchronously perform bulk hedged requests described by RequestStrategy and return result of type std::vector<std::optional<ResultType>>.
template<typename RequestStrategy>
auto utils::hedging::HedgeRequestsBulkAsync (std::vector< RequestStrategy > inputs, HedgingSettings settings)
 Asynchronously perform bulk hedged requests described by RequestStrategy and return future which returns Result of type std::vector<std::optional<ResultType>>.
template<typename RequestStrategy>
std::optional< typename RequestTraits< RequestStrategy >::ReplyType > utils::hedging::HedgeRequest (RequestStrategy input, HedgingSettings settings)
template<typename RequestStrategy>
auto utils::hedging::HedgeRequestAsync (RequestStrategy input, HedgingSettings settings)

Function Documentation

◆ HedgeRequest()

template<typename RequestStrategy>
std::optional< typename RequestTraits< RequestStrategy >::ReplyType > utils::hedging::HedgeRequest ( RequestStrategy input,
HedgingSettings settings )

Synchronously Perform hedged request described by RequestStrategy and return result or throw runtime_error. Exception can be thrown in case of timeout or if request was denied by strategy e.g. ProcessReply always returned std::nullopt or ExtractReply returned std::nullopt

Definition at line 331 of file hedged_request.hpp.

◆ HedgeRequestAsync()

template<typename RequestStrategy>
auto utils::hedging::HedgeRequestAsync ( RequestStrategy input,
HedgingSettings settings )

Create future which perform hedged request described by RequestStrategy and return result or throw runtime_error. Exception can be thrown in case of timeout or if request was denied by strategy e.g. ProcessReply always returned std::nullopt or ExtractReply returned std::nullopt

Definition at line 349 of file hedged_request.hpp.

◆ HedgeRequestsBulk()

template<typename RequestStrategy>
auto utils::hedging::HedgeRequestsBulk ( std::vector< RequestStrategy > inputs,
HedgingSettings hedging_settings )

Synchronously perform bulk hedged requests described by RequestStrategy and return result of type std::vector<std::optional<ResultType>>.

Result contains replies for each element in inputs or std::nullopt in case of either timeouts or bad replies (RequestStrategy::ProcessReply(RequestType&&) returned std::nullopt and RequestStrategy::ExtractReply() returned std::nullopt).

timeout - need to process plan

Timeout but we don't have planned actions any more

Got reply but it's not OK and user wants to retry over some delay

No need to check, the stop entry is planned until the stop

Definition at line 234 of file hedged_request.hpp.

◆ HedgeRequestsBulkAsync()

template<typename RequestStrategy>
auto utils::hedging::HedgeRequestsBulkAsync ( std::vector< RequestStrategy > inputs,
HedgingSettings settings )

Asynchronously perform bulk hedged requests described by RequestStrategy and return future which returns Result of type std::vector<std::optional<ResultType>>.

Result contains replies for each element in inputs or std::nullopt in case of either timeouts or bad replies (RequestStrategy::ProcessReply(RequestType&&) returned std::nullopt and RequestStrategy::ExtractReply() returned std::nullopt).

Definition at line 317 of file hedged_request.hpp.