race
parent
c4a1c9ce98
commit
4559e269e0
14
server.go
14
server.go
|
|
@ -10,17 +10,19 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Transport http.RoundTripper
|
Transport http.RoundTripper
|
||||||
resolver *net.Resolver
|
resolver *net.Resolver
|
||||||
limiter *rate.Limiter
|
limiter *rate.Limiter
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
dnsCache map[string]dns
|
dnsCacheLock sync.Mutex
|
||||||
|
dnsCache map[string]dns
|
||||||
}
|
}
|
||||||
|
|
||||||
type dns struct {
|
type dns struct {
|
||||||
|
|
@ -154,6 +156,8 @@ func (s *Server) dig(ctx context.Context, host string) (string, error) {
|
||||||
search := host
|
search := host
|
||||||
search = strings.TrimPrefix(search, "https://")
|
search = strings.TrimPrefix(search, "https://")
|
||||||
search = strings.TrimPrefix(search, "http://")
|
search = strings.TrimPrefix(search, "http://")
|
||||||
|
s.dnsCacheLock.Lock()
|
||||||
|
defer s.dnsCacheLock.Unlock()
|
||||||
if v, ok := s.dnsCache[host]; ok && v.ok() {
|
if v, ok := s.dnsCache[host]; ok && v.ok() {
|
||||||
return v.result, v.err
|
return v.result, v.err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue