11// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
22// SPDX-License-Identifier: Apache-2.0
33
4+ //! # backoff
5+ //! This module is used to provide a helper that keep tracks of exponential backoff information in a
6+ //! non-blocking way. This is important since Actors process one message at a time, and an sleep in
7+ //! the middle would affect performance.
8+ //!
9+ //! This way we just mark something as "in backoff" and just check that information before sending
10+ //! the request.
11+ //!
12+ //! This module is also used by [crate::tracker].
13+
414use std:: time:: { Duration , Instant } ;
515
16+ /// Backoff information based on [Instant]
617#[ derive( Debug , Clone ) ]
718pub struct BackoffInfo {
819 failed_count : u32 ,
920 failed_backoff_time : Instant ,
1021}
1122
1223impl BackoffInfo {
24+ /// Callback represinging that a request succeed
25+ ///
26+ /// This resets the failed_count
1327 pub fn ok ( & mut self ) {
1428 self . failed_count = 0 ;
1529 }
1630
31+ /// Callback represinging that a request failed
32+ ///
33+ /// This sets the backoff time to max(100ms * 2 ^ retries, 60s)
1734 pub fn fail ( & mut self ) {
1835 // backoff = max(100ms * 2 ^ retries, 60s)
1936 self . failed_backoff_time = Instant :: now ( )
@@ -22,6 +39,7 @@ impl BackoffInfo {
2239 self . failed_count += 1 ;
2340 }
2441
42+ /// Returns if is in backoff
2543 pub fn in_backoff ( & self ) -> bool {
2644 let now = Instant :: now ( ) ;
2745 now < self . failed_backoff_time
0 commit comments