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 following ExampleStrategy:

class ExampleStrategy { public: /// Create request future.

/// Called at least once per hedged request. First time at the beginning /// of a hedged request and then every HedgingSettings::hedging_delay /// milliseconds if none of the previous requests are ready or /// ProcessReply returned non-nullopt result. If ProcessReply returned /// some interval of time, then additional request will be scheduled at /// this interval of time. ///

Parameters
attempt- increasing number for each try std::optional<RequestType> Create(std::size_t attempt);

/// ProcessReply is called when some request has finished. Method should /// evaluate request status and decide whether new attempt is required. /// If new attempt is required, then method must return delay for next /// attempt. If no other attempt is required, then method must return /// std::nullopt. It is expected that successful result will be stored /// somewhere internally and will be available with ExtractReply() /// method. std::optional<std::chrono::milliseconds> ProcessReply(RequestType&&);

std::optional<ReplyType> ExtractReply();

/// Method is called when system does not need request any more. /// For example, if one of the requests has been finished successfully, /// the system will finish all related subrequests. /// It is recommended to make this call as fast as possible in order /// not to block overall execution. For example, if you internally have /// some future, it is recommended to call TryCancel() here and wait /// for a cancellation in destructor, rather than call TryCancel() and /// immediately wait. void Finish(RequestType&&); };

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 <functional>
#include <optional>
#include <queue>
#include <tuple>
#include <type_traits>
#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/datetime.hpp>
+ Include dependency graph for hedged_request.hpp:
+ This graph shows which files directly or indirectly include this file:

Classes

struct  utils::hedging::HedgingSettings
 Class define hedging settings. More...
 
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
 Utilities.
 

Functions

template<typename RequestStrategy >
auto utils::hedging::HedgeRequestsBulk (std::vector< RequestStrategy > inputs, HedgingSettings hedging_settings)
 
template<typename RequestStrategy >
auto utils::hedging::HedgeRequestsBulkAsync (std::vector< RequestStrategy > inputs, HedgingSettings settings)
 
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 450 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 466 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

Parameters
inputsor std::nullopt in case 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 planed actions any more

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

No need to check. we just added one entry

Definition at line 365 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

Parameters
inputsor std::nullopt in case either timeouts or bad replies (RequestStrategy::ProcessReply(RequestType&&) returned std::nullopt and RequestStrategy::ExtractReply() returned std::nulopt)

Definition at line 436 of file hedged_request.hpp.