优化查询性能

This commit is contained in:
lirui
2026-01-04 22:27:48 +08:00
parent 2eb133dc7a
commit 57fb2cfd67

View File

@@ -39,8 +39,12 @@ func NewDNSServer(port int, upstreamDNS string) *DNSServer {
UseDoH: false,
queries: make(map[string]*DNSQuery),
httpClient: &http.Client{
Timeout: 5 * time.Second,
Timeout: 3 * time.Second,
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 2 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: false,
},
@@ -57,8 +61,14 @@ func NewDNSServerWithDoH(port int, dohURL string) *DNSServer {
DoHURL: dohURL,
queries: make(map[string]*DNSQuery),
httpClient: &http.Client{
Timeout: 5 * time.Second,
Timeout: 3 * time.Second,
Transport: &http.Transport{
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 2 * time.Second,
DisableCompression: false,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: false,
},
@@ -153,7 +163,7 @@ func (s *DNSServer) forwardQueryUDP(query []byte) ([]byte, error) {
defer upstreamConn.Close()
// 设置超时
upstreamConn.SetDeadline(time.Now().Add(5 * time.Second))
upstreamConn.SetDeadline(time.Now().Add(3 * time.Second))
// 发送查询
_, err = upstreamConn.Write(query)
@@ -276,8 +286,8 @@ func (s *DNSServer) recordQuery(domain, queryType string) {
}
}
// 限制缓存大小
if len(s.queries) > 200 {
// 限制缓存大小,防止内存溢出
if len(s.queries) > 500 {
// 删除最旧的条目
oldestKey := ""
oldestTime := time.Now()