Skip to content

Commit

Permalink
Update DNS cache
Browse files Browse the repository at this point in the history
  • Loading branch information
andeya committed Mar 14, 2018
1 parent 8e39f7d commit 12e63e4
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions surf.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,17 @@ type DnsCache struct {
ipPortLib goutil.Map
}

// Reg registers DNS to cache.
// Reg registers ipPort to DNS cache.
func (d *DnsCache) Reg(addr, ipPort string) {
d.ipPortLib.Store(addr, ipPort)
}

// Query queries DNS from cache.
// Del deletes ipPort from DNS cache.
func (d *DnsCache) Del(addr string) {
d.ipPortLib.Delete(addr)
}

// Query queries ipPort from DNS cache.
func (d *DnsCache) Query(addr string) (string, bool) {
ipPort, ok := d.ipPortLib.Load(addr)
if !ok {
Expand Down Expand Up @@ -125,6 +130,12 @@ func (surf *Surf) buildClient(req *Request) *http.Client {
dnsCache.Reg(addr, c.RemoteAddr().String())
}
}()
} else {
defer func() {
if err != nil {
dnsCache.Del(addr)
}
}()
}
c, err = net.DialTimeout(network, ipPort, req.DialTimeout)
if err != nil {
Expand Down

0 comments on commit 12e63e4

Please sign in to comment.