hyperlane-performance 22.0.10

Reproducible multi-protocol performance tools for Hyperlane.
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
use super::*;

/// Builds the client configuration the secure protocols negotiate with.
///
/// # Arguments
///
/// - `&str` - The path of the certificate authority.
/// - `&[u8]` - The ALPN identifier to advertise.
///
/// # Returns
///
/// - `Result<ClientConfig, String>` - The configuration, or the reason it could
///   not be built.
pub(crate) fn build_client_config(ca_path: &str, alpn: &[u8]) -> Result<ClientConfig, String> {
    let mut roots: RootCertStore = RootCertStore::empty();
    let certificates: ReadIter<BufReader<File>, CertificateDer<'static>> =
        match CertificateDer::pem_file_iter(ca_path) {
            Ok(certificates) => certificates,
            Err(error) => return Err(format!("{ERROR_CA_UNREADABLE}: {error}")),
        };
    for certificate in certificates {
        let certificate: CertificateDer<'static> = match certificate {
            Ok(certificate) => certificate,
            Err(error) => return Err(format!("{ERROR_CA_UNREADABLE}: {error}")),
        };
        if let Err(error) = roots.add(certificate) {
            return Err(format!("{ERROR_CA_UNREADABLE}: {error}"));
        }
    }
    let versions: &[&'static SupportedProtocolVersion] = &[&TLS13, &TLS12];
    let mut config: ClientConfig =
        match ClientConfig::builder_with_provider(Arc::new(default_provider()))
            .with_protocol_versions(versions)
        {
            Ok(builder) => builder.with_root_certificates(roots).with_no_client_auth(),
            Err(error) => return Err(error.to_string()),
        };
    config.alpn_protocols = vec![alpn.to_vec()];
    Ok(config)
}

/// Builds the QUIC client configuration the HTTP/3 variant negotiates with.
///
/// # Arguments
///
/// - `&str` - The path of the certificate authority.
///
/// # Returns
///
/// - `Result<QuicClientConfig, String>` - The configuration, or the reason it
///   could not be built.
pub(crate) fn build_quic_config(ca_path: &str) -> Result<QuicClientConfig, String> {
    let crypto: QuicCryptoConfig =
        match QuicCryptoConfig::try_from(build_client_config(ca_path, ALPN_HTTP3)?) {
            Ok(crypto) => crypto,
            Err(error) => return Err(error.to_string()),
        };
    Ok(QuicClientConfig::new(Arc::new(crypto)))
}

/// Opens a plain socket to the server under test.
///
/// # Arguments
///
/// - `&str` - The address of the server.
///
/// # Returns
///
/// - `Result<TcpStream, String>` - The socket, or the reason it could not be
///   opened.
pub(crate) async fn open_plain(address: &str) -> Result<TcpStream, String> {
    let stream: TcpStream = match TcpStream::connect(address).await {
        Ok(stream) => stream,
        Err(error) => return Err(error.to_string()),
    };
    let _: Result<(), Error> = stream.set_nodelay(true);
    Ok(stream)
}

/// Opens a TLS session to the server under test.
///
/// # Arguments
///
/// - `&str` - The address of the server.
/// - `&str` - The path of the certificate authority.
/// - `&[u8]` - The ALPN identifier to advertise.
///
/// # Returns
///
/// - `Result<TlsStream<TcpStream>, String>` - The session,
///   or the reason the handshake failed.
pub(crate) async fn open_tls(
    address: &str,
    ca_path: &str,
    alpn: &[u8],
) -> Result<TlsStream<TcpStream>, String> {
    let config: ClientConfig = build_client_config(ca_path, alpn)?;
    let connector: TlsConnector = TlsConnector::from(Arc::new(config));
    let stream: TcpStream = open_plain(address).await?;
    let server_name: ServerName<'static> = ServerName::try_from(VERIFY_NAME)
        .map_err(|error: InvalidDnsNameError| error.to_string())?;
    let session: TlsStream<TcpStream> = match connector.connect(server_name, stream).await {
        Ok(session) => session,
        Err(error) => return Err(error.to_string()),
    };
    Ok(session)
}

/// Opens the transport one protocol variant writes its bytes over.
///
/// # Arguments
///
/// - `&str` - The address of the server.
/// - `Protocol` - The protocol the transport speaks.
/// - `Option<&str>` - The path of the certificate authority.
///
/// # Returns
///
/// - `Result<Box<dyn Transport>, String>` - The transport, or the reason it could
///   not be opened.
pub(crate) async fn open_transport(
    address: &str,
    protocol: Protocol,
    ca_path: Option<&str>,
) -> Result<Box<dyn Transport>, String> {
    let resolved: String = resolve_address(address).await?.to_string();
    let address: &str = resolved.as_str();
    match protocol {
        Protocol::Http1 => {
            let stream: TcpStream = open_plain(address).await?;
            Ok(Box::new(stream))
        }
        Protocol::Http1Tls => {
            let ca_path: &str = match ca_path {
                Some(ca_path) => ca_path,
                None => return Err(ERROR_TLS_NEEDS_CA.to_owned()),
            };
            let session: TlsStream<TcpStream> = open_tls(address, ca_path, ALPN_HTTP1).await?;
            Ok(Box::new(session))
        }
        Protocol::H2c | Protocol::H2Tls => {
            let transport: Box<dyn Transport> = match protocol {
                Protocol::H2Tls => {
                    let ca_path: &str = match ca_path {
                        Some(ca_path) => ca_path,
                        None => return Err(ERROR_TLS_NEEDS_CA.to_owned()),
                    };
                    let session: TlsStream<TcpStream> =
                        open_tls(address, ca_path, ALPN_HTTP2).await?;
                    Box::new(session)
                }
                _ => {
                    let stream: TcpStream = open_plain(address).await?;
                    Box::new(stream)
                }
            };
            Ok(transport)
        }
        Protocol::Http3 => Err(ERROR_TLS_NEEDS_CA.to_owned()),
    }
}

/// Resolves an address to the socket address the clients connect to.
///
/// The harness names the server by its container name, and the QUIC client
/// takes a numeric address. The name is resolved here, once, so the client
/// does not have to carry a resolver of its own.
///
/// # Arguments
///
/// - `&str` - The address, either numeric or a host name.
///
/// # Returns
///
/// - `Result<SocketAddr, String>` - The socket address, or the reason it could
///   not be resolved.
pub(crate) async fn resolve_address(address: &str) -> Result<SocketAddr, String> {
    if let Ok(parsed) = address.parse::<SocketAddr>() {
        return Ok(parsed);
    }
    let mut resolved: Vec<SocketAddr> = match lookup_host(address).await {
        Ok(resolved) => resolved.collect(),
        Err(error) => return Err(format!("{ERROR_RESOLVE}: {error}")),
    };
    match resolved.pop() {
        Some(address) => Ok(address),
        None => Err(format!("{ERROR_RESOLVE}: {address} resolves to nothing")),
    }
}

/// Drives one HTTP/2 connection until the deadline.
///
/// # Arguments
///
/// - `Run` - The parameters of the run.
///
/// # Returns
///
/// - `ConnStats` - The counters of the connection.
pub(crate) async fn run_http2(run: Run) -> ConnStats {
    let mut stats: ConnStats = ConnStats::default();
    let transport: Box<dyn Transport> =
        match open_transport(&run.address, run.protocol, run.ca_path.as_deref()).await {
            Ok(transport) => transport,
            Err(_) => return stats,
        };
    let (mut sender, connection) = match handshake(transport).await {
        Ok(handshake) => handshake,
        Err(_) => return stats,
    };
    let driver: JoinHandle<()> = spawn(async move {
        let _: Result<(), H2Error> = connection.await;
    });
    let scheme: &str = run.protocol.scheme();
    let _: BarrierWaitResult = run.barrier.wait().await;
    let mut index: usize = 0;
    loop {
        let now: Instant = Instant::now();
        if now >= run.deadline {
            break;
        }
        let Some(case) = run.cases.get(index % run.cases.len()) else {
            break;
        };
        index = index.wrapping_add(1);
        let request: Request<()> = build_request(case, scheme, AUTHORITY);
        let (response, mut send_body) = match sender.send_request(request, case.body.is_empty()) {
            Ok(pair) => pair,
            Err(_) => {
                stats.errors += 1;
                break;
            }
        };
        if !case.body.is_empty() {
            let body: Bytes = Bytes::copy_from_slice(case.body.as_slice());
            if send_body.send_data(body, true).is_err() {
                stats.errors += 1;
                break;
            }
        }
        let received: Response<RecvStream> = match response.await {
            Ok(response) => response,
            Err(_) => {
                stats.errors += 1;
                break;
            }
        };
        let length: usize = match read_h2_body(received.into_body()).await {
            Ok(length) => length,
            Err(_) => {
                stats.errors += 1;
                break;
            }
        };
        if length == case.expect_len {
            stats.completed += 1;
        } else {
            stats.errors += 1;
        }
        let micros: u128 = now.elapsed().as_micros();
        if now >= run.warmup_until {
            stats
                .latencies_us
                .push(u64::try_from(micros).unwrap_or(u64::MAX));
        }
    }
    driver.abort();
    stats
}

/// Sends one HTTP/2 request on a connection that is opened for it alone.
///
/// # Arguments
///
/// - `&str` - The address of the server under test.
/// - `&ProtocolCase` - The request to send.
/// - `Protocol` - The protocol the connection speaks.
/// - `Option<&str>` - The path of the certificate authority.
///
/// # Returns
///
/// - `Result<usize, String>` - The body length of the answer, or the reason the
///   exchange failed.
pub(crate) async fn send_one_http2(
    address: &str,
    case: &ProtocolCase,
    protocol: Protocol,
    ca_path: Option<&str>,
) -> Result<usize, String> {
    let transport: Box<dyn Transport> = open_transport(address, protocol, ca_path).await?;
    let (mut sender, connection) = handshake(transport)
        .await
        .map_err(|error: H2Error| error.to_string())?;
    let driver: JoinHandle<()> = spawn(async move {
        let _: Result<(), H2Error> = connection.await;
    });
    let scheme: &str = protocol.scheme();
    let request: Request<()> = build_request(case, scheme, AUTHORITY);
    let (response, mut send_body) = sender
        .send_request(request, case.body.is_empty())
        .map_err(|error: H2Error| error.to_string())?;
    if !case.body.is_empty() {
        let body: Bytes = Bytes::copy_from_slice(case.body.as_slice());
        send_body
            .send_data(body, true)
            .map_err(|error: H2Error| error.to_string())?;
    }
    let received: Response<RecvStream> =
        response.await.map_err(|error: H2Error| error.to_string())?;
    let length: usize = read_h2_body(received.into_body())
        .await
        .map_err(|error: String| error)?;
    driver.abort();
    Ok(length)
}

/// Drives one short HTTP/2 connection: one connection, one request, torn down.
///
/// # Arguments
///
/// - `Run` - The parameters of the run.
///
/// # Returns
///
/// - `ConnStats` - The counters of the connection.
pub(crate) async fn run_http2_short(run: Run) -> ConnStats {
    let mut stats: ConnStats = ConnStats::default();
    let _: BarrierWaitResult = run.barrier.wait().await;
    let interval: Duration = Duration::from_nanos(
        1_000_000_000 * u64::try_from(run.connections).unwrap_or(1) / run.rate_per_sec.max(1),
    );
    let mut next_open: Instant = Instant::now();
    let mut index: usize = 0;
    loop {
        let now: Instant = Instant::now();
        if now >= run.deadline {
            break;
        }
        let Some(case) = run.cases.get(index % run.cases.len()) else {
            break;
        };
        index = index.wrapping_add(1);
        if now < next_open {
            sleep(next_open - now).await;
        }
        next_open += interval;
        match send_one_http2(&run.address, case, run.protocol, run.ca_path.as_deref()).await {
            Ok(length) => {
                if length == case.expect_len {
                    stats.completed += 1;
                } else {
                    stats.errors += 1;
                }
            }
            Err(_) => {
                stats.errors += 1;
            }
        }
        let micros: u128 = now.elapsed().as_micros();
        if now >= run.warmup_until {
            stats
                .latencies_us
                .push(u64::try_from(micros).unwrap_or(u64::MAX));
        }
    }
    stats
}

/// Drives one HTTP/3 connection until the deadline.
///
/// # Arguments
///
/// - `Run` - The parameters of the run.
///
/// # Returns
///
/// - `ConnStats` - The counters of the connection.
pub(crate) async fn run_http3(run: Run) -> ConnStats {
    let mut stats: ConnStats = ConnStats::default();
    let Some(ca_path) = run.ca_path.clone() else {
        return stats;
    };
    let config: QuicClientConfig = match build_quic_config(&ca_path) {
        Ok(config) => config,
        Err(_) => return stats,
    };
    let bind: SocketAddr = match QUIC_BIND_ADDRESS.parse() {
        Ok(bind) => bind,
        Err(_) => return stats,
    };
    let endpoint: Endpoint = match Endpoint::client(bind) {
        Ok(endpoint) => endpoint,
        Err(_) => return stats,
    };
    let server: SocketAddr = match resolve_address(&run.address).await {
        Ok(server) => server,
        Err(_) => return stats,
    };
    let connecting: Connecting = match endpoint.connect_with(config, server, VERIFY_NAME) {
        Ok(connecting) => connecting,
        Err(_) => return stats,
    };
    let connection: QuicConnection = match connecting.await {
        Ok(connection) => connection,
        Err(_) => return stats,
    };
    let (client, mut sender) = match builder().build(Http3Transport::new(connection)).await {
        Ok(pair) => pair,
        Err(_) => return stats,
    };
    let driver: JoinHandle<()> = spawn(async move {
        let mut client: Http3Connection = client;
        let _: Http3ConnectionError =
            poll_fn(|context: &mut Context<'_>| client.poll_close(context)).await;
    });
    let _: BarrierWaitResult = run.barrier.wait().await;
    let mut index: usize = 0;
    loop {
        let now: Instant = Instant::now();
        if now >= run.deadline {
            break;
        }
        let Some(case) = run.cases.get(index % run.cases.len()) else {
            break;
        };
        index = index.wrapping_add(1);
        let request: Request<()> = build_request(case, SCHEME_SECURE, AUTHORITY);
        let mut stream: RequestStream<BidiStream<Bytes>, Bytes> =
            match sender.send_request(request).await {
                Ok(stream) => stream,
                Err(_) => {
                    stats.errors += 1;
                    break;
                }
            };
        if !case.body.is_empty() {
            let body: Bytes = Bytes::copy_from_slice(case.body.as_slice());
            if stream.send_data(body).await.is_err() {
                stats.errors += 1;
                break;
            }
        }
        if stream.finish().await.is_err() {
            stats.errors += 1;
            break;
        }
        if stream.recv_response().await.is_err() {
            stats.errors += 1;
            break;
        }
        let mut length: usize = 0;
        loop {
            match stream.recv_data().await {
                Ok(Some(chunk)) => length += chunk.chunk().len(),
                Ok(None) => break,
                Err(_) => {
                    stats.errors += 1;
                    break;
                }
            }
        }
        if length == case.expect_len {
            stats.completed += 1;
        } else {
            stats.errors += 1;
        }
        let micros: u128 = now.elapsed().as_micros();
        if now >= run.warmup_until {
            stats
                .latencies_us
                .push(u64::try_from(micros).unwrap_or(u64::MAX));
        }
    }
    driver.abort();
    endpoint.close(0u8.into(), QUIC_CLOSE_REASON);
    stats
}

/// Sends one HTTP/3 request on a connection that is opened for it alone.
///
/// # Arguments
///
/// - `&str` - The address of the server under test.
/// - `&ProtocolCase` - The request to send.
/// - `Option<String>` - The path of the certificate authority.
///
/// # Returns
///
/// - `Result<usize, String>` - The body length of the answer, or the reason the
///   exchange failed.
pub(crate) async fn send_one_http3(
    address: &str,
    case: &ProtocolCase,
    ca_path: Option<String>,
) -> Result<usize, String> {
    let ca_path: String = match ca_path {
        Some(ca_path) => ca_path,
        None => return Err(ERROR_TLS_NEEDS_CA.to_owned()),
    };
    let config: QuicClientConfig = build_quic_config(&ca_path)?;
    let bind: SocketAddr = QUIC_BIND_ADDRESS
        .parse()
        .map_err(|error: AddrParseError| error.to_string())?;
    let endpoint: Endpoint = Endpoint::client(bind).map_err(|error: Error| error.to_string())?;
    let server: SocketAddr = resolve_address(address).await?;
    let connecting: Connecting = endpoint
        .connect_with(config, server, VERIFY_NAME)
        .map_err(|error: ConnectError| error.to_string())?;
    let connection: QuicConnection = connecting
        .await
        .map_err(|error: ConnectionError| error.to_string())?;
    let (client, mut sender) = builder()
        .build(Http3Transport::new(connection))
        .await
        .map_err(|error: Http3ConnectionError| error.to_string())?;
    let driver: JoinHandle<()> = spawn(async move {
        let mut client: Http3Connection = client;
        let _: Http3ConnectionError =
            poll_fn(|context: &mut Context<'_>| client.poll_close(context)).await;
    });
    let request: Request<()> = build_request(case, SCHEME_SECURE, AUTHORITY);
    let mut stream: RequestStream<BidiStream<Bytes>, Bytes> = sender
        .send_request(request)
        .await
        .map_err(|error: StreamError| error.to_string())?;
    if !case.body.is_empty() {
        let body: Bytes = Bytes::copy_from_slice(case.body.as_slice());
        stream
            .send_data(body)
            .await
            .map_err(|error: StreamError| error.to_string())?;
    }
    stream
        .finish()
        .await
        .map_err(|error: StreamError| error.to_string())?;
    stream
        .recv_response()
        .await
        .map_err(|error: StreamError| error.to_string())?;
    let mut length: usize = 0;
    loop {
        match stream.recv_data().await {
            Ok(Some(chunk)) => length += chunk.chunk().len(),
            Ok(None) => break,
            Err(error) => {
                driver.abort();
                return Err(error.to_string());
            }
        }
    }
    driver.abort();
    endpoint.close(0u8.into(), QUIC_CLOSE_REASON);
    Ok(length)
}

/// Drives one short HTTP/3 connection: one connection, one request, torn down.
///
/// # Arguments
///
/// - `Run` - The parameters of the run.
///
/// # Returns
///
/// - `ConnStats` - The counters of the connection.
pub(crate) async fn run_http3_short(run: Run) -> ConnStats {
    let mut stats: ConnStats = ConnStats::default();
    let _: BarrierWaitResult = run.barrier.wait().await;
    let interval: Duration = Duration::from_nanos(
        1_000_000_000 * u64::try_from(run.connections).unwrap_or(1) / run.rate_per_sec.max(1),
    );
    let mut next_open: Instant = Instant::now();
    let mut index: usize = 0;
    loop {
        let now: Instant = Instant::now();
        if now >= run.deadline {
            break;
        }
        let Some(case) = run.cases.get(index % run.cases.len()) else {
            break;
        };
        index = index.wrapping_add(1);
        if now < next_open {
            sleep(next_open - now).await;
        }
        next_open += interval;
        match send_one_http3(&run.address, case, run.ca_path.clone()).await {
            Ok(length) => {
                if length == case.expect_len {
                    stats.completed += 1;
                } else {
                    stats.errors += 1;
                }
            }
            Err(_) => {
                stats.errors += 1;
            }
        }
        let micros: u128 = now.elapsed().as_micros();
        if now >= run.warmup_until {
            stats
                .latencies_us
                .push(u64::try_from(micros).unwrap_or(u64::MAX));
        }
    }
    stats
}

/// Builds the request one case of the mix carries.
///
/// # Arguments
///
/// - `&ProtocolCase` - The case.
/// - `&str` - The scheme the request carries.
/// - `&str` - The authority the request addresses.
///
/// # Returns
///
/// - `Request<()>` - The request.
pub(crate) fn build_request(case: &ProtocolCase, scheme: &str, authority: &str) -> Request<()> {
    let text: String = String::from_utf8_lossy(&case.bytes).to_string();
    let mut lines: Vec<&str> = text.split("\r\n").collect();
    let target: &str = lines.first().copied().unwrap_or_default();
    let mut fields: Vec<&str> = target.split(' ').collect();
    let method: &str = fields.first().copied().unwrap_or(METHOD_GET);
    let path: &str = fields.get(1).copied().unwrap_or(PATH_ROOT);
    fields.clear();
    let mut request: Request<()> = Request::new(());
    *request.method_mut() = match Method::from_bytes(method.as_bytes()) {
        Ok(method) => method,
        Err(_) => Method::GET,
    };
    let uri: String = format!("{scheme}://{authority}{path}");
    match uri.parse::<Uri>() {
        Ok(parsed) => *request.uri_mut() = parsed,
        Err(_) => *request.uri_mut() = Uri::from_static("/"),
    }
    let mut headers: HeaderMap = HeaderMap::new();
    if let Ok(value) = HeaderValue::from_str(authority) {
        headers.insert(HOST, value);
    }
    headers.insert(ACCEPT, HeaderValue::from_static(HEADER_ACCEPT));
    headers.insert(USER_AGENT, HeaderValue::from_static(HEADER_USER_AGENT));
    if let Ok(value) = HeaderValue::from_str(HEADER_TAG_VALUE)
        && let Ok(name) = HeaderName::from_bytes(HEADER_TAG.as_bytes())
    {
        headers.insert(name, value);
    }
    *request.headers_mut() = headers;
    lines.clear();
    request
}

/// Reads the body of one HTTP/2 response.
///
/// # Arguments
///
/// - `RecvStream` - The receive half of the response stream.
///
/// # Returns
///
/// - `Result<usize, String>` - The body length, or the reason the body could
///   not be read.
pub(crate) async fn read_h2_body(mut body: RecvStream) -> Result<usize, String> {
    let mut length: usize = 0;
    while let Some(chunk) = body.data().await {
        let chunk: Bytes = match chunk {
            Ok(chunk) => chunk,
            Err(error) => return Err(error.to_string()),
        };
        length += chunk.len();
    }
    Ok(length)
}