|
Lightweight 0.20260921.0
|
A reusable retry/backoff policy for transient database failures. More...
#include <SqlRetryPolicy.hpp>
Public Types | |
| using | RetryObserver = std::function< void(SqlRetryAttempt const &)> |
Public Member Functions | |
| SqlRetryPolicy ()=default | |
| LIGHTWEIGHT_API | SqlRetryPolicy (SqlRetrySettings settings, SqlRetryClassifier const *classifier=nullptr, SqlRetrySleeper *sleeper=nullptr, RetryObserver observer={}) |
| SqlRetrySettings const & | Settings () const noexcept |
| SqlRetryClassifier const & | Classifier () const noexcept |
| void | SetRetryObserver (RetryObserver observer) |
| LIGHTWEIGHT_API std::chrono::milliseconds | DelayFor (unsigned retryIndex) const noexcept |
| LIGHTWEIGHT_API SqlRetryDecision | Decide (SqlErrorInfo const &error, SqlRetryState const &state) const noexcept |
| template<typename Callable > | |
| auto | Execute (Callable callable) const -> std::invoke_result_t< Callable & > |
| template<typename Callable > | |
| auto | TryExecute (Callable callable) const -> std::expected< std::invoke_result_t< Callable & >, SqlErrorInfo > |
Static Public Member Functions | |
| static LIGHTWEIGHT_API SqlRetryPolicy | For (SqlServerType serverType, SqlRetrySettings settings={}) |
| static LIGHTWEIGHT_API SqlRetryPolicy | For (SqlConnection const &connection, SqlRetrySettings settings={}) |
A reusable retry/backoff policy for transient database failures.
Combines a SqlRetryClassifier (which errors are worth another attempt — a per-DBMS question) with SqlRetrySettings (how many attempts, how long to wait between them). Every collaborator is injectable, and none of them is constructed internally: the classifier and the sleeper are borrowed references with sensible process-wide defaults.
The type separates the decision from the driving:
Definition at line 188 of file SqlRetryPolicy.hpp.
| using Lightweight::SqlRetryPolicy::RetryObserver = std::function<void(SqlRetryAttempt const&)> |
Notified just before each retry. Used, for instance, to route retry notices into a progress reporter or a log.
Definition at line 193 of file SqlRetryPolicy.hpp.
|
default |
Constructs a policy with the default settings, the dialect-agnostic classifier and the real sleeper.
|
explicit |
Constructs a policy.
| settings | The backoff configuration. |
| classifier | Which errors are retryable; nullptr selects GenericRetryOps(). The referenced classifier must outlive the policy — the dialect singletons always do. |
| sleeper | How to wait between attempts; nullptr selects ThreadSleeper(). The referenced sleeper must outlive the policy. |
| observer | Called before each retry; may be empty. |
|
static |
Builds a policy whose classifier matches the given server type.
The mapping runs through SqlQueryFormatter::Get(), so the per-DBMS knowledge stays at the formatter dispatch point. A server type without a formatter of its own falls back to GenericRetryOps().
| serverType | The DBMS whose error dialect should be used. |
| settings | The backoff configuration. |
|
static |
Builds a policy whose classifier matches the connection's DBMS.
| connection | The connection whose server type selects the classifier. |
| settings | The backoff configuration. |
|
inlinenoexcept |
Definition at line 232 of file SqlRetryPolicy.hpp.
|
inlinenoexcept |
Definition at line 238 of file SqlRetryPolicy.hpp.
|
inline |
Installs an observer notified before each retry.
| observer | The observer; pass an empty function to remove a previously set one. |
Definition at line 246 of file SqlRetryPolicy.hpp.
|
noexcept |
Computes the backoff delay preceding a given retry.
| retryIndex | Zero-based retry number: 0 is the delay before the first retry. |
initialDelay multiplied by backoffMultiplier retryIndex times, clamped to maxDelay.
|
noexcept |
Decides what to do after a failed attempt.
Pure: no I/O, no clock, no hidden state.
| error | The error reported by the failed attempt. |
| state | How far the retry loop has already got. |
| auto Lightweight::SqlRetryPolicy::Execute | ( | Callable | callable | ) | const -> std::invoke_result_t<Callable&> |
Runs callable, retrying while the policy says the failure is worth another attempt.
Only SqlException is treated as a retry candidate; any other exception propagates immediately. When the policy gives up, the last SqlException is rethrown unchanged, so the caller sees the original diagnostics rather than a wrapper.
| Callable | A nullary callable, taken by value because it is invoked repeatedly. |
| callable | The operation to run. |
callable returns. Definition at line 298 of file SqlRetryPolicy.hpp.
References GiveUp, and Lightweight::SqlRetryAttempt::retryNumber.
| auto Lightweight::SqlRetryPolicy::TryExecute | ( | Callable | callable | ) | const -> std::expected<std::invoke_result_t<Callable&>, SqlErrorInfo> |
Like Execute, but reports a final failure as std::unexpected rather than throwing.
| Callable | A nullary callable, taken by value because it is invoked repeatedly. |
| callable | The operation to run. |
Definition at line 328 of file SqlRetryPolicy.hpp.
References Execute.