subscription-proxy-pool 0.1.0

Subscription-backed HTTP and SOCKS proxy pools with caching, health checks, and request-count or time-based rotation
Documentation
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use std::{
    collections::HashMap,
    num::{NonZeroU32, NonZeroU64, NonZeroUsize},
    sync::{Arc, Mutex},
    time::Duration,
};

use subscription_proxy_pool::{
    Error, HealthPolicy, ProxyNode, ProxyPool, RotationPolicy,
    reqwest::{Body, Method, Request, StatusCode, Version},
};
use tokio::{
    io::{AsyncReadExt, AsyncWriteExt},
    net::TcpListener,
    task::{JoinHandle, JoinSet},
};

/// A local forward proxy that records the absolute request URI and responds
/// itself. It never resolves or connects to the request's target.
struct MockProxy {
    node: ProxyNode,
    requests: Arc<Mutex<Vec<String>>>,
    task: JoinHandle<()>,
}

#[derive(Clone, Copy)]
enum ProxyBehavior {
    Respond(u16),
    Timeout,
    Disconnect,
}

impl MockProxy {
    async fn responding(name: &str, status: u16) -> Self {
        Self::start(name, ProxyBehavior::Respond(status)).await
    }

    async fn timing_out(name: &str) -> Self {
        Self::start(name, ProxyBehavior::Timeout).await
    }

    async fn disconnecting(name: &str) -> Self {
        Self::start(name, ProxyBehavior::Disconnect).await
    }

    async fn start(name: &str, behavior: ProxyBehavior) -> Self {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let node = ProxyNode::from_url(&format!("http://{}", listener.local_addr().unwrap()))
            .unwrap()
            .with_name(name);
        let requests = Arc::new(Mutex::new(Vec::new()));
        let recorded = Arc::clone(&requests);
        let body = name.to_owned();
        let task = tokio::spawn(async move {
            loop {
                let (mut socket, _) = listener.accept().await.unwrap();
                let mut headers = Vec::new();
                loop {
                    let mut chunk = [0_u8; 1024];
                    let count = socket.read(&mut chunk).await.unwrap();
                    if count == 0 {
                        break;
                    }
                    headers.extend_from_slice(&chunk[..count]);
                    if headers.windows(4).any(|window| window == b"\r\n\r\n") {
                        break;
                    }
                    assert!(headers.len() < 16_384, "unexpectedly large request headers");
                }
                if headers.is_empty() {
                    continue;
                }
                let request = String::from_utf8(headers).unwrap();
                recorded
                    .lock()
                    .unwrap()
                    .push(request.lines().next().unwrap().to_owned());
                let status = match behavior {
                    ProxyBehavior::Respond(status) => status,
                    ProxyBehavior::Timeout => {
                        // Keep this connection open until the test drops the server.
                        std::future::pending::<()>().await;
                        unreachable!();
                    }
                    ProxyBehavior::Disconnect => continue,
                };
                let response = format!(
                    "HTTP/1.1 {status} Mock\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
                    body.len()
                );
                socket.write_all(response.as_bytes()).await.unwrap();
                socket.shutdown().await.unwrap();
            }
        });
        Self {
            node,
            requests,
            task,
        }
    }

    fn requests(&self) -> Vec<String> {
        self.requests.lock().unwrap().clone()
    }
}

impl Drop for MockProxy {
    fn drop(&mut self) {
        self.task.abort();
    }
}

fn rotation(requests: u64) -> RotationPolicy {
    RotationPolicy {
        requests_per_proxy: NonZeroU64::new(requests),
        ..RotationPolicy::default()
    }
}

fn health() -> HealthPolicy {
    HealthPolicy {
        check_on_build: false,
        check_url: Some("http://127.0.0.1:1/health".into()),
        cooldown: Duration::from_secs(60),
        cooldown_jitter: 0.0,
        ..HealthPolicy::default()
    }
}

async fn pool(nodes: impl IntoIterator<Item = ProxyNode>, requests: u64) -> ProxyPool {
    ProxyPool::builder()
        .nodes(nodes)
        .rotation(rotation(requests))
        .health(health())
        .request_timeout(Duration::from_secs(2))
        .build()
        .await
        .unwrap()
}

fn request(method: Method) -> Request {
    Request::new(
        method,
        "http://127.0.0.1:1/resource?case=rotation".parse().unwrap(),
    )
}

#[tokio::test]
async fn execute_sends_exactly_two_requests_through_each_proxy_before_switching() {
    let first = MockProxy::responding("first", 200).await;
    let second = MockProxy::responding("second", 200).await;
    let pool = pool([first.node.clone(), second.node.clone()], 2).await;

    for expected in ["first", "first", "second", "second", "first", "first"] {
        let response = pool.execute(request(Method::GET)).await.unwrap();
        assert_eq!(response.text().await.unwrap(), expected);
    }

    assert_eq!(first.requests().len(), 4);
    assert_eq!(second.requests().len(), 2);
    for request in first.requests().into_iter().chain(second.requests()) {
        assert_eq!(
            request,
            "GET http://127.0.0.1:1/resource?case=rotation HTTP/1.1"
        );
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn concurrent_pool_clones_share_one_request_budget() {
    // Acquisitions do not send network traffic or run probes.
    let nodes = (0..4).map(|index| {
        ProxyNode::from_url(&format!("http://127.0.0.1:{}", 10_001 + index))
            .unwrap()
            .with_name(format!("node-{index}"))
    });
    let pool = pool(nodes, 20).await;
    let mut tasks = JoinSet::new();
    for _ in 0..8 {
        let clone = pool.clone();
        tasks.spawn(async move {
            let mut names = Vec::new();
            for _ in 0..100 {
                names.push(clone.acquire().unwrap().node().name().to_owned());
                tokio::task::yield_now().await;
            }
            names
        });
    }
    let mut counts = HashMap::new();
    while let Some(task) = tasks.join_next().await {
        for name in task.unwrap() {
            *counts.entry(name).or_insert(0) += 1;
        }
    }
    for index in 0..4 {
        assert_eq!(counts[&format!("node-{index}")], 200);
    }
    assert_eq!(pool.acquire().unwrap().node().name(), "node-0");
}

#[tokio::test]
async fn manual_rotation_through_a_clone_preserves_shared_round_robin_position() {
    let nodes = (0..3).map(|index| {
        ProxyNode::from_url(&format!("http://127.0.0.1:{}", 11_001 + index))
            .unwrap()
            .with_name(format!("node-{index}"))
    });
    let pool = pool(nodes, 0).await;
    let clone = pool.clone();
    for expected in ["node-0", "node-1", "node-2", "node-0"] {
        for _ in 0..25 {
            assert_eq!(pool.acquire().unwrap().node().name(), expected);
        }
        clone.rotate();
    }
}

#[tokio::test]
async fn get_failover_changes_proxy_after_transport_timeout_before_cooldown_threshold() {
    let first = MockProxy::timing_out("first").await;
    let second = MockProxy::responding("second", 200).await;
    let pool = ProxyPool::builder()
        .nodes([first.node.clone(), second.node.clone()])
        .health(health())
        .request_timeout(Duration::from_millis(300))
        .build()
        .await
        .unwrap();

    let response = pool
        .execute_with_failover(request(Method::GET), NonZeroUsize::new(3).unwrap())
        .await
        .unwrap();
    assert_eq!(response.text().await.unwrap(), "second");
    assert_eq!(first.requests().len(), 1);
    assert_eq!(second.requests().len(), 1);
    // A single failure does not yet quarantine the first node (threshold = 2).
    assert_eq!(pool.stats().eligible, 2);
}

#[tokio::test]
async fn disconnects_reselect_sticky_pools_and_sessions_and_eventually_quarantine() {
    for use_session in [false, true] {
        let broken = MockProxy::disconnecting("broken").await;
        let healthy = MockProxy::responding("healthy", 200).await;
        let pool = ProxyPool::builder()
            .nodes([broken.node.clone(), healthy.node.clone()])
            .rotation(RotationPolicy::sticky())
            .max_in_flight_per_proxy(NonZeroUsize::new(1).unwrap())
            .request_timeout(Duration::from_secs(2))
            .build()
            .await
            .unwrap();
        let session = use_session.then(|| pool.session(RotationPolicy::sticky()).unwrap());

        for (attempt, eligible) in [2, 1].into_iter().enumerate() {
            let result = match &session {
                Some(session) => session.execute(request(Method::GET)).await,
                None => pool.execute(request(Method::GET)).await,
            };
            assert!(matches!(result, Err(Error::Transport(error))
                if error.is_request() && !error.is_connect() && !error.is_timeout()));
            assert_eq!(pool.stats().eligible, eligible);
            assert_eq!(pool.stats().in_flight, 0);
            assert_eq!(broken.requests().len(), attempt + 1);
            assert_eq!(healthy.requests().len(), attempt, "execute must not replay");

            let response = match &session {
                Some(session) => session.execute(request(Method::GET)).await,
                None => pool.execute(request(Method::GET)).await,
            }
            .unwrap();
            assert_eq!(response.text().await.unwrap(), "healthy");
            // Return to the broken node to verify consecutive failures across
            // separate attempts; the first failure must already reselect.
            match &session {
                Some(session) => session.rotate(),
                None => pool.rotate(),
            }
        }
    }
}

#[tokio::test]
async fn local_request_errors_do_not_quarantine_or_reselect_the_proxy() {
    let first = MockProxy::responding("first", 200).await;
    let second = MockProxy::responding("second", 200).await;
    let pool = ProxyPool::builder()
        .nodes([first.node.clone(), second.node.clone()])
        .rotation(RotationPolicy::sticky())
        .health(HealthPolicy {
            failure_threshold: NonZeroU32::new(1).unwrap(),
            ..HealthPolicy::default()
        })
        .build()
        .await
        .unwrap();
    let unsupported_scheme = Request::new(Method::GET, "ftp://127.0.0.1/file".parse().unwrap());
    let mut unsupported_version = request(Method::GET);
    *unsupported_version.version_mut() = Version::HTTP_09;

    for request in [unsupported_scheme, unsupported_version] {
        assert!(matches!(
            pool.execute(request).await,
            Err(Error::Transport(_))
        ));
        assert_eq!(pool.stats().eligible, 2);
        assert_eq!(pool.stats().in_flight, 0);
        assert_eq!(pool.acquire().unwrap().node().name(), "first");
    }
    assert!(first.requests().is_empty());
    assert!(second.requests().is_empty());
}

#[tokio::test]
async fn a_failing_request_body_does_not_quarantine_or_reselect_the_proxy() {
    let first = MockProxy::timing_out("first").await;
    let second = MockProxy::responding("second", 200).await;
    let pool = ProxyPool::builder()
        .nodes([first.node.clone(), second.node.clone()])
        .rotation(RotationPolicy::sticky())
        .health(HealthPolicy {
            failure_threshold: NonZeroU32::new(1).unwrap(),
            ..HealthPolicy::default()
        })
        .request_timeout(Duration::from_secs(2))
        .build()
        .await
        .unwrap();
    for kind in [
        std::io::ErrorKind::ConnectionReset,
        std::io::ErrorKind::TimedOut,
    ] {
        let mut request = request(Method::POST);
        *request.body_mut() = Some(Body::wrap_stream(futures_util::stream::once(async move {
            Err::<Vec<u8>, _>(std::io::Error::new(kind, "caller-provided body failed"))
        })));

        let result = pool.execute(request).await;
        assert!(matches!(result, Err(Error::Transport(error)) if error.is_request()));
        assert_eq!(pool.stats().eligible, 2);
        assert_eq!(pool.stats().in_flight, 0);
        assert_eq!(pool.acquire().unwrap().node().name(), "first");
    }
    assert!(second.requests().is_empty());
}

#[tokio::test]
async fn failover_exhausts_distinct_nodes_without_retrying_a_failed_node() {
    let first = MockProxy::timing_out("first").await;
    let second = MockProxy::timing_out("second").await;
    let pool = ProxyPool::builder()
        .nodes([first.node.clone(), second.node.clone()])
        .health(health())
        .request_timeout(Duration::from_millis(300))
        .build()
        .await
        .unwrap();

    let result = pool
        .execute_with_failover(request(Method::GET), NonZeroUsize::new(5).unwrap())
        .await;
    assert!(matches!(result, Err(Error::Transport(error)) if error.is_timeout()));
    assert_eq!(first.requests().len(), 1);
    assert_eq!(second.requests().len(), 1);
    assert_eq!(pool.stats().eligible, 2);
}

#[tokio::test]
async fn post_failover_is_rejected_before_any_request_or_quota_is_consumed() {
    let first = MockProxy::responding("first", 200).await;
    let second = MockProxy::responding("second", 200).await;
    let pool = pool([first.node.clone(), second.node.clone()], 1).await;
    let result = pool
        .execute_with_failover(request(Method::POST), NonZeroUsize::new(2).unwrap())
        .await;

    assert!(matches!(result, Err(Error::Config(_))));
    assert!(first.requests().is_empty());
    assert!(second.requests().is_empty());
    assert_eq!(pool.acquire().unwrap().node().name(), "first");
}

#[tokio::test]
async fn upstream_http_errors_do_not_trigger_failover_or_quarantine() {
    for status in [404, 503] {
        let first = MockProxy::responding("first", status).await;
        let second = MockProxy::responding("second", 200).await;
        let pool = ProxyPool::builder()
            .nodes([first.node.clone(), second.node.clone()])
            .rotation(RotationPolicy::every(20.try_into().unwrap()))
            .health(HealthPolicy {
                failure_threshold: NonZeroU32::new(1).unwrap(),
                ..health()
            })
            .build()
            .await
            .unwrap();

        for _ in 0..2 {
            let response = pool
                .execute_with_failover(request(Method::GET), NonZeroUsize::new(2).unwrap())
                .await
                .unwrap();
            assert_eq!(response.status().as_u16(), status);
            assert_eq!(response.text().await.unwrap(), "first");
        }
        assert_eq!(pool.stats().eligible, 2);
        assert_eq!(first.requests().len(), 2);
        assert!(second.requests().is_empty());
    }
}

#[tokio::test]
async fn proxy_authentication_failure_quarantines_and_rotates_without_replaying_response() {
    let first = MockProxy::responding("first", 407).await;
    let second = MockProxy::responding("second", 200).await;
    let pool = ProxyPool::builder()
        .nodes([first.node.clone(), second.node.clone()])
        .health(HealthPolicy {
            failure_threshold: NonZeroU32::new(1).unwrap(),
            ..health()
        })
        .build()
        .await
        .unwrap();

    let response = pool
        .execute_with_failover(request(Method::GET), NonZeroUsize::new(2).unwrap())
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::PROXY_AUTHENTICATION_REQUIRED);
    assert_eq!(pool.stats().eligible, 1);
    assert!(second.requests().is_empty());
    assert_eq!(
        pool.execute(request(Method::GET))
            .await
            .unwrap()
            .text()
            .await
            .unwrap(),
        "second"
    );
}

#[tokio::test]
async fn no_eligible_proxy_returns_an_error_without_connecting_directly() {
    let proxy = MockProxy::responding("proxy", 200).await;
    let origin = MockProxy::responding("direct connection must not happen", 200).await;
    let pool = ProxyPool::builder()
        .nodes([proxy.node.clone()])
        .health(HealthPolicy {
            failure_threshold: NonZeroU32::new(1).unwrap(),
            ..health()
        })
        .build()
        .await
        .unwrap();
    pool.acquire().unwrap().report_failure();
    assert_eq!(pool.stats().eligible, 0);

    let request = Request::new(Method::GET, origin.node.url().parse().unwrap());
    assert!(matches!(
        pool.execute(request).await,
        Err(Error::NoProxyAvailable)
    ));
    assert!(proxy.requests().is_empty());
    assert!(origin.requests().is_empty());
}

#[tokio::test]
async fn an_empty_pool_is_rejected_instead_of_enabling_direct_connections() {
    let result = ProxyPool::builder().health(health()).build().await;
    assert!(matches!(result, Err(Error::NoProxyAvailable)));
}