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: Allow user to provide sleeper #93

Merged
merged 9 commits into from
Aug 22, 2024
Merged
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
20 changes: 0 additions & 20 deletions .github/actions/check/action.yml

This file was deleted.

43 changes: 15 additions & 28 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
name: CI

on: [push, pull_request]
on:
push:
branches:
- main
pull_request:
branches:
- main

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
Expand All @@ -11,25 +17,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/check
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
- macos-latest
- windows-latest
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- name: Build
uses: actions-rs/cargo@v1
with:
command: build
- name: Format
run: cargo fmt --all -- --check
- name: Clippy
run: cargo clippy --all-features --all-targets -- -D warnings

unit:
runs-on: ${{ matrix.os }}
Expand All @@ -41,23 +32,19 @@ jobs:
- windows-latest
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- name: Test
uses: actions-rs/cargo@v1
with:
command: test
args: -- --nocapture
run: cargo test --all-features
env:
RUST_LOG: DEBUG
RUST_BACKTRACE: full

wasm-unit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Install
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- run: wasm-pack test --node
- run: wasm-pack test --node --all-features
env:
RUST_LOG: DEBUG
RUST_BACKTRACE: full
10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ name = "backon"
repository = "https://github.com/Xuanwo/backon"
version = "0.4.4"

[features]
gloo-timers-sleep = ["dep:gloo-timers", "gloo-timers/futures"]
tokio-sleep = ["dep:tokio", "tokio/time"]

[dependencies]
fastrand = "2.0.0"
tokio = { version = "1", features = ["time"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
gloo-timers = { version = "0.3", features = ["futures"] }
gloo-timers = { version = "0.3", optional = true }

[dev-dependencies]
anyhow = "1"
Expand Down
26 changes: 13 additions & 13 deletions src/blocking_retry_with_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ pub trait BlockingRetryableWithContext<
>
{
/// Generate a new retry
fn retry(self, builder: &B) -> BlockingRetry<B::Backoff, T, E, Ctx, F>;
fn retry(self, builder: &B) -> BlockingRetryWithContext<B::Backoff, T, E, Ctx, F>;
}

impl<B, T, E, Ctx, F> BlockingRetryableWithContext<B, T, E, Ctx, F> for F
where
B: BackoffBuilder,
F: FnMut(Ctx) -> (Ctx, Result<T, E>),
{
fn retry(self, builder: &B) -> BlockingRetry<B::Backoff, T, E, Ctx, F> {
BlockingRetry::new(self, builder.build())
fn retry(self, builder: &B) -> BlockingRetryWithContext<B::Backoff, T, E, Ctx, F> {
BlockingRetryWithContext::new(self, builder.build())
}
}

/// Retry struct generated by [`Retryable`].
pub struct BlockingRetry<
pub struct BlockingRetryWithContext<
B: Backoff,
T,
E,
Expand All @@ -44,14 +44,14 @@ pub struct BlockingRetry<
ctx: Option<Ctx>,
}

impl<B, T, E, Ctx, F> BlockingRetry<B, T, E, Ctx, F>
impl<B, T, E, Ctx, F> BlockingRetryWithContext<B, T, E, Ctx, F>
where
B: Backoff,
F: FnMut(Ctx) -> (Ctx, Result<T, E>),
{
/// Create a new retry.
fn new(f: F, backoff: B) -> Self {
BlockingRetry {
BlockingRetryWithContext {
backoff,
retryable: |_: &E| true,
notify: |_: &E, _: Duration| {},
Expand All @@ -61,16 +61,16 @@ where
}
}

impl<B, T, E, Ctx, F, RF, NF> BlockingRetry<B, T, E, Ctx, F, RF, NF>
impl<B, T, E, Ctx, F, RF, NF> BlockingRetryWithContext<B, T, E, Ctx, F, RF, NF>
where
B: Backoff,
F: FnMut(Ctx) -> (Ctx, Result<T, E>),
RF: FnMut(&E) -> bool,
NF: FnMut(&E, Duration),
{
/// Set the context for retrying.
pub fn context(self, context: Ctx) -> BlockingRetry<B, T, E, Ctx, F, RF, NF> {
BlockingRetry {
pub fn context(self, context: Ctx) -> BlockingRetryWithContext<B, T, E, Ctx, F, RF, NF> {
BlockingRetryWithContext {
backoff: self.backoff,
retryable: self.retryable,
notify: self.notify,
Expand All @@ -85,8 +85,8 @@ where
pub fn when<RN: FnMut(&E) -> bool>(
self,
retryable: RN,
) -> BlockingRetry<B, T, E, Ctx, F, RN, NF> {
BlockingRetry {
) -> BlockingRetryWithContext<B, T, E, Ctx, F, RN, NF> {
BlockingRetryWithContext {
backoff: self.backoff,
retryable,
notify: self.notify,
Expand All @@ -101,8 +101,8 @@ where
pub fn notify<NN: FnMut(&E, Duration)>(
self,
notify: NN,
) -> BlockingRetry<B, T, E, Ctx, F, RF, NN> {
BlockingRetry {
) -> BlockingRetryWithContext<B, T, E, Ctx, F, RF, NN> {
BlockingRetryWithContext {
backoff: self.backoff,
retryable: self.retryable,
notify,
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ pub use retry::Retryable;
mod retry_with_context;
pub use retry_with_context::RetryableWithContext;

mod sleep;
pub use sleep::DefaultSleeper;
#[cfg(all(target_arch = "wasm32", feature = "gloo-timers-sleep"))]
pub use sleep::GlooTimersSleep;
pub use sleep::Sleeper;
#[cfg(all(not(target_arch = "wasm32"), feature = "tokio-sleep"))]
pub use sleep::TokioSleeper;

#[cfg(not(target_arch = "wasm32"))]
mod blocking_retry;
#[cfg(not(target_arch = "wasm32"))]
Expand Down
Loading