pub struct SlotBackoff { /* private fields */ }
Expand description
SlotBackoff is a backoff strategy that randomly chooses a time slot to retry.
This is useful when you have multiple tasks that can’t overlap, and each task takes roughly the same amount of time.
The unit
represents the time it takes to complete one attempt. Future attempts
are divided into time slots, and a random slot is chosen for the retry. The number
of slots increases exponentially with each attempt. Initially, there are 4 slots,
then 8, then 16, and so on.
Example: Suppose you have 10 tasks that can’t overlap, each taking 1 second. The tasks don’t know about each other and can’t coordinate. Each task randomly picks a time slot to retry. Here’s how it might look:
First round (4 slots):
task id | 1, 2, 3 | 4, 5, 6 | 7, 8, 9 | 10 |
status | x, x, ✓ | x, x, ✓ | x, x, ✓ | ✓ |
timeline | 0s | 1s | 2s | 3s |
Each slot can have one success. Here, tasks 3, 6, 9, and 10 succeed. In the next round, the number of slots doubles (8):
Second round (8 slots):
task id | 1 | 2 | | 4, 5 | 7 | 8 | | |
status | ✓ | ✓ | | x, ✓ | ✓ | ✓ | | |
timeline | 0s | 1s | 2s | 3s | 4s | 5s | 6s | 7s |
Most tasks are done now, except for task 4. It will succeed in the next round.
Implementations§
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SlotBackoff
impl RefUnwindSafe for SlotBackoff
impl Send for SlotBackoff
impl Sync for SlotBackoff
impl Unpin for SlotBackoff
impl UnwindSafe for SlotBackoff
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more