1+ //! Exponential backoff implementation for Pyth Lazer client.
2+ //!
3+ //! This module provides a wrapper around the [`backoff`] crate's exponential backoff functionality,
4+ //! offering a simplified interface tailored for Pyth Lazer client operations.
5+
16use std:: time:: Duration ;
27
38use backoff:: {
49 default:: { INITIAL_INTERVAL_MILLIS , MAX_INTERVAL_MILLIS , MULTIPLIER , RANDOMIZATION_FACTOR } ,
510 ExponentialBackoff , ExponentialBackoffBuilder ,
611} ;
712
13+ /// A wrapper around the backoff crate's exponential backoff configuration.
14+ ///
15+ /// This struct encapsulates the parameters needed to configure exponential backoff
16+ /// behavior and can be converted into the backoff crate's [`ExponentialBackoff`] type.
817#[ derive( Debug ) ]
918pub struct PythLazerExponentialBackoff {
19+ /// The initial retry interval.
1020 initial_interval : Duration ,
21+ /// The randomization factor to use for creating a range around the retry interval.
22+ ///
23+ /// A randomization factor of 0.5 results in a random period ranging between 50% below and 50%
24+ /// above the retry interval.
1125 randomization_factor : f64 ,
26+ /// The value to multiply the current interval with for each retry attempt.
1227 multiplier : f64 ,
28+ /// The maximum value of the back off period. Once the retry interval reaches this
29+ /// value it stops increasing.
1330 max_interval : Duration ,
1431}
1532
@@ -25,6 +42,10 @@ impl From<PythLazerExponentialBackoff> for ExponentialBackoff {
2542 }
2643}
2744
45+ /// Builder for [`PythLazerExponentialBackoff`].
46+ ///
47+ /// Provides a fluent interface for configuring exponential backoff parameters
48+ /// with sensible defaults from the backoff crate.
2849#[ derive( Debug ) ]
2950pub struct PythLazerExponentialBackoffBuilder {
3051 initial_interval : Duration ,
@@ -45,38 +66,47 @@ impl Default for PythLazerExponentialBackoffBuilder {
4566}
4667
4768impl PythLazerExponentialBackoffBuilder {
69+ /// Creates a new builder with default values.
4870 pub fn new ( ) -> Self {
4971 Default :: default ( )
5072 }
5173
52- /// The initial retry interval.
74+ /// Sets the initial retry interval.
75+ ///
76+ /// This is the starting interval for the first retry attempt.
5377 pub fn with_initial_interval ( & mut self , initial_interval : Duration ) -> & mut Self {
5478 self . initial_interval = initial_interval;
5579 self
5680 }
5781
58- /// The randomization factor to use for creating a range around the retry interval.
82+ /// Sets the randomization factor to use for creating a range around the retry interval.
5983 ///
6084 /// A randomization factor of 0.5 results in a random period ranging between 50% below and 50%
61- /// above the retry interval.
85+ /// above the retry interval. This helps avoid the "thundering herd" problem when multiple
86+ /// clients retry at the same time.
6287 pub fn with_randomization_factor ( & mut self , randomization_factor : f64 ) -> & mut Self {
6388 self . randomization_factor = randomization_factor;
6489 self
6590 }
6691
67- /// The value to multiply the current interval with for each retry attempt.
92+ /// Sets the value to multiply the current interval with for each retry attempt.
93+ ///
94+ /// A multiplier of 2.0 means each retry interval will be double the previous one.
6895 pub fn with_multiplier ( & mut self , multiplier : f64 ) -> & mut Self {
6996 self . multiplier = multiplier;
7097 self
7198 }
7299
73- /// The maximum value of the back off period. Once the retry interval reaches this
74- /// value it stops increasing.
100+ /// Sets the maximum value of the back off period.
101+ ///
102+ /// Once the retry interval reaches this value it stops increasing, providing
103+ /// an upper bound on the wait time between retries.
75104 pub fn with_max_interval ( & mut self , max_interval : Duration ) -> & mut Self {
76105 self . max_interval = max_interval;
77106 self
78107 }
79108
109+ /// Builds the [`PythLazerExponentialBackoff`] configuration.
80110 pub fn build ( & self ) -> PythLazerExponentialBackoff {
81111 PythLazerExponentialBackoff {
82112 initial_interval : self . initial_interval ,
0 commit comments