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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use io;
use Path;
pub use reqwest;
pub use ;
pub use ;
pub use ;
use DataStore;
use Arc;
/// Initializes only the cache middleware with a file-based data store.
///
/// This function creates a new `DriveCache` instance backed by a `DataStore` file.
///
/// ## Concurrency
///
/// Thread-safe within a single process.
/// Not multi-process safe when multiple processes use the same cache file concurrently.
///
/// # Arguments
///
/// * `cache_storage_file` - Path to the file where cached responses are stored.
/// * `policy` - The cache expiration policy.
///
/// # Returns
///
/// An `Arc<DriveCache>` instance managing the cache.
/// Initializes only the cache middleware using a discovered process-scoped cache location.
///
/// This uses a cache group derived from this crate's package name and creates a
/// process/thread-scoped cache storage file automatically, so callers do not need
/// to manually provide a cache path.
///
/// The underlying cache root is discovered via `CacheRoot::from_discovery()`.
///
/// # Errors
///
/// Returns an error if cache root discovery, process-scoped directory creation,
/// or store initialization fails.
/// Initializes both cache and throttle middleware with a file-based data store.
///
/// This function creates:
/// - A `DriveCache` instance for response caching.
/// - A `DriveThrottleBackoff` instance for rate-limiting and retrying failed requests.
///
/// ## Concurrency
///
/// The cache component is thread-safe within a process, but the cache file
/// should not be shared concurrently across multiple processes.
///
/// # Arguments
///
/// * `cache_storage_file` - Path to the file where cached responses are stored.
/// * `cache_policy` - The cache expiration policy.
/// * `throttle_policy` - The throttling and backoff policy.
///
/// # Returns
///
/// A tuple containing:
/// - `Arc<DriveCache>` for caching.
/// - `Arc<DriveThrottleBackoff>` for throttling.
/// Initializes cache and throttle middleware using a discovered process-scoped cache location.
///
/// This uses a cache group derived from this crate's package name and creates a
/// process/thread-scoped cache storage file automatically, so callers do not need
/// to manually provide a cache path.
///
/// # Errors
///
/// Returns an error if cache root discovery, process-scoped directory creation,
/// or store initialization fails.
/// Initializes only the cache middleware using an **existing** `Arc<DataStore>`.
///
/// This function is useful if a shared `DataStore` instance already exists
/// and should be reused instead of creating a new one.
///
/// ## Concurrency
///
/// Thread-safe within a process.
/// Avoid sharing the same underlying store/file concurrently across processes.
///
/// # Arguments
///
/// * `store` - A shared `Arc<DataStore>` instance.
/// * `policy` - The cache expiration policy.
///
/// # Returns
///
/// An `Arc<DriveCache>` instance managing the cache.
/// Initializes both cache and throttle middleware using an **existing** `Arc<DataStore>`.
///
/// This function is useful if a shared `DataStore` instance already exists
/// and should be reused instead of creating a new one.
///
/// ## Concurrency
///
/// Thread-safe within a process.
/// Avoid concurrent multi-process access to the same backing store/file.
///
/// # Arguments
///
/// * `store` - A shared `Arc<DataStore>` instance.
/// * `cache_policy` - The cache expiration policy.
/// * `throttle_policy` - The throttling and backoff policy.
///
/// # Returns
///
/// A tuple containing:
/// - `Arc<DriveCache>` for caching.
/// - `Arc<DriveThrottleBackoff>` for throttling.
/// Initializes only the throttle middleware without any cache or data store.
///
/// This mode applies request throttling and retry/backoff logic only.
/// No persistent storage is required.
///
/// # Arguments
///
/// * `throttle_policy` - The throttling and backoff policy.
///
/// # Returns
///
/// An `Arc<DriveThrottleBackoff>` instance for throttling requests.
///
/// # Example
///
/// ```no_run
/// use reqwest_drive::{init_throttle, ThrottlePolicy};
/// use reqwest_middleware::ClientBuilder;
///
/// #[tokio::main]
/// async fn main() {
/// let throttle = init_throttle(ThrottlePolicy {
/// base_delay_ms: 200,
/// adaptive_jitter_ms: 100,
/// max_concurrent: 2,
/// max_retries: 2,
/// });
///
/// let client = ClientBuilder::new(reqwest::Client::new())
/// .with_arc(throttle)
/// .build();
///
/// let response = client.get("https://httpbin.org/get").send().await.unwrap();
/// assert!(response.status().is_success());
/// }
/// ```
/// Initializes a `reqwest` client with both cache and throttle middleware.
///
/// This function constructs a `ClientWithMiddleware` by attaching:
/// - A `DriveCache` instance for caching HTTP responses.
/// - A `DriveThrottleBackoff` instance for request throttling and backoff handling.
///
/// ## Arguments
///
/// * `cache` - A shared `Arc<DriveCache>` instance for caching responses.
/// * `throttle` - A shared `Arc<DriveThrottleBackoff>` instance for throttling requests.
///
/// ## Returns
///
/// A `ClientWithMiddleware` instance that includes both caching and throttling.
///
/// ## Example
///
/// ```rust
/// use reqwest_drive::{init_cache_with_throttle, init_client_with_cache_and_throttle, CachePolicy, ThrottlePolicy};
/// use reqwest_middleware::ClientWithMiddleware;
/// use std::time::Duration;
/// use tempfile::tempdir;
///
/// #[tokio::main]
/// async fn main() {
/// let temp_dir = tempdir().unwrap();
/// let cache_path = temp_dir.path().join("cache_storage.bin");
///
/// let cache_policy = CachePolicy {
/// default_ttl: Duration::from_secs(60),
/// respect_headers: true,
/// cache_status_override: None,
/// };
///
/// let throttle_policy = ThrottlePolicy {
/// base_delay_ms: 200,
/// adaptive_jitter_ms: 100,
/// max_concurrent: 2,
/// max_retries: 2,
/// };
///
/// let (cache, throttle) = init_cache_with_throttle(&cache_path, cache_policy, throttle_policy);
///
/// let client: ClientWithMiddleware = init_client_with_cache_and_throttle(cache, throttle);
///
/// let response = client.get("https://httpbin.org/get").send().await.unwrap();
///
/// assert!(response.status().is_success());
/// }
/// ```