main
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]
default = ["tracing"]
log = ["dep:log"]
tracing = ["dep:tracing"]
[dependencies]
@ -20,7 +19,6 @@ reqwest-middleware = { version = "0.3.0", path = "../reqwest-middleware" }
anyhow = "1.0.0"
async-trait = "0.1.51"
futures = "0.3.0"
log = { version = "0.4.22", optional = true }
http = "1.0"
reqwest = { version = "0.12.0", default-features = false }
retry-policies = "0.4"

View File

@ -9,16 +9,6 @@ use reqwest::{Request, Response};
use reqwest_middleware::{Error, Middleware, Next, Result};
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)]
// We need this macro because tracing expects the level to be const:
// https://github.com/tokio-rs/tracing/issues/2730
@ -26,22 +16,15 @@ use ::log as LogTracing;
macro_rules! log_retry {
($level:expr, $($args:tt)*) => {{
match $level {
LogLevel::TRACE => LogTracing::trace!($($args)*),
LogLevel::DEBUG => LogTracing::debug!($($args)*),
LogLevel::INFO => LogTracing::info!($($args)*),
LogLevel::WARN => LogTracing::warn!($($args)*),
LogLevel::ERROR => LogTracing::error!($($args)*),
::tracing::Level::TRACE => ::tracing::trace!($($args)*),
::tracing::Level::DEBUG => ::tracing::debug!($($args)*),
::tracing::Level::INFO => ::tracing::info!($($args)*),
::tracing::Level::WARN => ::tracing::warn!($($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
/// and can be safely executed again.
///
@ -87,8 +70,8 @@ pub struct RetryTransientMiddleware<
> {
retry_policy: T,
retryable_strategy: R,
#[cfg(any(feature = "log", feature = "tracing"))]
retry_log_level: LogLevel,
#[cfg(feature = "tracing")]
retry_log_level: ::tracing::Level,
}
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.
/// The default is [`WARN`][tracing::Level::WARN].
#[cfg(any(feature = "log", feature = "tracing"))]
pub fn with_retry_log_level(mut self, level: LogLevel) -> Self {
#[cfg(feature = "tracing")]
pub fn with_retry_log_level(mut self, level: ::tracing::Level) -> Self {
self.retry_log_level = level;
self
}
@ -117,9 +100,7 @@ where
retry_policy,
retryable_strategy,
#[cfg(feature = "tracing")]
retry_log_level: LogLevel::WARN,
#[cfg(all(feature = "log", not(feature = "tracing")))]
retry_log_level: LogLevel::Warn,
retry_log_level: ::tracing::Level::WARN,
}
}
}
@ -186,7 +167,7 @@ where
.duration_since(SystemTime::now())
.unwrap_or_else(|_| Duration::default());
// Sleep the requested amount before we try again.
#[cfg(any(feature = "log", feature = "tracing"))]
#[cfg(feature = "tracing")]
log_retry!(
self.retry_log_level,
"Retry attempt #{}. Sleeping {:?} before the next attempt",