Skip to content

Commit

Permalink
Support tokio::time::Instant in SystemClock
Browse files Browse the repository at this point in the history
`tokio::time::Instant` is a special `Instant` type that supports mocking
for tests (via [`tokio::time::pause`][pause]). Furthermore, it [avoids
panics][panic] in `Instant` arithmetic.

When using `tokio::time::Instant`, there's no need for a dependency on
the `instant` crate.

This change:

* makes the `instant` crate an optional dependency, enabled by default;
* uses `tokio::time::Instant` when the `tokio_1` feature is enabled and
  `instant` is not; and
* uses `std::time::Instant` when neither of these features are enabled.

The type is exposed publicly via `backoff::Instant` as a convenience.

This change is backwards-compatible and does not change the default
behavior.

[pause]: https://docs.rs/tokio/latest/tokio/time/fn.pause.html
[panic]: tokio-rs/tokio#4461

Signed-off-by: Oliver Gould <ver@buoyant.io>
  • Loading branch information
olix0r committed Feb 23, 2022
1 parent 587e2da commit 541f7eb
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 12 deletions.
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

0 comments on commit 541f7eb

Please sign in to comment.