1use crate::SrvRecord;
4use async_trait::async_trait;
5use rand::Rng;
6use std::time::Instant;
7
8#[cfg(feature = "libresolv")]
9pub mod libresolv;
10
11#[cfg(feature = "trust-dns")]
12mod trust_dns;
13
14#[async_trait]
16pub trait SrvResolver: Send + Sync {
17 type Record: SrvRecord;
19
20 type Error: std::error::Error + 'static;
22
23 async fn get_srv_records_unordered(
27 &self,
28 srv: &str,
29 ) -> Result<(Vec<Self::Record>, Instant), Self::Error>;
30
31 async fn get_srv_records(
35 &self,
36 srv: &str,
37 ) -> Result<(Vec<Self::Record>, Instant), Self::Error> {
38 let (mut records, valid_until) = self.get_srv_records_unordered(srv).await?;
39 Self::order_srv_records(&mut records, rand::thread_rng());
40 Ok((records, valid_until))
41 }
42
43 fn order_srv_records(records: &mut [Self::Record], mut rng: impl Rng) {
45 records.sort_by_cached_key(|record| record.sort_key(&mut rng));
46 }
47}