polyoxide-core 0.46.0

Core utilities and shared types for the polyoxide venue clients: the HTTP send loop and its hooks, window quotas, capacity buckets and holds, errors and macros
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
use std::time::Duration;

use mockito::Server;
use polyoxide_core::http::StatusCode;
use polyoxide_core::{ApiError, HttpClientBuilder, Request, RetryConfig};
use serde::Deserialize;

/// Simple response type for testing deserialization.
#[derive(Debug, Deserialize)]
struct TestResponse {
    value: String,
}

/// Error wrapper implementing RequestError for tests.
#[derive(Debug)]
struct TestError(ApiError);

impl From<ApiError> for TestError {
    fn from(e: ApiError) -> Self {
        Self(e)
    }
}

fn test_request(server: &mockito::ServerGuard, path: &str) -> Request<TestResponse, TestError> {
    let http = HttpClientBuilder::new(server.url()).build().unwrap();
    Request::new(http, path)
}

fn test_request_with_retry(
    server: &mockito::ServerGuard,
    path: &str,
    config: RetryConfig,
) -> Request<TestResponse, TestError> {
    let http = HttpClientBuilder::new(server.url())
        .with_retry_config(config)
        .build()
        .unwrap();
    Request::new(http, path)
}

#[tokio::test]
async fn retries_on_429_then_succeeds() {
    let mut server = Server::new_async().await;

    // mockito answers with the first matching mock still short of its expected hits, so the 429
    // mock goes first with `expect(1)`: it answers once, and the success mock, made second,
    // answers the retry.
    let retry_mock = server
        .mock("GET", "/retry-test")
        .with_status(429)
        .with_header("retry-after", "0")
        .expect(1)
        .create_async()
        .await;

    let success_mock = server
        .mock("GET", "/retry-test")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"value": "ok"}"#)
        .create_async()
        .await;

    let req = test_request(&server, "/retry-test");
    let resp = req.send().await.unwrap();
    assert_eq!(resp.value, "ok");

    retry_mock.assert_async().await;
    success_mock.assert_async().await;
}

#[tokio::test]
async fn exhausts_retries_returns_rate_limit_error() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/always-429")
        .with_status(429)
        .with_header("retry-after", "0")
        .with_body(r#"{"error": "slow down"}"#)
        .expect(2)
        .create_async()
        .await;

    let req = test_request_with_retry(
        &server,
        "/always-429",
        RetryConfig {
            max_retries: 1,
            initial_backoff_ms: 1,
            max_backoff_ms: 1,
        },
    );

    let err = req.send().await.unwrap_err();
    match err.0 {
        ApiError::Response(response) if response.status == StatusCode::TOO_MANY_REQUESTS => {
            assert_eq!(response.message, "slow down");
        }
        other => panic!("Expected a 429, got: {:?}", other),
    }

    mock.assert_async().await;
}

#[tokio::test]
async fn non_429_error_does_not_retry() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/server-error")
        .with_status(500)
        .with_body(r#"{"error": "internal error"}"#)
        .expect(1)
        .create_async()
        .await;

    let req = test_request(&server, "/server-error");
    let err = req.send().await.unwrap_err();

    match err.0 {
        ApiError::Response(response) => {
            assert_eq!(response.status, StatusCode::INTERNAL_SERVER_ERROR);
            assert_eq!(response.message, "internal error");
        }
        other => panic!("Expected Response error, got: {:?}", other),
    }

    mock.assert_async().await;
}

#[tokio::test]
async fn from_response_parses_json_error_field() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/bad-input")
        .with_status(400)
        .with_header("content-type", "application/json")
        .with_body(r#"{"error": "bad input"}"#)
        .create_async()
        .await;

    let req = test_request(&server, "/bad-input");
    let err = req.send().await.unwrap_err();

    match err.0 {
        ApiError::Response(response) if response.status == StatusCode::BAD_REQUEST => {
            assert_eq!(
                response.message, "bad input",
                "Should extract 'error' field from JSON, not raw body"
            );
        }
        other => panic!("Expected a 400, got: {:?}", other),
    }

    mock.assert_async().await;
}

#[tokio::test]
async fn error_401_returns_authentication_error() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/unauthorized")
        .with_status(401)
        .with_header("content-type", "application/json")
        .with_body(r#"{"error": "unauthorized"}"#)
        .expect(1)
        .create_async()
        .await;

    let req = test_request(&server, "/unauthorized");
    let err = req.send().await.unwrap_err();

    match err.0 {
        ApiError::Response(response) if response.status == StatusCode::UNAUTHORIZED => {
            assert_eq!(response.message, "unauthorized");
        }
        other => panic!("Expected a 401, got: {:?}", other),
    }

    mock.assert_async().await;
}

#[tokio::test]
async fn error_403_returns_authentication_error() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/forbidden")
        .with_status(403)
        .with_header("content-type", "application/json")
        .with_body(r#"{"error": "forbidden"}"#)
        .expect(1)
        .create_async()
        .await;

    let req = test_request(&server, "/forbidden");
    let err = req.send().await.unwrap_err();

    match err.0 {
        ApiError::Response(response) if response.status == StatusCode::FORBIDDEN => {
            assert_eq!(response.message, "forbidden");
        }
        other => panic!("Expected a 403, got: {:?}", other),
    }

    mock.assert_async().await;
}

#[tokio::test]
async fn error_408_returns_timeout_error() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/timeout")
        .with_status(408)
        .with_body(r#"{"error": "request timeout"}"#)
        .expect(1)
        .create_async()
        .await;

    let req = test_request(&server, "/timeout");
    let err = req.send().await.unwrap_err();

    match err.0 {
        ApiError::Response(response) if response.status == StatusCode::REQUEST_TIMEOUT => {}
        other => panic!("Expected a 408, got: {:?}", other),
    }

    mock.assert_async().await;
}

// ── Concurrency limiter integration ─────────────────────────────

#[tokio::test]
async fn send_raw_works_with_concurrency_limit() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/concurrent")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"value": "ok"}"#)
        .expect(2)
        .create_async()
        .await;

    let http = HttpClientBuilder::new(server.url())
        .with_max_concurrent(1)
        .build()
        .unwrap();

    // Two concurrent requests with concurrency=1 should both succeed
    let req1 = Request::<TestResponse, TestError>::new(http.clone(), "/concurrent");
    let req2 = Request::<TestResponse, TestError>::new(http, "/concurrent");

    let (r1, r2) = tokio::join!(req1.send(), req2.send());
    assert!(r1.is_ok());
    assert!(r2.is_ok());

    mock.assert_async().await;
}

#[tokio::test]
async fn retry_releases_permit_during_backoff() {
    let mut server = Server::new_async().await;

    // First call returns 429 with 1s retry-after, second returns 200
    let success_mock = server
        .mock("GET", "/retry-permit")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"value": "ok"}"#)
        .create_async()
        .await;

    let retry_mock = server
        .mock("GET", "/retry-permit")
        .with_status(429)
        .with_header("retry-after", "1")
        .expect_at_most(1)
        .create_async()
        .await;

    let http = HttpClientBuilder::new(server.url())
        .with_max_concurrent(1)
        .build()
        .unwrap();
    let http_clone = http.clone();

    // Spawn the retrying request
    let handle = tokio::spawn(async move {
        let req = Request::<TestResponse, TestError>::new(http, "/retry-permit");
        req.send().await.unwrap()
    });

    // Wait for first request to hit 429 and enter backoff
    tokio::time::sleep(Duration::from_millis(200)).await;

    // Should be able to acquire permit during backoff (permit was released)
    let result =
        tokio::time::timeout(Duration::from_millis(100), http_clone.acquire_concurrency()).await;
    assert!(result.is_ok(), "Should acquire permit during retry backoff");
    // Drop permit immediately so the retry can proceed
    drop(result);

    let resp = handle.await.unwrap();
    assert_eq!(resp.value, "ok");

    retry_mock.assert_async().await;
    success_mock.assert_async().await;
}

#[tokio::test]
async fn concurrency_limit_serializes_requests() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/serial")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"value": "ok"}"#)
        .expect(4)
        .create_async()
        .await;

    let http = HttpClientBuilder::new(server.url())
        .with_max_concurrent(2)
        .build()
        .unwrap();

    // 4 concurrent requests with concurrency=2 should all succeed
    let mut handles = Vec::new();
    for _ in 0..4 {
        let h = http.clone();
        handles.push(tokio::spawn(async move {
            Request::<TestResponse, TestError>::new(h, "/serial")
                .send()
                .await
                .unwrap()
        }));
    }

    for h in handles {
        let resp = h.await.unwrap();
        assert_eq!(resp.value, "ok");
    }

    mock.assert_async().await;
}

// ── Method, body, authenticator and costs ───────────────────────

/// What a test authenticator saw and stamped on each attempt.
#[derive(Clone, Default)]
struct Seen(std::sync::Arc<std::sync::Mutex<Vec<String>>>);

impl Seen {
    fn push(&self, entry: String) {
        self.0.lock().unwrap().push(entry);
    }

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

/// Stamps the attempt on the request and records the body it signs.
struct StampAttempt(Seen);

impl polyoxide_core::Authenticator for StampAttempt {
    async fn sign(
        &self,
        parts: &mut polyoxide_core::RequestParts,
        attempt: u32,
    ) -> Result<(), ApiError> {
        self.0.push(format!(
            "sign {attempt} {}",
            String::from_utf8_lossy(parts.body.as_deref().unwrap_or(b"-"))
        ));
        parts.headers.insert("x-attempt", attempt.into());
        Ok(())
    }
}

/// A mock, not yet created, answering `method path` with `statuses` in turn,
/// then the last of them for good, logging each request as
/// `send <x-attempt> <body>`.
fn scripted(
    server: &mut mockito::ServerGuard,
    method: &str,
    path: &str,
    statuses: &[usize],
    seen: &Seen,
) -> mockito::Mock {
    let statuses = statuses.to_vec();
    let served = std::sync::atomic::AtomicUsize::new(0);
    let seen = seen.clone();
    server
        .mock(method, path)
        .match_query(mockito::Matcher::Any)
        .with_status_code_from_request(move |request| {
            let stamp = request
                .header("x-attempt")
                .first()
                .and_then(|v| v.to_str().ok())
                .unwrap_or("-")
                .to_owned();
            let body = String::from_utf8_lossy(request.body().unwrap()).into_owned();
            seen.push(format!("send {stamp} {body}"));
            let n = served.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            statuses[n.min(statuses.len() - 1)]
        })
        .with_header("content-type", "application/json")
        .with_body(r#"{"value": "ok"}"#)
}

fn quick_retries() -> RetryConfig {
    RetryConfig {
        max_retries: 3,
        initial_backoff_ms: 1,
        max_backoff_ms: 5,
    }
}

#[tokio::test]
async fn a_post_sends_its_body_serialised_once() {
    #[derive(serde::Serialize)]
    struct Order {
        side: &'static str,
        price: &'static str,
    }

    let mut server = Server::new_async().await;
    let seen = Seen::default();
    // Through `serde_json::Value`, whose keys sort: `price` before `side`.
    let wire = r#"{"price":"0.5","side":"BUY"}"#;
    let mock = scripted(&mut server, "POST", "/order", &[429, 200], &seen)
        .match_header("content-type", "application/json")
        .match_body(wire)
        .expect(2)
        .create_async()
        .await;

    let http = HttpClientBuilder::new(server.url())
        .with_retry_config(quick_retries())
        .build()
        .unwrap();
    let auth = polyoxide_core::DynAuthenticator::new_arc(StampAttempt(seen.clone()));
    let resp = Request::<TestResponse, TestError>::new(http, "/order")
        .method(polyoxide_core::http::Method::POST)
        .authenticator(auth)
        .body(&Order {
            side: "BUY",
            price: "0.5",
        })
        .unwrap()
        .send()
        .await
        .unwrap();
    assert_eq!(resp.value, "ok");
    mock.assert_async().await;

    assert_eq!(
        seen.entries(),
        [
            format!("sign 0 {wire}"),
            format!("send 0 {wire}"),
            format!("sign 1 {wire}"),
            format!("send 1 {wire}"),
        ],
        "every attempt signs and sends the bytes serialised once"
    );
}

#[tokio::test]
async fn an_authenticator_signs_every_attempt_of_a_request() {
    let mut server = Server::new_async().await;
    let seen = Seen::default();
    let mock = scripted(&mut server, "GET", "/signed", &[429, 429, 200], &seen)
        .expect(3)
        .create_async()
        .await;

    let http = HttpClientBuilder::new(server.url())
        .with_retry_config(quick_retries())
        .build()
        .unwrap();
    let auth = polyoxide_core::DynAuthenticator::new_arc(StampAttempt(seen.clone()));
    let request = Request::<TestResponse, TestError>::new(http, "/signed").authenticator(auth);
    // A clone carries the authenticator, as a paginated walk's pages do.
    request.clone().send().await.unwrap();
    mock.assert_async().await;

    assert_eq!(
        seen.entries(),
        ["sign 0 -", "send 0 ", "sign 1 -", "send 1 ", "sign 2 -", "send 2 "],
        "each attempt is signed afresh, and the server sees that attempt's signature"
    );
}

/// A throttle that records the costs each attempt carries.
#[derive(Clone, Default)]
struct CostRecorder(std::sync::Arc<std::sync::Mutex<Vec<Vec<polyoxide_core::Cost>>>>);

impl polyoxide_core::Throttle for CostRecorder {
    async fn acquire(
        &self,
        meta: &polyoxide_core::RequestMeta<'_>,
    ) -> Result<polyoxide_core::Charge, polyoxide_core::Refused> {
        self.0.lock().unwrap().push(meta.costs.to_vec());
        Ok(polyoxide_core::Charge::none())
    }

    fn observe(
        &self,
        _charge: &polyoxide_core::Charge,
        _response: &polyoxide_core::ResponseMeta<'_>,
        _attempt: &polyoxide_core::AttemptInfo,
    ) {
    }

    fn hold(&self, _delay: Duration) {}
}

#[tokio::test]
async fn a_request_s_costs_reach_the_throttle() {
    let mut server = Server::new_async().await;
    let seen = Seen::default();
    let mock = scripted(&mut server, "DELETE", "/orders", &[429, 200], &seen)
        .expect(2)
        .create_async()
        .await;

    let throttle = CostRecorder::default();
    let http = HttpClientBuilder::new(server.url())
        .with_throttle(throttle.clone())
        .with_retry_config(quick_retries())
        .build()
        .unwrap();
    let write = polyoxide_core::Cost {
        layer: polyoxide_core::LayerId("write"),
        units: 7,
        exact: true,
    };
    let read = polyoxide_core::Cost {
        layer: polyoxide_core::LayerId("read"),
        units: 1,
        exact: false,
    };
    Request::<TestResponse, TestError>::new(http, "/orders")
        .method(polyoxide_core::http::Method::DELETE)
        .with_cost(write)
        .with_cost(read)
        .send()
        .await
        .unwrap();
    mock.assert_async().await;

    assert_eq!(
        *throttle.0.lock().unwrap(),
        [vec![write, read], vec![write, read]],
        "every attempt is charged the request's costs, in order"
    );
}

#[tokio::test]
async fn a_request_parts_timeout_bounds_its_attempt() {
    // A listener that never accepts: the connection opens and no answer comes.
    let silent = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let http = HttpClientBuilder::new(format!("http://{}", silent.local_addr().unwrap()))
        .build()
        .unwrap();
    let mut parts = polyoxide_core::RequestParts::new(polyoxide_core::http::Method::GET, "/slow");
    parts.timeout = Some(Duration::from_millis(200));

    let start = std::time::Instant::now();
    let err = http.send(parts, &[], None).await.unwrap_err();
    let elapsed = start.elapsed();
    assert!(
        matches!(&err, ApiError::Transport(e) if e.kind() == polyoxide_core::TransportKind::Timeout),
        "{err:?}"
    );
    assert!(
        elapsed >= Duration::from_millis(150) && elapsed < Duration::from_secs(5),
        "the attempt ended after {elapsed:?}, not at its own 200ms timeout \
         (the client's is 30s)"
    );
}

// Why: issue #87. A gzip upload's body is not text, and the send loop carried
// only text until `RequestParts::body` became bytes.
#[tokio::test]
async fn a_body_that_is_not_utf8_reaches_the_server_byte_for_byte() {
    // A gzip member's magic and method, then bytes no UTF-8 text holds.
    let body: &'static [u8] = &[0x1f, 0x8b, 0x08, 0x00, 0xff, 0xfe, 0x00, 0x80];
    let mut server = Server::new_async().await;
    let mock = server
        .mock("POST", "/upload")
        .match_header("content-type", "application/gzip")
        .match_body(body.to_vec())
        .with_status(200)
        .expect(1)
        .create_async()
        .await;
    let http = HttpClientBuilder::new(server.url()).build().unwrap();
    let mut parts =
        polyoxide_core::RequestParts::new(polyoxide_core::http::Method::POST, "/upload");
    parts.headers.insert(
        polyoxide_core::http::header::CONTENT_TYPE,
        polyoxide_core::http::HeaderValue::from_static("application/gzip"),
    );
    parts.body = Some(bytes::Bytes::from_static(body));

    let response = http.send(parts, &[], None).await.unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    mock.assert_async().await;
}

#[tokio::test]
async fn a_request_parts_timeout_outlasts_the_client_s() {
    // A venue's session-signer posts wait 300s on a client whose own timeout is
    // 30s: the request's timeout replaces the client's, longer as well as
    // shorter. A server that answers after 400ms, a client that gives up
    // after 100ms, and a request allowed 5s.
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    tokio::spawn(async move {
        use tokio::io::{AsyncReadExt, AsyncWriteExt};
        let (mut socket, _) = listener.accept().await.unwrap();
        let mut request = [0u8; 1024];
        let _ = socket.read(&mut request).await.unwrap();
        tokio::time::sleep(Duration::from_millis(400)).await;
        socket
            .write_all(b"HTTP/1.1 200 OK\r\ncontent-length: 0\r\nconnection: close\r\n\r\n")
            .await
            .unwrap();
    });
    let http = HttpClientBuilder::new(format!("http://{addr}"))
        .timeout_ms(100)
        .build()
        .unwrap();
    let mut parts = polyoxide_core::RequestParts::new(polyoxide_core::http::Method::GET, "/slow");
    parts.timeout = Some(Duration::from_secs(5));

    let response = http.send(parts, &[], None).await.unwrap();
    assert_eq!(response.status(), polyoxide_core::http::StatusCode::OK);
}