Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement BackoffBuilder for Backoff itself #142

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions backon/src/backoff/api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
use core::time::Duration;

/// Backoff is an [`Iterator`] that returns [`Duration`].
///
/// - `Some(Duration)` indicates the caller should `sleep(Duration)` and retry the request.
/// - `None` indicates the limits have been reached, and the caller should return the current error instead.
pub trait Backoff: Iterator<Item = Duration> + Send + Sync + Unpin {}
impl<T> Backoff for T where T: Iterator<Item = Duration> + Send + Sync + Unpin {}

/// BackoffBuilder is utilized to construct a new backoff.
pub trait BackoffBuilder: Send + Sync + Unpin {
/// The associated backoff returned by this builder.
Expand All @@ -9,9 +16,24 @@ pub trait BackoffBuilder: Send + Sync + Unpin {
fn build(self) -> Self::Backoff;
}

/// Backoff is an [`Iterator`] that returns [`Duration`].
///
/// - `Some(Duration)` indicates the caller should `sleep(Duration)` and retry the request.
/// - `None` indicates the limits have been reached, and the caller should return the current error instead.
pub trait Backoff: Iterator<Item = Duration> + Send + Sync + Unpin {}
impl<T> Backoff for T where T: Iterator<Item = Duration> + Send + Sync + Unpin {}
impl<B: Backoff> BackoffBuilder for B {
type Backoff = B;

fn build(self) -> Self::Backoff {
self
}
}

#[cfg(test)]
mod tests {
use super::*;

fn test_fn_builder(b: impl BackoffBuilder) {
let _ = b.build();
}

#[test]
fn test_backoff_builder() {
test_fn_builder([Duration::from_secs(1)].into_iter())
}
}