parent
6fdc6773f8
commit
b0b3b5b8c1
|
@ -11,6 +11,7 @@ categories = ["web-programming::http-client"]
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["tracing"]
|
default = ["tracing"]
|
||||||
|
log = ["dep:log"]
|
||||||
tracing = ["dep:tracing"]
|
tracing = ["dep:tracing"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
@ -19,6 +20,7 @@ reqwest-middleware = { version = "0.3.0", path = "../reqwest-middleware" }
|
||||||
anyhow = "1.0.0"
|
anyhow = "1.0.0"
|
||||||
async-trait = "0.1.51"
|
async-trait = "0.1.51"
|
||||||
futures = "0.3.0"
|
futures = "0.3.0"
|
||||||
|
log = { version = "0.4.22", optional = true }
|
||||||
http = "1.0"
|
http = "1.0"
|
||||||
reqwest = { version = "0.12.0", default-features = false }
|
reqwest = { version = "0.12.0", default-features = false }
|
||||||
retry-policies = "0.4"
|
retry-policies = "0.4"
|
||||||
|
|
|
@ -9,6 +9,16 @@ use reqwest::{Request, Response};
|
||||||
use reqwest_middleware::{Error, Middleware, Next, Result};
|
use reqwest_middleware::{Error, Middleware, Next, Result};
|
||||||
use retry_policies::RetryPolicy;
|
use retry_policies::RetryPolicy;
|
||||||
|
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
type LogLevel = ::tracing::Level;
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
use ::tracing as LogTracing;
|
||||||
|
|
||||||
|
#[cfg(all(feature = "log", not(feature = "tracing")))]
|
||||||
|
type LogLevel = ::log::Level;
|
||||||
|
#[cfg(all(feature = "log", not(feature = "tracing")))]
|
||||||
|
use ::log as LogTracing;
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
// We need this macro because tracing expects the level to be const:
|
// We need this macro because tracing expects the level to be const:
|
||||||
// https://github.com/tokio-rs/tracing/issues/2730
|
// https://github.com/tokio-rs/tracing/issues/2730
|
||||||
|
@ -16,15 +26,22 @@ use retry_policies::RetryPolicy;
|
||||||
macro_rules! log_retry {
|
macro_rules! log_retry {
|
||||||
($level:expr, $($args:tt)*) => {{
|
($level:expr, $($args:tt)*) => {{
|
||||||
match $level {
|
match $level {
|
||||||
::tracing::Level::TRACE => ::tracing::trace!($($args)*),
|
LogLevel::TRACE => LogTracing::trace!($($args)*),
|
||||||
::tracing::Level::DEBUG => ::tracing::debug!($($args)*),
|
LogLevel::DEBUG => LogTracing::debug!($($args)*),
|
||||||
::tracing::Level::INFO => ::tracing::info!($($args)*),
|
LogLevel::INFO => LogTracing::info!($($args)*),
|
||||||
::tracing::Level::WARN => ::tracing::warn!($($args)*),
|
LogLevel::WARN => LogTracing::warn!($($args)*),
|
||||||
::tracing::Level::ERROR => ::tracing::error!($($args)*),
|
LogLevel::ERROR => LogTracing::error!($($args)*),
|
||||||
}
|
}
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(all(feature = "log", not(feature = "tracing")))]
|
||||||
|
macro_rules! log_retry {
|
||||||
|
($level:expr, $($args:tt)*) => {{
|
||||||
|
LogTracing::log!($level, $($args)*)
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
/// `RetryTransientMiddleware` offers retry logic for requests that fail in a transient manner
|
/// `RetryTransientMiddleware` offers retry logic for requests that fail in a transient manner
|
||||||
/// and can be safely executed again.
|
/// and can be safely executed again.
|
||||||
///
|
///
|
||||||
|
@ -70,8 +87,8 @@ pub struct RetryTransientMiddleware<
|
||||||
> {
|
> {
|
||||||
retry_policy: T,
|
retry_policy: T,
|
||||||
retryable_strategy: R,
|
retryable_strategy: R,
|
||||||
#[cfg(feature = "tracing")]
|
#[cfg(any(feature = "log", feature = "tracing"))]
|
||||||
retry_log_level: ::tracing::Level,
|
retry_log_level: LogLevel,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: RetryPolicy + Send + Sync> RetryTransientMiddleware<T, DefaultRetryableStrategy> {
|
impl<T: RetryPolicy + Send + Sync> RetryTransientMiddleware<T, DefaultRetryableStrategy> {
|
||||||
|
@ -82,8 +99,8 @@ impl<T: RetryPolicy + Send + Sync> RetryTransientMiddleware<T, DefaultRetryableS
|
||||||
|
|
||||||
/// Set the log [level][tracing::Level] for retry events.
|
/// Set the log [level][tracing::Level] for retry events.
|
||||||
/// The default is [`WARN`][tracing::Level::WARN].
|
/// The default is [`WARN`][tracing::Level::WARN].
|
||||||
#[cfg(feature = "tracing")]
|
#[cfg(any(feature = "log", feature = "tracing"))]
|
||||||
pub fn with_retry_log_level(mut self, level: ::tracing::Level) -> Self {
|
pub fn with_retry_log_level(mut self, level: LogLevel) -> Self {
|
||||||
self.retry_log_level = level;
|
self.retry_log_level = level;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -100,7 +117,9 @@ where
|
||||||
retry_policy,
|
retry_policy,
|
||||||
retryable_strategy,
|
retryable_strategy,
|
||||||
#[cfg(feature = "tracing")]
|
#[cfg(feature = "tracing")]
|
||||||
retry_log_level: ::tracing::Level::WARN,
|
retry_log_level: LogLevel::WARN,
|
||||||
|
#[cfg(all(feature = "log", not(feature = "tracing")))]
|
||||||
|
retry_log_level: LogLevel::Warn,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -167,7 +186,7 @@ where
|
||||||
.duration_since(SystemTime::now())
|
.duration_since(SystemTime::now())
|
||||||
.unwrap_or_else(|_| Duration::default());
|
.unwrap_or_else(|_| Duration::default());
|
||||||
// Sleep the requested amount before we try again.
|
// Sleep the requested amount before we try again.
|
||||||
#[cfg(feature = "tracing")]
|
#[cfg(any(feature = "log", feature = "tracing"))]
|
||||||
log_retry!(
|
log_retry!(
|
||||||
self.retry_log_level,
|
self.retry_log_level,
|
||||||
"Retry attempt #{}. Sleeping {:?} before the next attempt",
|
"Retry attempt #{}. Sleeping {:?} before the next attempt",
|
||||||
|
|
Loading…
Reference in New Issue