Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

HTTP Client Pool

High-level pool of HTTP clients where multiple connections that can be referenced in concurrent scenarios.

Reuses valid connections and recycles dropped communications to minimize contention and latency. Instances are created on-demand and maintained for subsequent requests to the same host.

Also useful because HTTP/2 and HTTP/3 expect long-lived sessions by default unlike HTTP/1.

To use this functionality, it is necessary to activate the http-client-pool feature.

Example

//! Fetches and prints the response body of a provided URI.
//!
//! Currently, only HTTP/2 is supported.

extern crate tokio;
extern crate wtx;
extern crate wtx_instances;

use wtx::{
  http::{HttpClient, Method, ReqResBuffer, client_pool::ClientPoolBuilder},
  misc::{Uri, from_utf8_basic},
};

#[tokio::main]
async fn main() -> wtx::Result<()> {
  let uri = Uri::new("SOME_URI");
  let pool = ClientPoolBuilder::tokio(1).build();
  let res = pool.send_recv_single(Method::Get, ReqResBuffer::empty(), &uri).await?;
  println!("{}", from_utf8_basic(&res.rrd.body)?);
  Ok(())
}