Skip to main content

Agent

Struct Agent 

Source
pub struct Agent { /* private fields */ }
Expand description

A Faith HTTP agent: where all fetches start.

An agent holds the resources and state shared across requests — connection pool, caches, DNS resolver, cookie jar, HTTP/3 upgrade memory — and is the browser instance of this library. A typical application makes one and starts every request from it.

Agent::new takes the defaults; Agent::builder configures one.

Implementations§

Source§

impl Agent

Source

pub fn new() -> Result<Self, FaithError>

An agent with default options.

Source

pub fn builder() -> AgentOptionsBuilder

Build an agent a setting at a time.

Each option group is reached through a closure, so a group left alone is absent from the call rather than spelled out as absent. Durations are Duration whatever unit the setting is carried in, and anything unset takes its default.

use std::time::Duration;
use web_faith::Agent;

let agent = Agent::builder()
    .user_agent("YourApp/1.2.3")
    .timeout(|timeout| timeout.connect(Duration::from_secs(2)).build())
    .pool(|pool| pool.max_idle_per_host(8).build())
    .build()?;
Source§

impl Agent

Source

pub fn prefetch_dns( &self, host: &str, ) -> Result<impl Future<Output = ()> + use<>, FaithError>

Warm the DNS cache for host, so a later request to it skips the lookup.

Takes a bare host; a scheme, port or path is ignored. The future completes when the answer lands and never fails — the work is advisory — and does nothing under the system resolver, which has no cache to warm. A host with nothing to resolve, or a closed agent, is refused here rather than by the future.

Source

pub fn preconnect( &self, origin: &str, ) -> Result<impl Future<Output = ()> + use<>, FaithError>

Open a pooled connection to origin, so the first request to it skips DNS, TCP and TLS setup.

Takes an origin (scheme://host[:port]); a longer URL is reduced to one. Sends a synthetic HEAD to the origin’s root — which the origin will see in its logs — over the transport the next request would use. The future completes when the attempt finishes and never fails. Something unconnectable, or a closed agent, is refused here rather than by the future.

Source§

impl Agent

Source

pub fn cookies(&self) -> Option<&Arc<FaithJar>>

Available on crate feature cookies only.

The agent’s cookie jar, if it keeps one.

The jar itself, so cookies go in and out through the type web-faith-cookies documents. It stays readable after Self::close.

Source

pub fn client(&self) -> Option<ClientWithMiddleware>

Available on crate feature raw-client only.

The client this agent sends through, or None once it is closed.

A request takes its handle when it is issued, which lets one already in flight finish while a later one is refused.

Source

pub fn raw_client(&self) -> Option<Client>

Available on crate feature raw-client only.

The same client without Faith’s middleware.

A request on it skips the HTTP cache and the Alt-Svc layer, while sharing the connection pool.

Source

pub fn dns_resolver(&self) -> Option<FaithResolver>

Available on crate features dns and raw-client only.

The agent’s DNS resolver, or None once it is closed.

Source

pub fn close(&self)

Close the agent, releasing its connection pool, DNS resolver, and background tasks without waiting for the last clone to drop. Worth doing if you make many short-lived agents.

Requests already in flight run to completion. A request issued on a closed agent fails with FaithErrorKind::Closed. Calling it more than once is a no-op, and the cookie jar, if any, stays readable through cookies().

Source

pub fn network_changed(&self)

Tell the agent the network under it has changed, so it stops acting on what it learned about a network that is gone.

There is no portable signal for an interface or connectivity change, so call this yourself on whatever trigger fits — an OS notification, a VPN transition, a captive-portal sign-in.

Drops pooled connections, flushes the DNS cache, demotes confirmed HTTP/3 origins back to advertised so a probe re-verifies them, and clears the HTTP/3 failure, slow and path-time state. Configuration, http3.hints, Alt-Svc advertisements, the cookie jar, the HTTP cache and the counters are kept — none of those is a claim about a network path.

Requests in flight run to completion on the connections they hold. Harmless to call repeatedly, or on a closed agent.

Source

pub fn stats(&self) -> AgentStats

The agent’s counters, as they stand.

Source

pub fn connections(&self) -> Vec<ConnectionSnapshot>

Available on crate feature connection-tracking only.

The connections this agent currently holds open.

TCP only; QUIC connections are not visible here. Statistics refresh once a second, so sample over time for rates such as retransmissions. Which fields are filled depends on the platform: the lost-packet count and delivery rate are Linux-only, an unsupported platform reports an empty list, and no field is guaranteed to stay available.

Source

pub fn resolvers(&self) -> Vec<ResolverReport>

Available on crate feature dns only.

The DNS servers this agent resolves through, in query order.

Each entry gives the nameserver’s address, the transport in use, and how that was arrived at. Empty until the resolver has been used, and empty under the system resolver.

Source

pub fn is_closed(&self) -> bool

Whether Self::close has been called.

Source§

impl Agent

Source

pub fn fetch<T>(&self, target: T) -> FetchBuilder

Aim a request at target, to send when awaited.

The target is a URL, or a Request to layer over. See FetchBuilder.

Trait Implementations§

Source§

impl Clone for Agent

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Agent

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Agent

§

impl !RefUnwindSafe for Agent

§

impl !UnwindSafe for Agent

§

impl Send for Agent

§

impl Sync for Agent

§

impl Unpin for Agent

§

impl UnsafeUnpin for Agent

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more