Available on crate feature
tower-delay only.Expand description
Request delay middleware.
Adds configurable delays before HTTP requests — useful for rate limiting, testing under slow network conditions, or just being polite to APIs.
§Quick Start
Fixed 1-second delay:
use std::time::Duration;
use wreq::Client;
use wreq_util::middleware::delay::DelayLayer;
let client = Client::builder()
.layer(DelayLayer::new(Duration::from_secs(1)))
.build()?;Random jitter (0.8s ~ 1.2s):
use std::time::Duration;
use wreq::Client;
use wreq_util::middleware::delay::JitterDelayLayer;
let client = Client::builder()
.layer(JitterDelayLayer::new(Duration::from_secs(1), 0.2))
.build()?;§Conditional Delays
Use .when() to apply delays only to matching requests:
ⓘ
// Only delay POST requests
DelayLayer::new(Duration::from_secs(1))
.when(|req: &http::Request<_>| req.method() == http::Method::POST)
// Jitter on specific paths
JitterDelayLayer::new(Duration::from_millis(500), 0.3)
.when(|req: &http::Request<_>| req.uri().path().starts_with("/api"))§Notes
- Delays are async and won’t block the runtime
- Not a substitute for proper rate limiters — servers can still see timing patterns
- Keep delays short in hot paths
Structs§
- Delay
- A Tower
Servicethat introduces a fixed delay before each request. - Delay
Layer - A Tower
Layerthat introduces a fixed delay before each request. - Delay
Layer With - Conditional delay
Layer, applies delay based on a predicate. - Delay
With - A Tower
Servicethat conditionally applies fixed delay based on a predicate. - Jitter
Delay - A Tower
Servicethat applies jittered delay to requests. - Jitter
Delay Layer - A Tower
Layerthat introduces a jittered delay before each request. - Jitter
Delay Layer With - Conditional jitter delay
Layer, applies delay based on a predicate. - Jitter
Delay With - A Tower
Servicethat conditionally applies jittered delay based on a predicate. - Response
Future - Response future for
Delay.