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, Policy: Policy + Default> SrvClient<Resolver, Policy>
impl<Resolver, Policy: Policy + Default> SrvClient<Resolver, Policy>
Sourcepub fn new_with_resolver(srv_name: impl ToString, resolver: Resolver) -> Self
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>
impl<Resolver: SrvResolver, Policy: Policy> SrvClient<Resolver, Policy>
Sourcepub async fn get_srv_records(
&self,
) -> Result<(Vec<Resolver::Record>, Instant), Error<Resolver::Error>>
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.
Sourcepub async fn get_fresh_uri_candidates(
&self,
) -> Result<(Vec<Uri>, Instant), Error<Resolver::Error>>
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.
Sourcepub 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>>
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>>
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());
}
Sourcepub async fn execute<T, E, Fut>(
&self,
execution_mode: Execution,
func: impl FnMut(Uri) -> Fut,
) -> Result<Result<T, E>, Error<Resolver::Error>>
pub async fn execute<T, E, Fut>( &self, execution_mode: Execution, func: impl FnMut(Uri) -> Fut, ) -> Result<Result<T, E>, Error<Resolver::Error>>
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>
impl<Resolver, Policy: Policy> SrvClient<Resolver, Policy>
Sourcepub fn policy<P: Policy>(self, policy: P) -> SrvClient<Resolver, P>
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);
Sourcepub fn http_scheme(self, http_scheme: Scheme) -> Self
pub fn http_scheme(self, http_scheme: Scheme) -> Self
Sets the http scheme of the client.
Sourcepub fn path_prefix(self, path_prefix: impl ToString) -> Self
pub fn path_prefix(self, path_prefix: impl ToString) -> Self
Sets the path prefix of the client.