Skip to content

Commit

Permalink
Rename runtime module and trait to sleeper
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenperera committed Oct 15, 2024
1 parent e97175c commit 0f01dce
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
14 changes: 7 additions & 7 deletions src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,30 @@ use log::{debug, error, info, trace};

use reqwest::{header, Client, Response};

use crate::runtime::{DefaultRuntime, Runtime};
use crate::sleeper::{DefaultSleeper, Sleeper};
use crate::{
BlockStatus, BlockSummary, Builder, Error, MerkleProof, OutputStatus, Tx, TxStatus,
BASE_BACKOFF_MILLIS, RETRYABLE_ERROR_CODES,
};

#[derive(Debug, Clone)]
pub struct AsyncClient<R = DefaultRuntime> {
pub struct AsyncClient<S = DefaultSleeper> {
/// The URL of the Esplora Server.
url: String,
/// The inner [`reqwest::Client`] to make HTTP requests.
client: Client,
/// Number of times to retry a request
max_retries: usize,

runtime: PhantomData<R>,
runtime: PhantomData<S>,
}

impl<R> AsyncClient<R>
impl<S> AsyncClient<S>
where
R: Runtime,
S: Sleeper,
{
/// Build an async client from a builder
pub fn from_builder(builder: Builder<R>) -> Result<Self, Error> {
pub fn from_builder(builder: Builder<S>) -> Result<Self, Error> {
let mut client_builder = Client::builder();

#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -441,7 +441,7 @@ where
loop {
match self.client.get(url).send().await? {
resp if attempts < self.max_retries && is_status_retryable(resp.status()) => {
R::sleep(delay).await;
S::sleep(delay).await;
attempts += 1;
delay *= 2;
}
Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ pub mod r#async;
#[cfg(feature = "blocking")]
pub mod blocking;
#[cfg(feature = "async")]
pub mod runtime;
pub mod sleeper;

pub use api::*;
#[cfg(feature = "blocking")]
pub use blocking::BlockingClient;
#[cfg(feature = "async")]
pub use r#async::AsyncClient;
use runtime::{DefaultRuntime, Runtime};
use sleeper::{DefaultSleeper, Sleeper};

/// Response status codes for which the request may be retried.
const RETRYABLE_ERROR_CODES: [u16; 3] = [
Expand Down Expand Up @@ -114,7 +114,7 @@ pub fn convert_fee_rate(target: usize, estimates: HashMap<u16, f64>) -> Option<f
}

#[derive(Debug, Clone)]
pub struct Builder<R = DefaultRuntime> {
pub struct Builder<S = DefaultSleeper> {
/// The URL of the Esplora server.
pub base_url: String,
/// Optional URL of the proxy to use to make requests to the Esplora server
Expand All @@ -137,7 +137,7 @@ pub struct Builder<R = DefaultRuntime> {
/// Max retries
pub max_retries: usize,
/// Async runtime, trait must implement `sleep` function, default is `tokio`
pub runtime: PhantomData<R>,
pub runtime: PhantomData<S>,
}

impl Builder {
Expand All @@ -160,9 +160,9 @@ impl Builder {
}
}

impl<R: Runtime> Builder<R>
impl<S: Sleeper> Builder<S>
where
R: Runtime,
S: Sleeper,
{
/// Instantiate a new builder, with a custom runtime
#[cfg(feature = "async")]
Expand Down Expand Up @@ -204,7 +204,7 @@ where

// Build an asynchronous client from builder
#[cfg(feature = "async")]
pub fn build_async(self) -> Result<AsyncClient<R>, Error> {
pub fn build_async(self) -> Result<AsyncClient<S>, Error> {
AsyncClient::from_builder(self)
}
}
Expand Down Expand Up @@ -1029,7 +1029,7 @@ mod test {
struct TestRuntime;

#[cfg(not(feature = "tokio"))]
impl Runtime for TestRuntime {
impl Sleeper for TestRuntime {
async fn sleep(duration: Duration) {
tokio::time::sleep(duration).await;
}
Expand Down
6 changes: 3 additions & 3 deletions src/runtime.rs → src/sleeper.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::time::Duration;

pub trait Runtime {
pub trait Sleeper {
fn sleep(duration: Duration) -> impl std::future::Future<Output = ()> + Send;
}

pub struct DefaultRuntime;
pub struct DefaultSleeper;

#[cfg(feature = "tokio")]
impl Runtime for DefaultRuntime {
impl Sleeper for DefaultSleeper {
async fn sleep(duration: Duration) {
tokio::time::sleep(duration).await;
}
Expand Down

0 comments on commit 0f01dce

Please sign in to comment.