@@ -116,13 +116,18 @@ impl Speed {
116116 }
117117 }
118118
119- /// Getter of the duration time it must wait since the last event to target the given TPS (Transaction Per Seconds) rate
120- /// Consider an overhead to get a lasy duration to not overwhelmed a distant
121- /// TPS should be superior to 0 otherwise it'll panic
122- /// Duration equal 0 if the result is negative
119+ /// Returns the duration to wait since the last event to achieve the target TPS (Transactions Per Second).
120+ /// Includes a small overhead to ensure a conservative duration and avoid overwhelming a remote system.
121+ /// If TPS is not positive or finite, returns `Duration::MAX`.
122+ /// Returns ` Duration::ZERO` if the calculated duration is negative.
123123 ///
124124 /// <math><mfrac><mi>1000 × <msub><mi>N</mi><mn>t</mn></msub></mi><mi>TPS</mi></mfrac> + overhead − <msub><mi>Σ</mi><mn>t</mn></msub> = duration</math>
125125 pub fn get_duration_overhead ( & self , tps : f64 , overhead : Option < Duration > ) -> Duration {
126+ // Guard against invalid TPS values to prevent division by zero or invalid computation
127+ if tps <= 0.0 || !tps. is_finite ( ) {
128+ return Duration :: MAX ;
129+ }
130+
126131 let duration =
127132 Duration :: from_millis ( ( ( 1000 * self . event_speeds . len ( ) ) as f64 / tps) as u64 ) ;
128133 let sum_duration = self . accumulate_event_speeds ( ) ;
@@ -135,9 +140,9 @@ impl Speed {
135140 }
136141 }
137142
138- /// Getter of the duration time it must wait since the last event to target the given TPS (Transaction Per Seconds) rate
139- /// TPS should be superior to 0 otherwise it'll panic
140- /// Duration equal 0 if the result is negative
143+ /// Returns the duration to wait since the last event to achieve the target TPS (Transactions Per Second).
144+ /// If TPS is not positive or finite, returns `Duration::MAX`.
145+ /// Returns ` Duration::ZERO` if the calculated duration is negative.
141146 ///
142147 /// <math><mfrac><mi>1000 × <msub><mi>N</mi><mn>t</mn></msub></mi><mi>TPS</mi></mfrac> − <msub><mi>Σ</mi><mn>t</mn></msub> = duration</math>
143148 pub fn get_duration ( & self , tps : f64 ) -> Duration {
0 commit comments