Resilient
|
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... | |
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
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.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
.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
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.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.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.RetryStateFactory | The factory used to create the RetryState used while |
|
inline |
Construct a new Retry object.
retryStateFactory | The factory to be used to generate states for the retries. |
|
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.
callable | The task to execute |
args... | The arguments to the task |
stopretries_type
of the retry state.