From e9de332163f212a8d5fc513cbcc4995ad314e663 Mon Sep 17 00:00:00 2001 From: ohaiibuzzle <23693150+ohaiibuzzle@users.noreply.github.com> Date: Sun, 11 Aug 2024 19:35:02 +0700 Subject: [PATCH] fix: make doh truly singleton --- dns/doh.go | 50 +++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/dns/doh.go b/dns/doh.go index dd8c4cb..43e52bc 100644 --- a/dns/doh.go +++ b/dns/doh.go @@ -8,6 +8,7 @@ import ( "net" "net/http" "strings" + "sync" "time" "github.com/miekg/dns" @@ -20,35 +21,38 @@ type DoHClient struct { } var client *DoHClient +var clientOnce sync.Once func GetDoHClient(upstream string) *DoHClient { - if client == nil { - if !strings.HasPrefix(upstream, "https://") { - upstream = "https://" + upstream - } + clientOnce.Do(func() { + if client == nil { + if !strings.HasPrefix(upstream, "https://") { + upstream = "https://" + upstream + } - if !strings.HasSuffix(upstream, "/dns-query") { - upstream = upstream + "/dns-query" - } + if !strings.HasSuffix(upstream, "/dns-query") { + upstream = upstream + "/dns-query" + } - c := &http.Client{ - Timeout: 5 * time.Second, - Transport: &http.Transport{ - DialContext: (&net.Dialer{ - Timeout: 3 * time.Second, - KeepAlive: 30 * time.Second, - }).DialContext, - TLSHandshakeTimeout: 5 * time.Second, - MaxIdleConnsPerHost: 100, - MaxIdleConns: 100, - }, - } + c := &http.Client{ + Timeout: 5 * time.Second, + Transport: &http.Transport{ + DialContext: (&net.Dialer{ + Timeout: 3 * time.Second, + KeepAlive: 30 * time.Second, + }).DialContext, + TLSHandshakeTimeout: 5 * time.Second, + MaxIdleConnsPerHost: 100, + MaxIdleConns: 100, + }, + } - client = &DoHClient{ - upstream: upstream, - c: c, + client = &DoHClient{ + upstream: upstream, + c: c, + } } - } + }) return client }