From b9c92b90db08bbcb7c726c307d2c7b2590278953 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Fri, 11 Feb 2022 12:30:36 -0800 Subject: [PATCH] cmd/derper: optimize handleBootstrapDNS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Do json formatting once, rather than on every request. Use an atomic.Value. name old time/op new time/op delta HandleBootstrapDNS-10 6.35µs ± 0% 0.10µs ± 4% -98.35% (p=0.000 n=14+15) name old alloc/op new alloc/op delta HandleBootstrapDNS-10 3.20kB ± 0% 0.42kB ± 0% -86.99% (p=0.000 n=12+15) name old allocs/op new allocs/op delta HandleBootstrapDNS-10 41.0 ± 0% 3.0 ± 0% -92.68% (p=0.000 n=15+15) Signed-off-by: Josh Bleecher Snyder --- cmd/derper/bootstrap_dns.go | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/cmd/derper/bootstrap_dns.go b/cmd/derper/bootstrap_dns.go index 2220e72a8..8c6b9cfed 100644 --- a/cmd/derper/bootstrap_dns.go +++ b/cmd/derper/bootstrap_dns.go @@ -12,14 +12,11 @@ import ( "net" "net/http" "strings" - "sync" + "sync/atomic" "time" ) -var ( - dnsMu sync.Mutex - dnsCache = map[string][]net.IP{} -) +var dnsCache atomic.Value // of []byte var bootstrapDNSRequests = expvar.NewInt("counter_bootstrap_dns_requests") @@ -37,6 +34,7 @@ func refreshBootstrapDNS() { if *bootstrapDNS == "" { return } + dnsEntries := make(map[string][]net.IP) ctx, cancel := context.WithTimeout(context.Background(), time.Minute) defer cancel() names := strings.Split(*bootstrapDNS, ",") @@ -47,23 +45,19 @@ func refreshBootstrapDNS() { log.Printf("bootstrap DNS lookup %q: %v", name, err) continue } - dnsMu.Lock() - dnsCache[name] = addrs - dnsMu.Unlock() + dnsEntries[name] = addrs } -} - -func handleBootstrapDNS(w http.ResponseWriter, r *http.Request) { - bootstrapDNSRequests.Add(1) - dnsMu.Lock() j, err := json.MarshalIndent(dnsCache, "", "\t") - dnsMu.Unlock() if err != nil { - log.Printf("bootstrap DNS JSON: %v", err) - http.Error(w, "JSON marshal error", 500) + // leave the old values in place return } + dnsCache.Store(j) +} +func handleBootstrapDNS(w http.ResponseWriter, r *http.Request) { + bootstrapDNSRequests.Add(1) w.Header().Set("Content-Type", "application/json") + j, _ := dnsCache.Load().([]byte) w.Write(j) }