Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Zaczkowski authored and eopb committed Aug 28, 2024
1 parent d966ad8 commit d5290d0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 32 deletions.
2 changes: 0 additions & 2 deletions reqwest-retry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ categories = ["web-programming::http-client"]

[features]
default = ["tracing"]
log = ["dep:log"]
tracing = ["dep:tracing"]

[dependencies]
Expand All @@ -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"
Expand Down
41 changes: 11 additions & 30 deletions reqwest-retry/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,22 @@ 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
#[cfg(feature = "tracing")]
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.
///
Expand Down Expand Up @@ -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> {
Expand All @@ -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
}
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit d5290d0

Please sign in to comment.