8 #ifndef SCARAB_EXPONENTIAL_BACKOFF_HH_ 9 #define SCARAB_EXPONENTIAL_BACKOFF_HH_ 21 LOGGER( expbacklog_hh,
"exponential_backoff.hh" )
33 template<
typename ... x_args >
37 exponential_backoff( std::function<
bool (x_args...) > an_action,
unsigned a_max_attempts = 10,
unsigned a_base_delay_ms = 100 );
44 unsigned go( x_args... args );
53 template<
typename ... x_args >
55 f_action( an_action ),
56 f_max_attempts( a_max_attempts ),
57 f_base_delay_ms( a_base_delay_ms )
59 if( f_max_attempts == 0 )
throw error() <<
"Max attempts must be > 0";
62 template<
typename ... x_args >
66 template<
typename ... x_args >
70 unsigned t_attempts = 1;
71 bool t_success = f_action( args... );
72 LDEBUG( expbacklog_hh,
"function called first time: " << t_success );
73 while( ! t_success && t_attempts < f_max_attempts )
75 LDEBUG( expbacklog_hh,
"attempt: " << t_attempts + 1 <<
" after delay: " << (1<<t_attempts) * f_base_delay_ms );
76 std::this_thread::sleep_for( std::chrono::milliseconds( (1<<t_attempts) * f_base_delay_ms ) );
77 t_success = f_action( args... );
78 LDEBUG( expbacklog_hh,
"function called: " << t_success );
82 return t_success ? t_attempts : 0;
Class that performs an action using exponential backoff.
unsigned go(x_args... args)
returns the number of attempts made at calling the function (including the last, successful or not) ...
Contains the logger class and macros, based on Kasper's KLogger class.
virtual ~exponential_backoff()