hyper-client-pool 0.6.0

Pooled Hyper Async Clients
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#[macro_use]
extern crate lazy_static;

extern crate hyper_client_pool;
extern crate ipnet;
extern crate regex;
extern crate tracing_subscriber;

use futures::{channel::mpsc, prelude::*};
use std::net::IpAddr;
use std::process::Command;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::sync::RwLock;
use std::time::{Duration, Instant};

use hyper::{Body, Request};
use hyper_client_pool::*;
use ipnet::{Contains, IpNet};
use regex::Regex;

lazy_static! {
    /// For tests that depend on global state (ahem - keep_alive_works_as_expected())
    /// we have this test_lock which they can grab with a `write()` in order to ensure
    /// no other tests in this file are running
    static ref TEST_LOCK: RwLock<()> = RwLock::new(());
}

#[derive(Debug)]
struct MspcDeliverable(mpsc::UnboundedSender<DeliveryResult>);

impl Deliverable for MspcDeliverable {
    fn complete(self, result: DeliveryResult) {
        let _ = self.0.unbounded_send(result);
    }
}

fn default_config() -> Config {
    Config {
        keep_alive_timeout: Duration::from_secs(3),
        transaction_timeout: Duration::from_secs(2),
        dns_threads_per_worker: 1,
        max_transactions_per_worker: 1_000,
        workers: 2,
    }
}

fn onesignal_transaction<D: Deliverable>(deliverable: D) -> Transaction<D> {
    Transaction::new(
        deliverable,
        Request::get("https://onesignal.com/")
            .body(Body::empty())
            .unwrap(),
        false,
    )
}

fn httpbin_transaction<D: Deliverable>(deliverable: D) -> Transaction<D> {
    Transaction::new(
        deliverable,
        Request::get("http://httpbin:80/ip")
            .body(Body::empty())
            .unwrap(),
        false,
    )
}

fn check_successful_result(result: DeliveryResult) -> (bool, DeliveryResult) {
    let successful = match result {
        DeliveryResult::Response { ref response, .. } => response.status().is_success(),
        _ => false,
    };
    (successful, result)
}

fn assert_successful_result(result: DeliveryResult) {
    let (successful, result) = check_successful_result(result);
    assert_eq!(true, successful, "Not successful result: {:?}!", result);
}

#[tokio::test]
async fn some_gets_single_worker() {
    let _read = TEST_LOCK.read().unwrap_or_else(|e| e.into_inner());

    let _ = tracing_subscriber::fmt::try_init();

    let mut config = default_config();
    config.workers = 1;

    let mut pool = Pool::builder(config).build().unwrap();
    let (tx, mut rx) = mpsc::unbounded();

    for _ in 0..5 {
        pool.request(onesignal_transaction(MspcDeliverable(tx.clone())))
            .expect("request ok");
    }

    for _ in 0..5 {
        assert_successful_result(rx.next().await.unwrap());
    }

    pool.shutdown().await;
}

#[tokio::test]
async fn ton_of_gets() {
    const REQUEST_AMOUNT: usize = 600;
    let _read = TEST_LOCK.read().unwrap_or_else(|e| e.into_inner());

    let _ = tracing_subscriber::fmt::try_init();

    let mut config = default_config();
    config.dns_threads_per_worker = 4;
    config.workers = 4;
    config.max_transactions_per_worker = 1_000;
    config.transaction_timeout = Duration::from_secs(60);

    let mut pool = Pool::builder(config).build().unwrap();
    let (tx, mut rx) = mpsc::unbounded();

    for _ in 0..REQUEST_AMOUNT {
        pool.request(httpbin_transaction(MspcDeliverable(tx.clone())))
            .expect("request ok");
    }

    let mut successes = 0i32;
    let mut not_successes = 0i32;
    for _ in 0..REQUEST_AMOUNT {
        let (successful, _result) = check_successful_result(rx.next().await.unwrap());
        if successful {
            successes += 1;
        } else {
            not_successes += 1;
        }
    }

    let expected_successes = ((REQUEST_AMOUNT as f32) * 0.9) as _;
    assert!(
        successes > expected_successes,
        "expected at least {} successes, found {}",
        expected_successes,
        successes,
    );

    let expected_failures = ((REQUEST_AMOUNT as f32) * 0.1) as _;
    assert!(
        not_successes < expected_failures,
        "expected at most {} failures, found {}",
        expected_failures,
        not_successes
    );

    pool.shutdown().await;
    println!(
        "Successes: {} | Not Successes: {}",
        successes, not_successes
    );
}

#[derive(Debug, Clone)]
struct SuccessfulCompletionCounter {
    count: Arc<AtomicUsize>,
}

impl SuccessfulCompletionCounter {
    fn new() -> SuccessfulCompletionCounter {
        SuccessfulCompletionCounter {
            count: Arc::new(AtomicUsize::new(0)),
        }
    }

    fn count(&self) -> usize {
        self.count.load(Ordering::Acquire)
    }
}

impl Deliverable for SuccessfulCompletionCounter {
    fn complete(self, result: DeliveryResult) {
        assert_successful_result(result);
        self.count.fetch_add(1, Ordering::AcqRel);
    }
}

#[tokio::test]
async fn graceful_shutdown() {
    let _read = TEST_LOCK.read().unwrap_or_else(|e| e.into_inner());

    let _ = tracing_subscriber::fmt::try_init();

    let txn = 20;
    let counter = SuccessfulCompletionCounter::new();

    let mut config = default_config();
    config.workers = 2;

    let mut pool = Pool::builder(config).build().unwrap();
    for _ in 0..txn {
        pool.request(onesignal_transaction(counter.clone()))
            .expect("request ok");
    }

    pool.shutdown().await;
    assert_eq!(counter.count(), txn);
}

#[tokio::test]
async fn full_error() {
    let _read = TEST_LOCK.read().unwrap_or_else(|e| e.into_inner());

    let _ = tracing_subscriber::fmt::try_init();

    let mut config = default_config();
    config.workers = 3;
    config.max_transactions_per_worker = 1;

    let mut pool = Pool::builder(config).build().unwrap();
    let (tx, mut rx) = mpsc::unbounded();

    // Start requests
    for _ in 0..3 {
        pool.request(onesignal_transaction(MspcDeliverable(tx.clone())))
            .expect("request ok");
    }

    match pool.request(onesignal_transaction(MspcDeliverable(tx.clone()))) {
        Err(err) => assert_eq!(err.kind, ErrorKind::PoolFull),
        _ => panic!("Expected Error, got success request!"),
    }

    for _ in 0..3 {
        assert_successful_result(rx.next().await.unwrap());
    }

    pool.shutdown().await;
}

static CLOUDFLARE_NETS: &[&str] = &[
    // IPv4
    "103.21.244.0/22",
    "103.22.200.0/22",
    "103.31.4.0/22",
    "104.16.0.0/12",
    "108.162.192.0/18",
    "131.0.72.0/22",
    "141.101.64.0/18",
    "162.158.0.0/15",
    "172.64.0.0/13",
    "173.245.48.0/20",
    "188.114.96.0/20",
    "190.93.240.0/20",
    "197.234.240.0/22",
    "198.41.128.0/17",
    // IPv6
    "2400:cb00::/32",
    "2405:8100::/32",
    "2405:b500::/32",
    "2606:4700::/32",
    "2803:f800::/32",
    "2c0f:f248::/32",
    "2a06:98c0::/29",
];

lazy_static! {
    static ref CLOUDFLARE_PARSED_NETS: Vec<IpNet> = {
        CLOUDFLARE_NETS
            .iter()
            .map(|net| net.parse::<IpNet>())
            .collect::<Result<Vec<IpNet>, _>>()
            .unwrap()
    };
    static ref LSOF_PARSE_IP_REGEX: Regex = Regex::new(r"->\[?([^\]]*)\]?:https").unwrap();
}

fn matches_cloudflare_ip(input: &str) -> bool {
    if let Some(captures) = LSOF_PARSE_IP_REGEX.captures(input) {
        match captures[1].parse::<IpAddr>() {
            Ok(addr) => CLOUDFLARE_PARSED_NETS.iter().any(|net| net.contains(&addr)),
            Err(_err) => false,
        }
    } else {
        false
    }
}

#[test]
fn matches_cloudflare_ip_works_as_expected() {
    // Test-case from staging
    let input1 = "hyper_cli 29606 deploy    9u  IPv6 74567336      0t0  TCP onepush-test-darren:46286->[2400:cb00:2048:1::6810:cea5]:https (ESTABLISHED)";
    assert_eq!(matches_cloudflare_ip(input1), true);
    // test-case from personal mac
    let input2 = "lib-f13ca 83600 darrentsung   12u  IPv4 0x2aad2644e2239ff9      0t0  TCP 192.168.2.240:54285->104.16.207.165:https (ESTABLISHED)";
    assert_eq!(matches_cloudflare_ip(input2), true);
}

fn onesignal_connection_count() -> (usize, String) {
    let output = Command::new("lsof")
        .args(&["-i"])
        .output()
        .expect("command works");

    let stdout = String::from_utf8(output.stdout).unwrap();
    let matching_lines = stdout
        .split("\n")
        .filter(|line| matches_cloudflare_ip(line))
        .count();

    (matching_lines, stdout)
}

macro_rules! assert_onesignal_connection_open_count_eq {
    ($expected_open_count:expr) => {
        let (open_count, stdout) = onesignal_connection_count();
        assert_eq!($expected_open_count, open_count, "Output:\n{}", stdout);
    };
}

#[tokio::test]
async fn keep_alive_works_as_expected() {
    let _write = TEST_LOCK.write().unwrap_or_else(|e| e.into_inner());

    // block until no connections are open - this is unfortunate..
    // but at least we have tests covering the keep-alive :)
    let count = onesignal_connection_count().0;
    while count > 0 {
        eprintln!("keep_alive_works_as_expected blocking - open connections to cloudflare: {} must be 0 before test can run", count);
        tokio::time::sleep(Duration::from_secs(1)).await;
    }

    let _ = tracing_subscriber::fmt::try_init();

    let mut config = default_config();
    config.keep_alive_timeout = Duration::from_secs(3);

    let mut pool = Pool::builder(config).build().unwrap();
    let (tx, mut rx) = mpsc::unbounded();

    // Start first request
    pool.request(onesignal_transaction(MspcDeliverable(tx.clone())))
        .expect("request ok");

    // wait for request to finish
    assert_successful_result(rx.next().await.unwrap());
    let start = Instant::now();
    loop {
        let seconds_elapsed = start.elapsed().as_secs();
        if seconds_elapsed < 2 {
            assert_onesignal_connection_open_count_eq!(1);
        } else if seconds_elapsed > 3 {
            // keep-alive should kill connection by now
            assert_onesignal_connection_open_count_eq!(0);
            break;
        }
        tokio::time::sleep(Duration::from_millis(100)).await;
    }

    pool.shutdown().await;
}

#[tokio::test]
async fn connection_reuse_works_as_expected() {
    let _write = TEST_LOCK.write().unwrap_or_else(|e| e.into_inner());

    // block until no connections are open - this is unfortunate..
    // but at least we have tests covering the keep-alive :)
    let count = onesignal_connection_count().0;
    while count > 0 {
        eprintln!("connection_reuse_works_as_expected blocking - open connections to cloudflare: {} must be 0 before test can run", count);
        tokio::time::sleep(Duration::from_secs(1)).await;
    }

    let _ = tracing_subscriber::fmt::try_init();

    let mut config = default_config();
    // note that workers must be one otherwise the second transaction will be
    // routed to another worker and a new connection will be established
    config.workers = 1;
    config.keep_alive_timeout = Duration::from_secs(10);

    let mut pool = Pool::builder(config).build().unwrap();
    let (tx, mut rx) = mpsc::unbounded();

    // Start first request
    pool.request(onesignal_transaction(MspcDeliverable(tx.clone())))
        .expect("request ok");
    // wait for request to finish
    assert_successful_result(rx.next().await.unwrap());

    assert_onesignal_connection_open_count_eq!(1);
    tokio::time::sleep(Duration::from_secs(3)).await;
    assert_onesignal_connection_open_count_eq!(1);

    // Start second request
    pool.request(onesignal_transaction(MspcDeliverable(tx.clone())))
        .expect("request ok");
    // wait for request to finish
    assert_successful_result(rx.next().await.unwrap());

    // there should only be one connection open
    assert_onesignal_connection_open_count_eq!(1);

    pool.shutdown().await;
}

#[tokio::test]
async fn timeout_works_as_expected() {
    let _read = TEST_LOCK.read().unwrap_or_else(|e| e.into_inner());

    let _ = tracing_subscriber::fmt::try_init();

    let mut config = default_config();
    config.transaction_timeout = Duration::from_secs(2);

    let mut pool = Pool::builder(config).build().unwrap();
    let (tx, mut rx) = mpsc::unbounded();

    // Start first request
    pool.request(
        // This endpoint will not return for a while, therefore should timeout
        Transaction::new(
            MspcDeliverable(tx.clone()),
            Request::get("https://httpstat.us/200?sleep=5000")
                .body(Body::empty())
                .unwrap(),
            false,
        ),
    )
    .expect("request ok");

    match rx.next().await.unwrap() {
        DeliveryResult::Timeout { .. } => (), // ok
        res => panic!("Expected timeout!, got: {:?}", res),
    }

    pool.shutdown().await;
}

#[tokio::test]
async fn transaction_counting_works() {
    let _read = TEST_LOCK.read().unwrap_or_else(|e| e.into_inner());

    let _ = tracing_subscriber::fmt::try_init();

    let mut config = default_config();
    config.workers = 3;

    let transaction_counters = Arc::new(RwLock::new(Vec::new()));

    let mut pool = Pool::builder(config)
        .transaction_counters(transaction_counters.clone())
        .build()
        .unwrap();
    let (tx, mut rx) = mpsc::unbounded();

    // Start requests
    for _ in 0..3 {
        pool.request(onesignal_transaction(MspcDeliverable(tx.clone())))
            .expect("request ok");
    }

    let mut saw_transactions = false;
    let mut received = 0i32;
    loop {
        let counters = transaction_counters.try_read().unwrap();
        for counter in counters.iter() {
            assert_eq!(counter.is_valid(), true);
            let transaction_count = counter.count();
            if transaction_count == 1 {
                saw_transactions = true;
            }
            assert!(transaction_count <= 1);
        }

        if let Ok(Some(recv)) = rx.try_next() {
            assert_successful_result(recv);
            received += 1;

            if received == 3 {
                break;
            }
        }

        tokio::time::sleep(Duration::from_millis(250)).await;
    }

    // Make sure that we saw the counter was doing something
    assert!(saw_transactions);

    pool.shutdown().await;

    // Counters should not be valid anymore
    let counters = transaction_counters.try_read().unwrap();
    for counter in counters.iter() {
        assert_eq!(counter.is_valid(), false);
    }
}