-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathclient.rs
More file actions
299 lines (269 loc) · 10 KB
/
client.rs
File metadata and controls
299 lines (269 loc) · 10 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//! Connection pooling [`Client`] for HTTP requests.
//!
//! The [`Client`] caches connections to avoid repeated TCP handshakes and TLS negotiations.
//!
//! When the `async` feature is enabled, the client uses async connections via `tokio`.
//! Otherwise a blocking client backed by `std::net::TcpStream` is provided.
use std::collections::{hash_map, HashMap, VecDeque};
use std::sync::{Arc, Mutex};
// ---------------------------------------------------------------------------
// Async Client (feature = "async")
// ---------------------------------------------------------------------------
#[cfg(feature = "async")]
use crate::connection::AsyncConnection;
use crate::request::{OwnedConnectionParams as ConnectionKey, ParsedRequest};
use crate::{Error, Request, Response};
/// A client that caches connections for reuse.
///
/// The client maintains a pool of up to `capacity` connections, evicting
/// the least recently used connection when the cache is full.
///
/// # Example
///
/// ```no_run
/// # async fn request() {
/// use bitreq::{Client, RequestExt};
///
/// let client = Client::new(10); // Cache up to 10 connections
/// let response = bitreq::get("https://example.com")
/// .send_async_with_client(&client)
/// .await;
/// # }
/// ```
#[cfg(feature = "async")]
#[derive(Clone)]
pub struct Client {
r#async: Arc<Mutex<AsyncClientState>>,
}
#[cfg(feature = "async")]
struct AsyncClientState {
connections: HashMap<ConnectionKey, Arc<AsyncConnection>>,
lru_order: VecDeque<ConnectionKey>,
capacity: usize,
}
#[cfg(feature = "async")]
impl Client {
/// Creates a new `Client` with the specified connection cache capacity.
///
/// # Arguments
///
/// * `capacity` - Maximum number of cached connections. When this limit is
/// reached, the least recently used connection is evicted.
pub fn new(capacity: usize) -> Self {
Client {
r#async: Arc::new(Mutex::new(AsyncClientState {
connections: HashMap::new(),
lru_order: VecDeque::new(),
capacity,
})),
}
}
/// Sends a request asynchronously using a cached connection if available.
pub async fn send_async(&self, request: Request) -> Result<Response, Error> {
let parsed_request = ParsedRequest::new(request)?;
let key = parsed_request.connection_params();
let owned_key = key.into();
// Try to get cached connection
let conn_opt = {
let state = self.r#async.lock().unwrap();
if let Some(conn) = state.connections.get(&owned_key) {
Some(Arc::clone(conn))
} else {
None
}
};
let conn = if let Some(conn) = conn_opt {
conn
} else {
let connection = AsyncConnection::new(key, parsed_request.timeout_at).await?;
let connection = Arc::new(connection);
let mut state = self.r#async.lock().unwrap();
if let hash_map::Entry::Vacant(entry) = state.connections.entry(owned_key) {
entry.insert(Arc::clone(&connection));
state.lru_order.push_back(key.into());
if state.connections.len() > state.capacity {
if let Some(oldest_key) = state.lru_order.pop_front() {
state.connections.remove(&oldest_key);
}
}
}
connection
};
// Send the request
conn.send(parsed_request).await
}
}
/// Extension trait for [`Request`] to use with [`Client`].
#[cfg(feature = "async")]
pub trait RequestExt {
/// Sends this request asynchronously using the provided client's connection pool.
fn send_async_with_client(
self,
client: &Client,
) -> impl std::future::Future<Output = Result<Response, Error>>;
}
#[cfg(feature = "async")]
impl RequestExt for Request {
fn send_async_with_client(
self,
client: &Client,
) -> impl std::future::Future<Output = Result<Response, Error>> {
client.send_async(self)
}
}
// ---------------------------------------------------------------------------
// Blocking Client (no "async" feature)
// ---------------------------------------------------------------------------
#[cfg(not(feature = "async"))]
use core::time::Duration;
#[cfg(not(feature = "async"))]
use std::time::Instant;
#[cfg(not(feature = "async"))]
use crate::connection::{Connection, HttpStream};
#[cfg(not(feature = "async"))]
use crate::Method;
#[cfg(not(feature = "async"))]
struct PoolEntry {
stream: HttpStream,
expires_at: Instant,
}
/// A client that caches connections for reuse.
///
/// The client maintains a pool of up to `capacity` connections, evicting
/// the least recently used connection when the cache is full. A cached
/// connection is reused when the server indicated `Connection: keep-alive`
/// and the keep-alive timeout has not yet expired.
///
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), bitreq::Error> {
/// use bitreq::{Client, RequestExt};
///
/// let client = Client::new(10); // Cache up to 10 connections
/// let response = bitreq::get("http://example.com")
/// .send_with_client(&client)?;
/// # Ok(()) }
/// ```
#[cfg(not(feature = "async"))]
#[derive(Clone)]
pub struct Client {
state: Arc<Mutex<BlockingClientState>>,
}
#[cfg(not(feature = "async"))]
struct BlockingClientState {
connections: HashMap<ConnectionKey, PoolEntry>,
lru_order: VecDeque<ConnectionKey>,
capacity: usize,
}
#[cfg(not(feature = "async"))]
impl Client {
/// Creates a new `Client` with the specified connection cache capacity.
///
/// # Arguments
///
/// * `capacity` - Maximum number of cached connections. When this limit is
/// reached, the least recently used connection is evicted.
pub fn new(capacity: usize) -> Self {
Client {
state: Arc::new(Mutex::new(BlockingClientState {
connections: HashMap::new(),
lru_order: VecDeque::new(),
capacity,
})),
}
}
/// Sends a request using a cached connection if available.
pub fn send(&self, request: Request) -> Result<Response, Error> {
let parsed_request = ParsedRequest::new(request)?;
self.send_inner(parsed_request)
}
fn send_inner(&self, mut request: ParsedRequest) -> Result<Response, Error> {
loop {
let key: ConnectionKey = request.connection_params().into();
// Get cached stream or create new connection
let connection = match self.take_stream(&key) {
Some(stream) => Connection::from_stream(stream),
None => Connection::new(request.connection_params(), request.timeout_at)?,
};
let (response, stream, req) = connection.send_for_pool(request)?;
request = req;
// Cache stream if keep-alive, with expiry
if let Some(stream) = stream {
let expires_at = Self::parse_keep_alive_timeout(response.headers.get("keep-alive"));
self.put_stream(key, stream, expires_at);
}
// Handle redirects
match response.status_code {
301 | 302 | 303 | 307 => {
let location = response
.headers
.get("location")
.ok_or(Error::RedirectLocationMissing)?
.clone();
request.redirect_to(&location)?;
if response.status_code == 303 {
match request.config.method {
Method::Post | Method::Put | Method::Delete => {
request.config.method = Method::Get;
}
_ => {}
}
}
continue;
}
_ => return Ok(response),
}
}
}
fn take_stream(&self, key: &ConnectionKey) -> Option<HttpStream> {
let mut state = self.state.lock().unwrap();
if let Some(entry) = state.connections.remove(key) {
// Remove from LRU order
if let Some(pos) = state.lru_order.iter().position(|k| k == key) {
state.lru_order.remove(pos);
}
if entry.expires_at > Instant::now() {
return Some(entry.stream);
}
}
None
}
fn put_stream(&self, key: ConnectionKey, stream: HttpStream, expires_at: Instant) {
let mut state = self.state.lock().unwrap();
if let hash_map::Entry::Vacant(entry) = state.connections.entry(key.clone()) {
entry.insert(PoolEntry { stream, expires_at });
state.lru_order.push_back(key);
if state.connections.len() > state.capacity {
if let Some(oldest_key) = state.lru_order.pop_front() {
state.connections.remove(&oldest_key);
}
}
}
}
fn parse_keep_alive_timeout(keep_alive_header: Option<&String>) -> Instant {
let default_timeout = Instant::now() + Duration::from_secs(60);
if let Some(header) = keep_alive_header {
for param in header.split(',') {
if let Some((k, v)) = param.trim().split_once('=') {
if k.trim() == "timeout" {
if let Ok(secs) = v.parse::<u64>() {
return Instant::now() + Duration::from_secs(secs.saturating_sub(1));
}
}
}
}
}
default_timeout
}
}
/// Extension trait for [`Request`] to use with [`Client`].
#[cfg(not(feature = "async"))]
pub trait RequestExt {
/// Sends this request using the provided client's connection pool.
fn send_with_client(self, client: &Client) -> Result<Response, Error>;
}
#[cfg(not(feature = "async"))]
impl RequestExt for Request {
fn send_with_client(self, client: &Client) -> Result<Response, Error> { client.send(self) }
}