Skip to content

Commit 68e9306

Browse files
authored
feature: add support to config dns client cache size (#1308)
1 parent 2021741 commit 68e9306

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

crates/shadowsocks-service/src/config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,11 @@ pub struct LocalConfig {
860860
/// Sending DNS query through proxy to this address
861861
#[cfg(feature = "local-dns")]
862862
pub remote_dns_addr: Option<Address>,
863+
// client cache size
864+
// if a lot of `create connection` observed in log,
865+
// increase the size
866+
#[cfg(feature = "local-dns")]
867+
pub client_cache_size: Option<usize>,
863868

864869
/// Tun interface's name
865870
///
@@ -919,6 +924,8 @@ impl LocalConfig {
919924
local_dns_addr: None,
920925
#[cfg(feature = "local-dns")]
921926
remote_dns_addr: None,
927+
#[cfg(feature = "local-dns")]
928+
client_cache_size: Some(5),
922929

923930
#[cfg(feature = "local-tun")]
924931
tun_interface_name: None,

crates/shadowsocks-service/src/local/dns/client_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl DnsClientCache {
196196
}
197197
}
198198
Entry::Vacant(vac) => {
199-
let mut q = VecDeque::with_capacity(5);
199+
let mut q = VecDeque::with_capacity(self.max_client_per_addr);
200200
q.push_back(client);
201201
vac.insert(q);
202202
}

crates/shadowsocks-service/src/local/dns/server.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub struct DnsBuilder {
5353
remote_addr: Address,
5454
bind_addr: ServerAddr,
5555
balancer: PingBalancer,
56+
client_cache_size: usize,
5657
}
5758

5859
impl DnsBuilder {
@@ -62,9 +63,10 @@ impl DnsBuilder {
6263
local_addr: NameServerAddr,
6364
remote_addr: Address,
6465
balancer: PingBalancer,
66+
client_cache_size: usize,
6567
) -> DnsBuilder {
6668
let context = ServiceContext::new();
67-
DnsBuilder::with_context(Arc::new(context), bind_addr, local_addr, remote_addr, balancer)
69+
DnsBuilder::with_context(Arc::new(context), bind_addr, local_addr, remote_addr, balancer, client_cache_size)
6870
}
6971

7072
/// Create with an existed `context`
@@ -74,6 +76,7 @@ impl DnsBuilder {
7476
local_addr: NameServerAddr,
7577
remote_addr: Address,
7678
balancer: PingBalancer,
79+
client_cache_size: usize,
7780
) -> DnsBuilder {
7881
DnsBuilder {
7982
context,
@@ -82,6 +85,7 @@ impl DnsBuilder {
8285
remote_addr,
8386
bind_addr,
8487
balancer,
88+
client_cache_size,
8589
}
8690
}
8791

@@ -92,7 +96,8 @@ impl DnsBuilder {
9296

9397
/// Build DNS server
9498
pub async fn build(self) -> io::Result<Dns> {
95-
let client = Arc::new(DnsClient::new(self.context.clone(), self.balancer, self.mode));
99+
let client = Arc::new(DnsClient::new(self.context.clone(), self.balancer, self.mode,
100+
self.client_cache_size));
96101

97102
let local_addr = Arc::new(self.local_addr);
98103
let remote_addr = Arc::new(self.remote_addr);
@@ -589,10 +594,11 @@ struct DnsClient {
589594
}
590595

591596
impl DnsClient {
592-
fn new(context: Arc<ServiceContext>, balancer: PingBalancer, mode: Mode) -> DnsClient {
597+
fn new(context: Arc<ServiceContext>, balancer: PingBalancer, mode: Mode,
598+
client_cache_size: usize) -> DnsClient {
593599
DnsClient {
594600
context,
595-
client_cache: DnsClientCache::new(5),
601+
client_cache: DnsClientCache::new(client_cache_size),
596602
mode,
597603
balancer,
598604
attempts: 2,

crates/shadowsocks-service/src/local/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,15 @@ impl Server {
353353
let mut server_builder = {
354354
let local_addr = local_config.local_dns_addr.expect("missing local_dns_addr");
355355
let remote_addr = local_config.remote_dns_addr.expect("missing remote_dns_addr");
356+
let client_cache_size = local_config.client_cache_size.unwrap();
356357

357358
DnsBuilder::with_context(
358359
context.clone(),
359360
client_addr,
360361
local_addr.clone(),
361362
remote_addr.clone(),
362363
balancer,
364+
client_cache_size,
363365
)
364366
};
365367
server_builder.set_mode(local_config.mode);

0 commit comments

Comments
 (0)