Skip to content

Commit 878c136

Browse files
(libmcping): Enable DNS caching for maximum SPEEEEEEEEEED
1 parent af8ca55 commit 878c136

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

libmcping/src/tokio/bedrock.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::{
77
time::{Duration, Instant},
88
};
99

10-
use hickory_resolver::{config::*, TokioAsyncResolver};
1110
use tokio::{
1211
io::{AsyncRead, AsyncReadExt, AsyncWriteExt},
1312
net::UdpSocket,
@@ -94,8 +93,7 @@ impl Connection {
9493
};
9594

9695
// Do a hostname lookup
97-
let resolver =
98-
TokioAsyncResolver::tokio(ResolverConfig::default(), ResolverOpts::default());
96+
let resolver = super::resolver();
9997

10098
let ip = resolver
10199
.lookup_ip(host.as_str())

libmcping/src/tokio/java.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::{
77
time::{Duration, Instant},
88
};
99

10-
use hickory_resolver::{config::*, TokioAsyncResolver};
1110
use tokio::{
1211
io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
1312
net::TcpStream,
@@ -122,8 +121,7 @@ impl Connection {
122121
};
123122

124123
// Attempt to lookup the ip of the server from an srv record, falling back on the ip from a host
125-
let resolver =
126-
TokioAsyncResolver::tokio(ResolverConfig::default(), ResolverOpts::default());
124+
let resolver = super::resolver();
127125

128126
// Determine what host to lookup by doing the following:
129127
// - Lookup the SRV record for the domain, if it exists perform a lookup of the ip from the target

libmcping/src/tokio/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
mod bedrock;
22
mod java;
33

4+
use std::sync::OnceLock;
5+
6+
use hickory_resolver::{
7+
config::{ResolverConfig, ResolverOpts},
8+
TokioAsyncResolver,
9+
};
10+
411
use crate::Error;
512

613
/// Represents a pingable entity.
@@ -52,3 +59,16 @@ pub trait AsyncPingable {
5259
pub async fn get_status<P: AsyncPingable>(pingable: P) -> Result<(u64, P::Response), Error> {
5360
pingable.ping().await
5461
}
62+
63+
fn new_resolver() -> TokioAsyncResolver {
64+
let config = ResolverConfig::cloudflare();
65+
let mut opts = ResolverOpts::default();
66+
opts.cache_size = 64;
67+
opts.attempts = 3;
68+
TokioAsyncResolver::tokio(config, opts)
69+
}
70+
71+
pub fn resolver() -> &'static TokioAsyncResolver {
72+
static RESOLVER: OnceLock<TokioAsyncResolver> = OnceLock::new();
73+
RESOLVER.get_or_init(new_resolver)
74+
}

0 commit comments

Comments
 (0)