1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//! Rate limiting helpers and optional wrapper ergonomics for `reqwest`.
//!
//! This crate keeps `reqwest` at the center while adding helpers to apply rate limits
//! and, optionally, a small wrapper client with middleware hooks.
//!
//! # Example
//!
//! ```no_run
//! use governor::Quota;
//! use std::num::NonZeroU32;
//! use std::sync::Arc;
//!
//! let rate_limiter = Arc::new(governor::RateLimiter::direct(Quota::per_hour(
//! NonZeroU32::new(5_000).unwrap(),
//! )));
//!
//! let client = reqwest_rate_limit::Client::builder()
//! .user_agent("reqwest-rate-limit-docs")
//! .configure(|b| b.timeout(std::time::Duration::from_secs(30)))
//! .build()
//! .unwrap();
//!
//! let _future = client
//! .get("https://api.example.com/v1/health")
//! .with_rate_limiter(rate_limiter)
//! .send();
//! ```
//!
//! # Why `configure`?
//!
//! `ClientBuilder::configure` gives you access to the full surface of
//! `reqwest::ClientBuilder` without this crate having to mirror every method.
//! That means you can use all of reqwest's options and still keep the wrapper
//! ergonomics and middleware hooks.
//!
//! ```no_run
//! # use governor::Quota;
//! # use std::num::NonZeroU32;
//! # use std::sync::Arc;
//! # let rate_limiter = Arc::new(governor::RateLimiter::direct(Quota::per_hour(
//! # NonZeroU32::new(5_000).unwrap(),
//! # )));
//! let client = reqwest_rate_limit::Client::builder()
//! .configure(|b| {
//! b.timeout(std::time::Duration::from_secs(10))
//! .pool_max_idle_per_host(8)
//! .https_only(true)
//! })
//! .rate_limiter(rate_limiter)
//! .build()
//! .unwrap();
//! ```
//!
//! If you do not want the wrapper, use `send_with_rate_limiter` with a plain
//! `reqwest::Client` instead.
//!
//! # ResponseMiddleware
//!
//! Implement `ResponseMiddleware` to inspect responses and apply rate-limit rules.
//! The `github_rest_api.rs` example shows how to translate
//! `retry-after` headers into concrete waits and backoff behavior.
//!
//! # Features
//!
//! This crate forwards optional `reqwest` features:
//! `json`, `form`, `query`, and `multipart`.
/// Re-exported for convenience when constructing rate limiters.
pub use governor;
/// Wrapper client that layers rate limiting and middleware hooks over `reqwest`.
pub use ;
/// Intercept a response to apply rate-limit aware behavior.
/// Default middleware that returns the response unchanged.
;
/// Send a request after waiting for the rate limiter to allow it.
///
/// # Examples
///
/// ```no_run
/// use governor::{Quota, RateLimiter};
/// use std::num::NonZeroU32;
///
/// # async fn example() -> reqwest::Result<()> {
/// let client = reqwest::Client::new();
/// let request = client.get("https://api.example.com/health");
/// let limiter = RateLimiter::direct(Quota::per_second(NonZeroU32::new(5).unwrap()));
/// let _response = reqwest_rate_limit::send_with_rate_limiter(request, &limiter).await?;
/// # Ok(())
/// # }
/// ```
pub async
/// Send a request through a response middleware after rate limiting.
///
/// # Examples
///
/// ```no_run
/// use governor::{Quota, RateLimiter};
/// use std::num::NonZeroU32;
///
/// struct Passthrough;
///
/// impl reqwest_rate_limit::ResponseMiddleware for Passthrough {
/// type Error = reqwest::Error;
///
/// fn on_response(
/// &self,
/// response: reqwest::Result<reqwest::Response>,
/// ) -> Result<reqwest::Response, Self::Error> {
/// response
/// }
/// }
///
/// # async fn example() -> Result<(), reqwest::Error> {
/// let client = reqwest::Client::new();
/// let request = client.get("https://api.example.com/health");
/// let limiter = RateLimiter::direct(Quota::per_second(NonZeroU32::new(5).unwrap()));
/// let middleware = Passthrough;
/// let _response =
/// reqwest_rate_limit::send_with_rate_limiter_and_middleware(request, &limiter, &middleware)
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async