This commit is contained in:
Piotr Zaczkowski 2024-08-06 22:17:25 +02:00 committed by Ethan Brierley
parent d966ad89e5
commit d5290d056f
2 changed files with 11 additions and 32 deletions

View file

@ -11,7 +11,6 @@ categories = ["web-programming::http-client"]
[features] [features]
default = ["tracing"] default = ["tracing"]
log = ["dep:log"]
tracing = ["dep:tracing"] tracing = ["dep:tracing"]
[dependencies] [dependencies]
@ -20,7 +19,6 @@ 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"

View file

@ -9,16 +9,6 @@ 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
@ -26,22 +16,15 @@ use ::log as LogTracing;
macro_rules! log_retry { macro_rules! log_retry {
($level:expr, $($args:tt)*) => {{ ($level:expr, $($args:tt)*) => {{
match $level { match $level {
LogLevel::TRACE => LogTracing::trace!($($args)*), ::tracing::Level::TRACE => ::tracing::trace!($($args)*),
LogLevel::DEBUG => LogTracing::debug!($($args)*), ::tracing::Level::DEBUG => ::tracing::debug!($($args)*),
LogLevel::INFO => LogTracing::info!($($args)*), ::tracing::Level::INFO => ::tracing::info!($($args)*),
LogLevel::WARN => LogTracing::warn!($($args)*), ::tracing::Level::WARN => ::tracing::warn!($($args)*),
LogLevel::ERROR => LogTracing::error!($($args)*), ::tracing::Level::ERROR => ::tracing::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.
/// ///
@ -87,8 +70,8 @@ pub struct RetryTransientMiddleware<
> { > {
retry_policy: T, retry_policy: T,
retryable_strategy: R, retryable_strategy: R,
#[cfg(any(feature = "log", feature = "tracing"))] #[cfg(feature = "tracing")]
retry_log_level: LogLevel, retry_log_level: ::tracing::Level,
} }
impl<T: RetryPolicy + Send + Sync> RetryTransientMiddleware<T, DefaultRetryableStrategy> { impl<T: RetryPolicy + Send + Sync> RetryTransientMiddleware<T, DefaultRetryableStrategy> {
@ -99,8 +82,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(any(feature = "log", feature = "tracing"))] #[cfg(feature = "tracing")]
pub fn with_retry_log_level(mut self, level: LogLevel) -> Self { pub fn with_retry_log_level(mut self, level: ::tracing::Level) -> Self {
self.retry_log_level = level; self.retry_log_level = level;
self self
} }
@ -117,9 +100,7 @@ where
retry_policy, retry_policy,
retryable_strategy, retryable_strategy,
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
retry_log_level: LogLevel::WARN, retry_log_level: ::tracing::Level::WARN,
#[cfg(all(feature = "log", not(feature = "tracing")))]
retry_log_level: LogLevel::Warn,
} }
} }
} }
@ -186,7 +167,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(any(feature = "log", feature = "tracing"))] #[cfg(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",