Resilient
Public Member Functions | List of all members
resilient::retry::Retry< RetryStateFactory > Class Template Reference

Reduce the impact of tasks failing due to transient failures. More...

#include <retry.hpp>

Public Member Functions

 Retry (RetryStateFactory retryStateFactory)
 Construct a new Retry object. More...
 
template<typename Callable , typename... Args>
return_type_t< Callable, Args... > execute (Callable &&callable, Args &&...args)
 Execute the task, retrying if it returns a failure. More...
 

Detailed Description

template<typename RetryStateFactory>
class resilient::retry::Retry< RetryStateFactory >

Reduce the impact of tasks failing due to transient failures.

A task might fail because of a temporary condition. In such a situation the failure can be managed by retrying the operation again.

The Retry calls the tasks which is executed with it until it succeeds or it's determined that it shouldn't be executed anymore. Optionally the Retry can wait between calls. This can be useful if the task is consuming resources and there is no reason to try several executions as close as possible.

For each execution of a task an associated RetryState is used. Any class which abides to the RetryState concept can be used. Retry is instantiate with a factory class which is called to get an instance of RetryState for each task. Any class which abides to the RetryStateFactory concept can be used.

RetryState concept
A RetryState keeps track of the retries and failures of a task in a Retry and decides whether it should be re-executed or not. The following must be valid for an instance named state of type T which implements the RetryState concept.
  • Checking if Retry should retry. The statement resilient::Variant<resilient::retry_after, T::stopretries_type> a = state.shouldRetry(); must be valid. When Retry should execute again holds_alternative<resilient::retry_after>(a) must be true. The current thread will wait for the duration of the value contained in retry_after if it's strictly positive. When Retry should stop executing the task holds_alternative<T::stopretries_type>(a) must be true. Retry will return a Failable with the failure set to the value of type T::stop_retries.
  • Propagating failure informations. Given an instance failure of type Q, the statement state.failedWith(std::move(q)); must be valid. Q is the Failure hold by the Failable the associated task returns.
RetryStateFactory concept
A RetryStateFactory is used to create a RetryState to associate to the execution of a task with the Retry policy. The following must be true for an instance named factory of type T which implements the RetryStateFactory concept.
  • Getting a RetryState instance The statement decltype(auto) state{factory.getRetryState(retriedtask_failure<Q>{})}; must be valid. Q is the Failure hold by the Failable the associated task returns. state must be an instance of a type which implements the RetryState concept.
  • Returning a RetryState instance The statement factory.returnRetryState(std::forward<decltype(state)>(state)); must be valid. The method is always called once for each call made to getRetryState which did not throw an exception, independently from the result of the execution of the task. This call is done after all the retries for the given task have been executed.
Template Parameters
RetryStateFactoryThe factory used to create the RetryState used while

Constructor & Destructor Documentation

template<typename RetryStateFactory >
resilient::retry::Retry< RetryStateFactory >::Retry ( RetryStateFactory  retryStateFactory)
inline

Construct a new Retry object.

Parameters
retryStateFactoryThe factory to be used to generate states for the retries.

Member Function Documentation

template<typename RetryStateFactory >
template<typename Callable , typename... Args>
return_type_t<Callable, Args...> resilient::retry::Retry< RetryStateFactory >::execute ( Callable &&  callable,
Args &&...  args 
)
inline

Execute the task, retrying if it returns a failure.

Execute the task until either it returns a success or the retry state returns a stopretries_type when checking if we should retry.

Parameters
callableThe task to execute
args...The arguments to the task
Returns
A Failable which has as value the value returned by task and as failure the stopretries_type of the retry state.

The documentation for this class was generated from the following file: