mirror of
https://github.com/tailscale/tailscale.git
synced 2025-02-27 10:47:35 +00:00
cmd/derper: optimize handleBootstrapDNS
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 <josh@tailscale.com>
This commit is contained in:
parent
e206a3663f
commit
b9c92b90db
@ -12,14 +12,11 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var dnsCache atomic.Value // of []byte
|
||||||
dnsMu sync.Mutex
|
|
||||||
dnsCache = map[string][]net.IP{}
|
|
||||||
)
|
|
||||||
|
|
||||||
var bootstrapDNSRequests = expvar.NewInt("counter_bootstrap_dns_requests")
|
var bootstrapDNSRequests = expvar.NewInt("counter_bootstrap_dns_requests")
|
||||||
|
|
||||||
@ -37,6 +34,7 @@ func refreshBootstrapDNS() {
|
|||||||
if *bootstrapDNS == "" {
|
if *bootstrapDNS == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
dnsEntries := make(map[string][]net.IP)
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
names := strings.Split(*bootstrapDNS, ",")
|
names := strings.Split(*bootstrapDNS, ",")
|
||||||
@ -47,23 +45,19 @@ func refreshBootstrapDNS() {
|
|||||||
log.Printf("bootstrap DNS lookup %q: %v", name, err)
|
log.Printf("bootstrap DNS lookup %q: %v", name, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
dnsMu.Lock()
|
dnsEntries[name] = addrs
|
||||||
dnsCache[name] = addrs
|
|
||||||
dnsMu.Unlock()
|
|
||||||
}
|
}
|
||||||
|
j, err := json.MarshalIndent(dnsCache, "", "\t")
|
||||||
|
if err != nil {
|
||||||
|
// leave the old values in place
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dnsCache.Store(j)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleBootstrapDNS(w http.ResponseWriter, r *http.Request) {
|
func handleBootstrapDNS(w http.ResponseWriter, r *http.Request) {
|
||||||
bootstrapDNSRequests.Add(1)
|
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)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
j, _ := dnsCache.Load().([]byte)
|
||||||
w.Write(j)
|
w.Write(j)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user