Skip to content

Commit

Permalink
Don't rate limit requests that have already been resolved, and cache …
Browse files Browse the repository at this point in the history
…negative responses.
  • Loading branch information
Tom Wilkie committed Sep 7, 2015
1 parent d3c7138 commit f5d6872
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions probe/endpoint/resolver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package endpoint

import (
"fmt"
"net"
"strings"
"time"
Expand All @@ -14,6 +15,8 @@ const (
rAddrCacheExpiration = 30 * time.Minute
)

var errNotFound = fmt.Errorf("Not found")

type revResFunc func(addr string) (names []string, err error)

// ReverseResolver is a caching, reverse resolver.
Expand Down Expand Up @@ -42,8 +45,11 @@ func NewReverseResolver() *ReverseResolver {
// possible names that can be obtained for that IP.
func (r *ReverseResolver) Get(address string) (string, error) {
val, err := r.cache.Get(address)
if err == nil {
return val.(string), nil
if hostname, ok := val.(string); err == nil && ok {
return hostname, nil
}
if _, ok := val.(struct{}); err == nil && ok {
return "", errNotFound
}
if err == gcache.NotFoundKeyError {
// We trigger a asynchronous reverse resolution when not cached
Expand All @@ -52,20 +58,22 @@ func (r *ReverseResolver) Get(address string) (string, error) {
default:
}
}
return "", err
return "", errNotFound
}

func (r *ReverseResolver) loop() {
for request := range r.addresses {
<-r.Throttle // rate limit our DNS resolutions
// and check if the answer is already in the cache
// check if the answer is already in the cache
if _, err := r.cache.Get(request); err == nil {
continue
}
<-r.Throttle // rate limit our DNS resolutions
names, err := r.Resolver(request)
if err == nil && len(names) > 0 {
name := strings.TrimRight(names[0], ".")
r.cache.Set(request, name)
} else {
r.cache.Set(request, struct{}{})
}
}
}
Expand Down

0 comments on commit f5d6872

Please sign in to comment.