Struct SrvClient

Source
pub struct SrvClient<Resolver, Policy: Policy = Affinity> { /* private fields */ }
Expand description

Client for intelligently performing operations on a service located by SRV records.

§Usage

After being created by SrvClient::new or SrvClient::new_with_resolver, operations can be performed on the service pointed to by a SrvClient with the execute and execute_stream methods.

§DNS Resolvers

The resolver used to lookup SRV records is determined by a client’s SrvResolver, and can be set with SrvClient::resolver.

§SRV Target Selection Policies

SRV target selection order is determined by a client’s Policy, and can be set with SrvClient::policy.

Implementations§

Source§

impl<Resolver: Default, Policy: Policy + Default> SrvClient<Resolver, Policy>

Source

pub fn new(srv_name: impl ToString) -> Self

Creates a new client for communicating with services located by srv_name.

§Examples
use srv_rs::{SrvClient, resolver::libresolv::LibResolv};
let client = SrvClient::<LibResolv>::new("_http._tcp.example.com");
Source§

impl<Resolver, Policy: Policy + Default> SrvClient<Resolver, Policy>

Source

pub fn new_with_resolver(srv_name: impl ToString, resolver: Resolver) -> Self

Creates a new client for communicating with services located by srv_name.

Source§

impl<Resolver: SrvResolver, Policy: Policy> SrvClient<Resolver, Policy>

Source

pub async fn get_srv_records( &self, ) -> Result<(Vec<Resolver::Record>, Instant), Error<Resolver::Error>>

Gets a fresh set of SRV records from a client’s DNS resolver, returning them along with the time they’re valid until.

Source

pub async fn get_fresh_uri_candidates( &self, ) -> Result<(Vec<Uri>, Instant), Error<Resolver::Error>>

Gets a fresh set of SRV records from a client’s DNS resolver and parses their target/port pairs into URIs, which are returned along with the time they’re valid until–i.e., the time a cache containing these URIs should expire.

Source

pub async fn execute_stream<'a, T, E, Fut>( &'a self, execution_mode: Execution, func: impl FnMut(Uri) -> Fut + 'a, ) -> Result<impl Stream<Item = Result<T, E>> + 'a, Error<Resolver::Error>>
where E: Error, Fut: Future<Output = Result<T, E>> + 'a,

Performs an operation on all of a client’s SRV targets, producing a stream of results (one for each target). If the serial execution mode is specified, the operation will be performed on each target in the order determined by the current Policy, and the results will be returned in the same order. If the concurrent execution mode is specified, the operation will be performed on all targets concurrently, and results will be returned in the order they become available.

§Examples
use srv_rs::{SrvClient, Error, Execution};
use srv_rs::resolver::libresolv::{LibResolv, LibResolvError};

let results_stream = client.execute_stream(Execution::Serial, |address| async move {
    Ok::<_, std::convert::Infallible>(address.to_string())
})
.await?;
// Do something with the stream, for example collect all results into a `Vec`:
use futures::stream::StreamExt;
let results: Vec<Result<_, _>> = results_stream.collect().await;
for result in results {
    assert!(result.is_ok());
}
Source

pub async fn execute<T, E, Fut>( &self, execution_mode: Execution, func: impl FnMut(Uri) -> Fut, ) -> Result<Result<T, E>, Error<Resolver::Error>>
where E: Error, Fut: Future<Output = Result<T, E>>,

Performs an operation on a client’s SRV targets, producing the first successful result or the last error encountered if every execution of the operation was unsuccessful.

§Examples
use srv_rs::{SrvClient, Error, Execution};
use srv_rs::resolver::libresolv::{LibResolv, LibResolvError};

let client = SrvClient::<LibResolv>::new(EXAMPLE_SRV);

let res = client.execute(Execution::Serial, |address| async move {
    Ok::<_, std::convert::Infallible>(address.to_string())
})
.await?;
assert!(res.is_ok());

let res = client.execute(Execution::Concurrent, |address| async move {
    address.to_string().parse::<usize>()
})
.await?;
assert!(res.is_err());
Source§

impl<Resolver, Policy: Policy> SrvClient<Resolver, Policy>

Source

pub fn srv_name(self, srv_name: impl ToString) -> Self

Sets the SRV name of the client.

Source

pub fn resolver<R>(self, resolver: R) -> SrvClient<R, Policy>

Sets the resolver of the client.

Source

pub fn policy<P: Policy>(self, policy: P) -> SrvClient<Resolver, P>

Sets the policy of the client.

§Examples
use srv_rs::{SrvClient, policy::Rfc2782, resolver::libresolv::LibResolv};
let client = SrvClient::<LibResolv>::new(EXAMPLE_SRV).policy(Rfc2782);
Source

pub fn http_scheme(self, http_scheme: Scheme) -> Self

Sets the http scheme of the client.

Source

pub fn path_prefix(self, path_prefix: impl ToString) -> Self

Sets the path prefix of the client.

Trait Implementations§

Source§

impl<Resolver: Debug, Policy: Debug + Policy> Debug for SrvClient<Resolver, Policy>
where Policy::CacheItem: Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Resolver, Policy = Affinity> !Freeze for SrvClient<Resolver, Policy>

§

impl<Resolver, Policy> RefUnwindSafe for SrvClient<Resolver, Policy>
where Resolver: RefUnwindSafe, Policy: RefUnwindSafe, <Policy as Policy>::CacheItem: RefUnwindSafe,

§

impl<Resolver, Policy> Send for SrvClient<Resolver, Policy>
where Resolver: Send, Policy: Send, <Policy as Policy>::CacheItem: Sync + Send,

§

impl<Resolver, Policy> Sync for SrvClient<Resolver, Policy>
where Resolver: Sync, Policy: Sync, <Policy as Policy>::CacheItem: Sync + Send,

§

impl<Resolver, Policy> Unpin for SrvClient<Resolver, Policy>
where Resolver: Unpin, Policy: Unpin,

§

impl<Resolver, Policy> UnwindSafe for SrvClient<Resolver, Policy>
where Resolver: UnwindSafe, Policy: UnwindSafe, <Policy as Policy>::CacheItem: RefUnwindSafe,

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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
Source§

impl<T> ErasedDestructor for T
where T: 'static,