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

Support tokio::time::Instant in SystemClock #53

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions .github/workflows/check_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
feature:
- async-std
- futures
- instant
- tokio
- wasm-bindgen
steps:
Expand All @@ -27,10 +28,10 @@ jobs:
override: true

- name: Run cargo check
run: cargo check --features=${{ matrix.feature }}
run: cargo check --no-default-features --features=${{ matrix.feature }}

- name: Run cargo test
run: cargo test --features=${{ matrix.feature }}
run: cargo test --no-default-features --features=${{ matrix.feature }}

lints:
name: Lints
Expand All @@ -51,4 +52,4 @@ jobs:
run: cargo fmt --all -- --check

- name: Run cargo clippy
run: cargo clippy -- -D warnings
run: cargo clippy -- -D warnings
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ travis-ci = { repository = "ihrwein/backoff" }
[dependencies]
async_std_1 = { package = "async-std", version = "1.9", optional = true }
futures-core = { version = "0.3.8", default-features = false, optional = true }
instant = "0.1"
instant = { version = "0.1", optional = true }
pin-project-lite = { version = "0.2.7", optional = true }
rand = "0.8"
getrandom = "0.2"
Expand All @@ -32,7 +32,7 @@ tokio_1 = { package = "tokio", version = "1.0", features = ["macros", "time", "r
futures-executor = "0.3"

[features]
default = []
default = ["instant"]
wasm-bindgen = ["instant/wasm-bindgen", "getrandom/js"]
futures = ["futures-core", "pin-project-lite"]
tokio = ["futures", "tokio_1"]
Expand Down
2 changes: 1 addition & 1 deletion src/clock.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use instant::Instant;
use crate::Instant;

/// Clock returns the current time.
pub trait Clock {
Expand Down
3 changes: 1 addition & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::error;
use std::fmt;

use instant::Duration;
use std::time::Duration;

/// Error is the error value in an operation's
/// result.
Expand Down
2 changes: 1 addition & 1 deletion src/exponential.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use instant::Instant;
use std::marker::PhantomData;
use std::time::Duration;

use crate::backoff::Backoff;
use crate::clock::Clock;
use crate::default;
use crate::Instant;

#[derive(Debug)]
pub struct ExponentialBackoff<C> {
Expand Down
8 changes: 8 additions & 0 deletions src/instant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[cfg(feature = "instant")]
pub use instant::Instant;

#[cfg(all(feature = "tokio_1", not(feature = "instant")))]
pub use tokio_1::time::Instant;

#[cfg(not(any(feature = "tokio_1", feature = "instant")))]
pub use std::time::Instant;
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ pub mod backoff;
mod clock;
pub mod default;
mod error;
mod instant;
pub mod exponential;

#[cfg(feature = "futures")]
Expand All @@ -226,6 +227,7 @@ mod retry;

pub use crate::clock::{Clock, SystemClock};
pub use crate::error::Error;
pub use crate::instant::Instant;
pub use crate::retry::{retry, retry_notify, Notify};

/// Exponential backoff policy with system's clock.
Expand Down
4 changes: 1 addition & 3 deletions tests/exponential.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
extern crate backoff;
extern crate instant;

use backoff::backoff::Backoff;
use backoff::exponential::ExponentialBackoff;
use backoff::{Clock, SystemClock};
use backoff::{Clock, Instant, SystemClock};

use instant::Instant;
use std::cell::RefCell;
use std::time::Duration;

Expand Down