Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add support to config dns client cache size #1308

Merged
merged 1 commit into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/shadowsocks-service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,11 @@ pub struct LocalConfig {
/// Sending DNS query through proxy to this address
#[cfg(feature = "local-dns")]
pub remote_dns_addr: Option<Address>,
// client cache size
// if a lot of `create connection` observed in log,
// increase the size
#[cfg(feature = "local-dns")]
pub client_cache_size: Option<usize>,

/// Tun interface's name
///
Expand Down Expand Up @@ -919,6 +924,8 @@ impl LocalConfig {
local_dns_addr: None,
#[cfg(feature = "local-dns")]
remote_dns_addr: None,
#[cfg(feature = "local-dns")]
client_cache_size: Some(5),

#[cfg(feature = "local-tun")]
tun_interface_name: None,
Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks-service/src/local/dns/client_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl DnsClientCache {
}
}
Entry::Vacant(vac) => {
let mut q = VecDeque::with_capacity(5);
let mut q = VecDeque::with_capacity(self.max_client_per_addr);
q.push_back(client);
vac.insert(q);
}
Expand Down
14 changes: 10 additions & 4 deletions crates/shadowsocks-service/src/local/dns/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub struct DnsBuilder {
remote_addr: Address,
bind_addr: ServerAddr,
balancer: PingBalancer,
client_cache_size: usize,
}

impl DnsBuilder {
Expand All @@ -62,9 +63,10 @@ impl DnsBuilder {
local_addr: NameServerAddr,
remote_addr: Address,
balancer: PingBalancer,
client_cache_size: usize,
) -> DnsBuilder {
let context = ServiceContext::new();
DnsBuilder::with_context(Arc::new(context), bind_addr, local_addr, remote_addr, balancer)
DnsBuilder::with_context(Arc::new(context), bind_addr, local_addr, remote_addr, balancer, client_cache_size)
}

/// Create with an existed `context`
Expand All @@ -74,6 +76,7 @@ impl DnsBuilder {
local_addr: NameServerAddr,
remote_addr: Address,
balancer: PingBalancer,
client_cache_size: usize,
) -> DnsBuilder {
DnsBuilder {
context,
Expand All @@ -82,6 +85,7 @@ impl DnsBuilder {
remote_addr,
bind_addr,
balancer,
client_cache_size,
}
}

Expand All @@ -92,7 +96,8 @@ impl DnsBuilder {

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

let local_addr = Arc::new(self.local_addr);
let remote_addr = Arc::new(self.remote_addr);
Expand Down Expand Up @@ -589,10 +594,11 @@ struct DnsClient {
}

impl DnsClient {
fn new(context: Arc<ServiceContext>, balancer: PingBalancer, mode: Mode) -> DnsClient {
fn new(context: Arc<ServiceContext>, balancer: PingBalancer, mode: Mode,
client_cache_size: usize) -> DnsClient {
DnsClient {
context,
client_cache: DnsClientCache::new(5),
client_cache: DnsClientCache::new(client_cache_size),
mode,
balancer,
attempts: 2,
Expand Down
2 changes: 2 additions & 0 deletions crates/shadowsocks-service/src/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,15 @@ impl Server {
let mut server_builder = {
let local_addr = local_config.local_dns_addr.expect("missing local_dns_addr");
let remote_addr = local_config.remote_dns_addr.expect("missing remote_dns_addr");
let client_cache_size = local_config.client_cache_size.unwrap();

DnsBuilder::with_context(
context.clone(),
client_addr,
local_addr.clone(),
remote_addr.clone(),
balancer,
client_cache_size,
)
};
server_builder.set_mode(local_config.mode);
Expand Down