-
Notifications
You must be signed in to change notification settings - Fork 4
/
mod.rs
47 lines (39 loc) · 1.5 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! SRV resolvers.
use crate::SrvRecord;
use async_trait::async_trait;
use rand::Rng;
use std::time::Instant;
#[cfg(feature = "libresolv")]
pub mod libresolv;
#[cfg(feature = "trust-dns")]
mod trust_dns;
/// Represents the ability to act as a SRV resolver.
#[async_trait]
pub trait SrvResolver: Send + Sync {
/// SRV record representation produced by the resolver.
type Record: SrvRecord;
/// Errors encountered during SRV resolution.
type Error: std::error::Error + 'static;
/// Gets the records corresponding to a srv name without sorting by priority
/// or shuffling based on weight, returning them along with the time they're
/// valid until.
async fn get_srv_records_unordered(
&self,
srv: &str,
) -> Result<(Vec<Self::Record>, Instant), Self::Error>;
/// Gets the records corresponding to a srv name, sorting by priority and
/// shuffling based on weight, returning them along with the time they're
/// valid until.
async fn get_srv_records(
&self,
srv: &str,
) -> Result<(Vec<Self::Record>, Instant), Self::Error> {
let (mut records, valid_until) = self.get_srv_records_unordered(srv).await?;
Self::order_srv_records(&mut records, rand::thread_rng());
Ok((records, valid_until))
}
/// Sorts SRV records by priority and weight per RFC 2782.
fn order_srv_records(records: &mut [Self::Record], mut rng: impl Rng) {
records.sort_by_cached_key(|record| record.sort_key(&mut rng));
}
}