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
use std::{
    num::{NonZeroU32, NonZeroU64, NonZeroUsize},
    sync::{
        Arc, Mutex,
        atomic::{AtomicUsize, Ordering},
    },
    time::{Duration, Instant, SystemTime},
};

use futures_util::future::join_all;
use subscription_proxy_pool::{
    CachePolicy, HealthPolicy, PoolBuilder, ProxyPool, RotationPolicy, SourceOutcome,
    SubscriptionSource,
};
use tokio::{
    io::{AsyncReadExt, AsyncWriteExt},
    net::{TcpListener, TcpStream},
    sync::Semaphore,
    task::{JoinHandle, JoinSet},
};

const FIRST: &str = "http://127.0.0.1:18001#first\nhttp://127.0.0.1:18002#second";

async fn read_request(socket: &mut TcpStream) -> Option<String> {
    let mut request = Vec::new();
    let mut buffer = [0; 1024];
    loop {
        let count = socket.read(&mut buffer).await.ok()?;
        if count == 0 {
            return None;
        }
        request.extend_from_slice(&buffer[..count]);
        if request.windows(4).any(|part| part == b"\r\n\r\n") {
            return Some(String::from_utf8(request).unwrap().to_ascii_lowercase());
        }
        assert!(request.len() < 16 * 1024);
    }
}

struct Server {
    source: SubscriptionSource,
    response: Arc<Mutex<String>>,
    gate: Arc<Mutex<Option<Arc<Semaphore>>>>,
    hits: Arc<AtomicUsize>,
    requests: Arc<Mutex<Vec<String>>>,
    task: JoinHandle<()>,
}

impl Server {
    async fn new(body: &str) -> Self {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let response = Arc::new(Mutex::new(Self::wire(200, body, "")));
        let gate: Arc<Mutex<Option<Arc<Semaphore>>>> = Arc::new(Mutex::new(None));
        let hits = Arc::new(AtomicUsize::new(0));
        let requests = Arc::new(Mutex::new(Vec::new()));
        let task = {
            let response = Arc::clone(&response);
            let gate = Arc::clone(&gate);
            let hits = Arc::clone(&hits);
            let requests = Arc::clone(&requests);
            tokio::spawn(async move {
                loop {
                    let (mut socket, _) = listener.accept().await.unwrap();
                    let Some(request) = read_request(&mut socket).await else {
                        continue;
                    };
                    requests.lock().unwrap().push(request);
                    hits.fetch_add(1, Ordering::SeqCst);
                    let waiting = gate.lock().unwrap().clone();
                    if let Some(waiting) = waiting {
                        waiting.acquire().await.unwrap().forget();
                    }
                    let wire = response.lock().unwrap().clone();
                    let _ = socket.write_all(wire.as_bytes()).await;
                }
            })
        };
        Self {
            source: SubscriptionSource::new(&format!(
                "http://{address}/private-path?token=credential-secret"
            ))
            .unwrap(),
            response,
            gate,
            hits,
            requests,
            task,
        }
    }

    fn wire(status: u16, body: &str, headers: &str) -> String {
        format!(
            "HTTP/1.1 {status} Test\r\nContent-Length: {}\r\nConnection: close\r\n{headers}\r\n{body}",
            body.len()
        )
    }

    fn respond(&self, status: u16, body: &str, headers: &str) {
        *self.response.lock().unwrap() = Self::wire(status, body, headers);
    }

    fn hits(&self) -> usize {
        self.hits.load(Ordering::SeqCst)
    }

    async fn wait_for_hits(&self, count: usize) {
        tokio::time::timeout(Duration::from_secs(3), async {
            while self.hits() < count {
                tokio::time::sleep(Duration::from_millis(2)).await;
            }
        })
        .await
        .expect("local subscription request did not arrive");
    }
}

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

struct ProxyServer {
    endpoint: String,
    connections: Arc<AtomicUsize>,
    task: JoinHandle<()>,
}

impl ProxyServer {
    async fn new() -> Self {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let connections = Arc::new(AtomicUsize::new(0));
        let task = {
            let connections = Arc::clone(&connections);
            tokio::spawn(async move {
                let mut handlers = JoinSet::new();
                loop {
                    tokio::select! {
                        accepted = listener.accept() => {
                            let (mut socket, _) = accepted.unwrap();
                            connections.fetch_add(1, Ordering::SeqCst);
                            handlers.spawn(async move {
                                while read_request(&mut socket).await.is_some() {
                                    if socket.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok").await.is_err() {
                                        break;
                                    }
                                }
                            });
                        }
                        _ = handlers.join_next(), if !handlers.is_empty() => {}
                    }
                }
            })
        };
        Self {
            endpoint: format!("http://{address}"),
            connections,
            task,
        }
    }
}

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

fn builder(source: &SubscriptionSource) -> PoolBuilder {
    ProxyPool::builder()
        .subscription(source.clone())
        .health(HealthPolicy::default())
        .request_timeout(Duration::from_secs(3))
        .subscription_timeout(Duration::from_secs(3))
}

fn acquired_name(pool: &ProxyPool) -> String {
    pool.acquire().unwrap().node().name().to_owned()
}

#[tokio::test]
async fn not_modified_preserves_clients_quota_and_renews_the_persistent_cache() {
    let proxy = ProxyServer::new().await;
    let body = format!("{}#first\nhttp://127.0.0.1:1#second", proxy.endpoint);
    let server = Server::new(&body).await;
    server.respond(200, &body, "ETag: \"one\"\r\n");
    let directory = tempfile::tempdir().unwrap();
    let cache = CachePolicy {
        ttl: Duration::from_secs(30),
        max_stale: Duration::from_secs(600),
        ..CachePolicy::new(directory.path())
    };
    let pool = builder(&server.source)
        .cache(cache.clone())
        .rotation(RotationPolicy {
            requests_per_proxy: NonZeroU64::new(3),
            ..RotationPolicy::default()
        })
        .build()
        .await
        .unwrap();
    let ids: Vec<_> = pool.nodes().iter().map(|node| node.id()).collect();
    let mut first = pool.acquire().unwrap();
    assert_eq!(
        first
            .client()
            .get("http://127.0.0.1:9/resource")
            .send()
            .await
            .unwrap()
            .text()
            .await
            .unwrap(),
        "ok"
    );
    first.report_success();
    assert_eq!(proxy.connections.load(Ordering::SeqCst), 1);

    // Age only the cache fixture; the test need not sleep through a real TTL.
    let cache_path = directory
        .path()
        .join(format!("{}.json", server.source.key()));
    let mut saved: serde_json::Value =
        serde_json::from_slice(&std::fs::read(&cache_path).unwrap()).unwrap();
    let old_time = SystemTime::now() - Duration::from_secs(60);
    saved["saved_at"] = serde_json::to_value(old_time).unwrap();
    std::fs::write(&cache_path, serde_json::to_vec(&saved).unwrap()).unwrap();
    server.respond(304, "", "");
    let report = pool.refresh().await.unwrap();
    assert_eq!(report.not_modified_sources, 1);
    assert_eq!(report.updated_sources, 0);
    assert_eq!(report.sources[0].outcome, SourceOutcome::NotModified);
    assert_eq!(
        pool.nodes()
            .iter()
            .map(|node| node.id())
            .collect::<Vec<_>>(),
        ids
    );
    let mut second = pool.acquire().unwrap();
    assert_eq!(second.node().name(), "first");
    assert_eq!(
        second
            .client()
            .get("http://127.0.0.1:9/resource")
            .send()
            .await
            .unwrap()
            .text()
            .await
            .unwrap(),
        "ok"
    );
    second.report_success();
    assert_eq!(
        proxy.connections.load(Ordering::SeqCst),
        1,
        "304 must retain the client's reusable connection"
    );
    assert_eq!(acquired_name(&pool), "first");
    assert_eq!(
        acquired_name(&pool),
        "second",
        "304 must not reset the request quota"
    );
    assert!(server.requests.lock().unwrap()[1].contains("if-none-match: \"one\"\r\n"));

    let saved: serde_json::Value =
        serde_json::from_slice(&std::fs::read(&cache_path).unwrap()).unwrap();
    let renewed: SystemTime = serde_json::from_value(saved["saved_at"].clone()).unwrap();
    assert!(renewed > old_time + Duration::from_secs(30));
    let restarted = builder(&server.source).cache(cache).build().await.unwrap();
    assert_eq!(
        server.hits(),
        2,
        "revalidation must make startup cache fresh again"
    );
    assert_eq!(
        restarted.last_refresh_report().unwrap().sources[0].outcome,
        SourceOutcome::FreshCache
    );
}

#[tokio::test]
async fn reports_capture_startup_manual_refresh_and_sanitized_retained_failures() {
    let server = Server::new(FIRST).await;
    let pool = builder(&server.source).build().await.unwrap();
    let startup = pool.last_refresh_report().unwrap();
    assert_eq!(startup.updated_sources, 1);
    assert_eq!(startup.sources[0].source_key, server.source.key());
    assert_eq!(startup.sources[0].outcome, SourceOutcome::Updated);
    assert!(startup.sources[0].error.is_none());

    server.respond(200, "http://127.0.0.1:18003#new", "");
    pool.refresh().await.unwrap();
    assert_eq!(pool.last_refresh_report().unwrap().nodes, 1);
    for (status, body) in [
        (503, "credential-secret"),
        (200, "proxies: [credential-secret"),
    ] {
        server.respond(status, body, "");
        let report = pool.refresh().await.unwrap();
        assert_eq!(report.failed_sources, 1);
        assert_eq!(report.sources[0].outcome, SourceOutcome::Retained);
        assert!(report.sources[0].error.is_some());
        let stored = pool.last_refresh_report().unwrap();
        assert_eq!(stored.sources[0].outcome, SourceOutcome::Retained);
        assert_eq!(stored.sources[0].error, report.sources[0].error);
        let diagnostics = format!("{stored:?}");
        for secret in ["credential-secret", "private-path", "token="] {
            assert!(!diagnostics.contains(secret));
        }
        assert_eq!(acquired_name(&pool), "new");
    }
}

#[tokio::test]
async fn overlapping_refreshes_share_one_remote_request_wave() {
    let server = Server::new(FIRST).await;
    let pool = builder(&server.source).build().await.unwrap();
    let gate = Arc::new(Semaphore::new(0));
    *server.gate.lock().unwrap() = Some(Arc::clone(&gate));
    server.respond(200, "http://127.0.0.1:18003#new", "");
    let refreshes = join_all((0..24).map(|_| pool.refresh()));
    let release = async {
        server.wait_for_hits(2).await;
        gate.add_permits(24);
    };
    let (reports, ()) = tokio::join!(refreshes, release);
    for report in reports {
        let report = report.unwrap();
        assert_eq!(report.updated_sources, 1);
        assert_eq!(report.nodes, 1);
    }
    assert_eq!(
        server.hits(),
        2,
        "overlapping calls must reuse the completed refresh"
    );
    assert_eq!(acquired_name(&pool), "new");
}

#[tokio::test]
async fn maintenance_handles_share_a_loop_and_only_last_drop_stops_it() {
    let server = Server::new(FIRST).await;
    let period = Duration::from_millis(100);
    let pool = builder(&server.source)
        .refresh_interval(period)
        .build()
        .await
        .unwrap();
    let started = Instant::now();
    let first = pool.spawn_maintenance();
    tokio::time::sleep(Duration::from_millis(50)).await;
    let second = pool.spawn_maintenance();
    let cloned = second.clone();
    // Staggered starts expose duplicate loops even if simultaneous refreshes coalesce.
    tokio::time::sleep(Duration::from_millis(280)).await;
    let hits = server.hits();
    let elapsed_periods = started.elapsed().as_millis() / period.as_millis();
    assert!(hits >= 2);
    assert!(
        hits <= 1 + elapsed_periods as usize,
        "repeated spawn started another refresh loop"
    );
    drop(first);
    server.wait_for_hits(hits + 1).await;
    let hits = server.hits();
    drop(second);
    server.wait_for_hits(hits + 1).await;
    assert_eq!(
        pool.last_refresh_report().unwrap().sources[0].outcome,
        SourceOutcome::Updated
    );
    drop(cloned);
    tokio::time::sleep(Duration::from_millis(20)).await;
    let stopped_at = server.hits();
    tokio::time::sleep(period * 2).await;
    assert_eq!(
        server.hits(),
        stopped_at,
        "last handle drop must stop background requests"
    );
}

#[tokio::test]
async fn shutdown_stops_every_shared_handle_and_allows_a_fresh_restart() {
    let server = Server::new(FIRST).await;
    let period = Duration::from_millis(50);
    let pool = builder(&server.source)
        .refresh_interval(period)
        .build()
        .await
        .unwrap();
    let first = pool.spawn_maintenance();
    let second = pool.spawn_maintenance();
    let cloned = first.clone();
    server.wait_for_hits(2).await;
    first.shutdown().await;
    let stopped_at = server.hits();
    tokio::time::sleep(period * 3).await;
    assert_eq!(server.hits(), stopped_at);
    let restarted = pool.spawn_maintenance();
    server.wait_for_hits(stopped_at + 1).await;
    drop(second);
    drop(cloned);
    let hits = server.hits();
    server.wait_for_hits(hits + 1).await;
    restarted.shutdown().await;
}

#[tokio::test]
async fn leases_from_removed_generations_cannot_change_readded_endpoint_health_or_capacity() {
    for old_success in [false, true] {
        let server = Server::new(FIRST).await;
        let pool = builder(&server.source)
            .rotation(RotationPolicy {
                requests_per_proxy: NonZeroU64::new(10),
                ..RotationPolicy::default()
            })
            .health(HealthPolicy {
                failure_threshold: NonZeroU32::new(1).unwrap(),
                cooldown_jitter: 0.0,
                ..HealthPolicy::default()
            })
            .max_in_flight_per_proxy(NonZeroUsize::new(1).unwrap())
            .build()
            .await
            .unwrap();
        let mut old = pool.acquire().unwrap();
        assert_eq!(old.node().name(), "first");
        server.respond(200, "http://127.0.0.1:18002#second", "");
        pool.refresh().await.unwrap();
        server.respond(200, FIRST, "");
        pool.refresh().await.unwrap();
        let mut current = pool.acquire().unwrap();
        assert_eq!(current.node().name(), "first");
        assert_eq!(pool.stats().in_flight, 1);
        if old_success {
            current.report_failure();
            assert_eq!(pool.stats().eligible, 1);
            old.report_success();
            assert_eq!(
                pool.stats().eligible,
                1,
                "old success must not recover a new unhealthy entry"
            );
        } else {
            old.report_failure();
            assert_eq!(
                pool.stats().in_flight,
                1,
                "old completion must not release a new generation's slot"
            );
            current.report_success();
            assert_eq!(
                pool.stats().eligible,
                2,
                "old failure must not quarantine a new healthy entry"
            );
        }
    }
}