ORE Studio 0.0.4
Loading...
Searching...
No Matches
Public Member Functions | List of all members
retry_strategy Class Reference

Stateful exponential-backoff retry calculator. More...

#include <retry_strategy.hpp>

Collaboration diagram for retry_strategy:
Collaboration graph

Public Member Functions

 retry_strategy (config cfg)
 
void on_start ()
 Record a successful start.
 
std::chrono::seconds on_failure ()
 Record a failure and compute the next backoff delay.
 
bool exceeded (int max) const
 Returns whether the failure count has reached or exceeded max.
 
int failure_count () const
 Returns the current consecutive failure count.
 
void set_failure_count (int n)
 Restore a previously saved failure count without resetting state.
 
void reset ()
 Reset the counter and start-time record to initial state.
 

Detailed Description

Stateful exponential-backoff retry calculator.

Tracks the number of consecutive failures and the time of the last successful start, and computes the next wait interval as:

delay = min(initial_delay * 2^n, max_delay)

where n is the current failure count. If the elapsed time since the last start exceeds reset_threshold, the failure counter is reset before computing the delay — a long-running process that has an occasional transient crash restarts quickly instead of incurring a large penalty.

This class is a pure chrono utility: it has no knowledge of processes, services, or any external system. All logging and policy decisions belong to the caller.

Member Function Documentation

◆ on_start()

void on_start ( )

Record a successful start.

Must be called immediately after each successful launch so that on_failure() can measure elapsed uptime accurately.

◆ on_failure()

std::chrono::seconds on_failure ( )

Record a failure and compute the next backoff delay.

If the elapsed time since on_start() exceeds config.reset_threshold, the failure counter is reset to 0 before computing the delay. The counter is then incremented and the delay is returned.

Returns
Duration the caller should wait before the next attempt.

◆ exceeded()

bool exceeded ( int  max) const

Returns whether the failure count has reached or exceeded max.

Call before on_failure() to decide if the threshold has been crossed on this failure, before the counter is incremented.

◆ set_failure_count()

void set_failure_count ( int  n)

Restore a previously saved failure count without resetting state.

Used when a new entry must inherit an accumulated count from a prior one (e.g. after a process is relaunched and the entry is replaced).