Skip to content

Commit 34ebd07

Browse files
committed
fix(local-dns): DNS client cache create DnsClient only when missed
1 parent c9f47c6 commit 34ebd07

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

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

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -115,37 +115,46 @@ impl DnsClientCache {
115115
svr_cfg: Option<&ServerConfig>,
116116
) -> Result<Message, ProtoError> {
117117
let mut last_err = None;
118-
let mut dns_res: io::Result<DnsClient>;
119118
for _ in 0..self.retry_count {
120-
match dck {
121-
DnsClientKey::TcpLocal(tcp_l) => {
122-
dns_res = DnsClient::connect_tcp_local(*tcp_l, connect_opts.unwrap()).await;
123-
}
124-
DnsClientKey::UdpLocal(udp_l) => {
125-
dns_res = DnsClient::connect_udp_local(*udp_l, connect_opts.unwrap()).await;
126-
}
127-
DnsClientKey::TcpRemote(tcp_l) => {
128-
dns_res = DnsClient::connect_tcp_remote(
129-
context.unwrap().context(),
130-
svr_cfg.unwrap(),
131-
tcp_l,
132-
context.unwrap().connect_opts_ref(),
133-
context.unwrap().flow_stat(),
134-
)
135-
.await;
136-
}
137-
DnsClientKey::UdpRemote(udp_l) => {
138-
dns_res = DnsClient::connect_udp_remote(
139-
context.unwrap().context(),
140-
svr_cfg.unwrap(),
141-
udp_l.clone(),
142-
context.unwrap().connect_opts_ref(),
143-
context.unwrap().flow_stat(),
144-
)
145-
.await;
119+
let create_fn = async {
120+
match dck {
121+
DnsClientKey::TcpLocal(tcp_l) => {
122+
let connect_opts = connect_opts.expect("connect options is required for local DNS");
123+
DnsClient::connect_tcp_local(*tcp_l, connect_opts).await
124+
}
125+
DnsClientKey::UdpLocal(udp_l) => {
126+
let connect_opts = connect_opts.expect("connect options is required for local DNS");
127+
DnsClient::connect_udp_local(*udp_l, connect_opts).await
128+
}
129+
DnsClientKey::TcpRemote(tcp_l) => {
130+
let context = context.expect("context is required for remote DNS");
131+
let svr_cfg = svr_cfg.expect("server config is required for remote DNS");
132+
133+
DnsClient::connect_tcp_remote(
134+
context.context(),
135+
svr_cfg,
136+
tcp_l,
137+
context.connect_opts_ref(),
138+
context.flow_stat(),
139+
)
140+
.await
141+
}
142+
DnsClientKey::UdpRemote(udp_l) => {
143+
let context = context.expect("context is required for remote DNS");
144+
let svr_cfg = svr_cfg.expect("server config is required for remote DNS");
145+
146+
DnsClient::connect_udp_remote(
147+
context.context(),
148+
svr_cfg,
149+
udp_l.clone(),
150+
context.connect_opts_ref(),
151+
context.flow_stat(),
152+
)
153+
.await
154+
}
146155
}
147-
}
148-
match self.get_client_or_create(dck, async { dns_res }).await {
156+
};
157+
match self.get_client_or_create(dck, create_fn).await {
149158
Ok(mut client) => match client.lookup_timeout(msg.clone(), self.timeout).await {
150159
Ok(msg) => {
151160
self.save_client(dck.clone(), client).await;

0 commit comments

Comments
 (0)