Skip to main content

Module client

Module client 

Source
Expand description

Outbound HTTP client

Services call other services. HttpClient is a small client for that with the settings production calls need and ad-hoc code forgets:

  • timeouts for connecting and for the whole attempt, and a cap on the response size
  • connection pooling (keep-alive) per host
  • retries with exponential backoff and jitter for connection failures and 502/503/504/429 (honouring Retry-After), only for requests that are safe to repeat: idempotent methods or requests carrying an Idempotency-Key
  • redirects (followed, up to a limit; credentials are dropped when the target is another origin)
  • an SSRF guard (HttpClient::public_only): every connection, including those after a redirect, goes to a vetted public address
  • HTTPS with the Mozilla root certificates (feature client-tls)
use std::time::Duration;
use ullun::client::HttpClient;

let client = HttpClient::new()
    .timeout(Duration::from_secs(5))
    .retries(2)
    .public_only();
let res = client.get("http://example.com/api/items?limit=10").send().await?;
if res.status() == 200 {
    let items: serde_json::Value = res.json()?;
    println!("{items}");
}

ClientError converts into Error (504 for timeouts, 502 otherwise), so handlers can use ? on client calls.

Structs§

ClientResponse
What the server answered
HttpClient
HTTP client, see the module documentation. Cloning shares the connection pool.
RequestBuilder
A request under construction, created by the verbs of HttpClient

Enums§

ClientError
Why a client call failed