Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small improvements to reverse resolver #451

Merged
merged 1 commit into from
Sep 7, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

This comment was marked as abuse.

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