Set timeouts and handle errors
For Rust developers calling servers that can be slow or answer with an error
You give a fibre_http client a timeout, turn error statuses like 404 into errors you can match on and watch a request to a server that never answers give up on time.
Before you start
You need Rust with Cargo. fibre_http comes from crates.io:
fibre_http = "0"Set up the project#
Start a new binary crate.
Put this in the [dependencies] section of Cargo.toml:
[]
= "0"This tutorial uses the blocking client, so it needs no async runtime.
The program#
Replace src/main.rs with this:
use TcpListener;
use ;
use ;
Run it:
What each part does#
timeout(Duration::from_secs(10)) on the builder gives every request from this client a 10 second limit. The limit covers the whole request: getting a connection, sending, waiting for the answer and reading the body.
A 404 is still a response, so send succeeds. error_for_status() turns any 4xx or 5xx status into Error::Status, which carries the code. That lets one match handle network failures and error statuses together.
The third request goes to a local port that accepts connections and never replies. timeout on the request replaces the client's 10 seconds with 500 ms for this one call. When the time runs out, send returns Error::Timeout.
Error has other cases worth matching on, such as Error::Tls for a certificate problem and Error::Refused when a server turns a request away before handling it. The usage guide lists them.
Where to go next#
The usage guide covers redirects, retries, proxies and the rest of the error cases: https://github.com/excsn/fibre/blob/main/http/README.USAGE.md
The full API is on docs.rs at https://docs.rs/fibre_http.
