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.
17+ #[ derive( Debug ) ]
18+ pub struct PythLazerExponentialBackoff {
19+ /// The initial retry interval.
20+ 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.
25+ randomization_factor : f64 ,
26+ /// The value to multiply the current interval with for each retry attempt.
27+ multiplier : f64 ,
28+ /// The maximum value of the back off period. Once the retry interval reaches this
29+ /// value it stops increasing.
30+ max_interval : Duration ,
31+ }
32+
33+ impl From < PythLazerExponentialBackoff > for ExponentialBackoff {
34+ fn from ( val : PythLazerExponentialBackoff ) -> Self {
35+ ExponentialBackoffBuilder :: default ( )
36+ . with_initial_interval ( val. initial_interval )
37+ . with_randomization_factor ( val. randomization_factor )
38+ . with_multiplier ( val. multiplier )
39+ . with_max_interval ( val. max_interval )
40+ . with_max_elapsed_time ( None )
41+ . build ( )
42+ }
43+ }
44+
45+ /// Builder for [`PythLazerExponentialBackoff`].
46+ ///
47+ /// Provides a fluent interface for configuring exponential backoff parameters
48+ /// with sensible defaults from the backoff crate.
849#[ derive( Debug ) ]
950pub struct PythLazerExponentialBackoffBuilder {
1051 initial_interval : Duration ,
@@ -25,45 +66,53 @@ impl Default for PythLazerExponentialBackoffBuilder {
2566}
2667
2768impl PythLazerExponentialBackoffBuilder {
69+ /// Creates a new builder with default values.
2870 pub fn new ( ) -> Self {
2971 Default :: default ( )
3072 }
3173
32- /// The initial retry interval.
74+ /// Sets the initial retry interval.
75+ ///
76+ /// This is the starting interval for the first retry attempt.
3377 pub fn with_initial_interval ( & mut self , initial_interval : Duration ) -> & mut Self {
3478 self . initial_interval = initial_interval;
3579 self
3680 }
3781
38- /// 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.
3983 ///
4084 /// A randomization factor of 0.5 results in a random period ranging between 50% below and 50%
41- /// 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.
4287 pub fn with_randomization_factor ( & mut self , randomization_factor : f64 ) -> & mut Self {
4388 self . randomization_factor = randomization_factor;
4489 self
4590 }
4691
47- /// 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.
4895 pub fn with_multiplier ( & mut self , multiplier : f64 ) -> & mut Self {
4996 self . multiplier = multiplier;
5097 self
5198 }
5299
53- /// The maximum value of the back off period. Once the retry interval reaches this
54- /// 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.
55104 pub fn with_max_interval ( & mut self , max_interval : Duration ) -> & mut Self {
56105 self . max_interval = max_interval;
57106 self
58107 }
59108
60- pub fn build ( & self ) -> ExponentialBackoff {
61- ExponentialBackoffBuilder :: default ( )
62- . with_initial_interval ( self . initial_interval )
63- . with_randomization_factor ( self . randomization_factor )
64- . with_multiplier ( self . multiplier )
65- . with_max_interval ( self . max_interval )
66- . with_max_elapsed_time ( None )
67- . build ( )
109+ /// Builds the [`PythLazerExponentialBackoff`] configuration.
110+ pub fn build ( & self ) -> PythLazerExponentialBackoff {
111+ PythLazerExponentialBackoff {
112+ initial_interval : self . initial_interval ,
113+ randomization_factor : self . randomization_factor ,
114+ multiplier : self . multiplier ,
115+ max_interval : self . max_interval ,
116+ }
68117 }
69118}
0 commit comments