-
Notifications
You must be signed in to change notification settings - Fork 712
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New asynchronous, caching DNS resolver for reverse resolutions
Add nodes for the remote side of connections iff we have a DNS reverse resolution for the IP. Unit test for the resolver
- Loading branch information
Showing
6 changed files
with
160 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package endpoint | ||
|
||
import ( | ||
"net" | ||
"strings" | ||
"time" | ||
|
||
"github.com/bluele/gcache" | ||
) | ||
|
||
const ( | ||
rAddrCacheLen = 500 // Default cache length | ||
rAddrBacklog = 1000 | ||
rAddrCacheExpiration = 30 * time.Minute | ||
) | ||
|
||
type revResFunc func(addr string) (names []string, err error) | ||
|
||
type revResRequest struct { | ||
address string | ||
done chan struct{} | ||
} | ||
|
||
// ReverseResolver is a caching, reverse resolver | ||
type reverseResolver struct { | ||
addresses chan revResRequest | ||
cache gcache.Cache | ||
resolver revResFunc | ||
} | ||
|
||
// NewReverseResolver starts a new reverse resolver that | ||
// performs reverse resolutions and caches the result. | ||
func newReverseResolver() *reverseResolver { | ||
r := reverseResolver{ | ||
addresses: make(chan revResRequest, rAddrBacklog), | ||
cache: gcache.New(rAddrCacheLen).LRU().Expiration(rAddrCacheExpiration).Build(), | ||
resolver: net.LookupAddr, | ||
} | ||
go r.loop() | ||
return &r | ||
} | ||
|
||
// Get the reverse resolution for an IP address if already in the cache, | ||
// a gcache.NotFoundKeyError error otherwise. | ||
// Note: it returns one of the possible names that can be obtained for that IP. | ||
func (r *reverseResolver) Get(address string, wait bool) (string, error) { | ||
val, err := r.cache.Get(address) | ||
if err == nil { | ||
return val.(string), nil | ||
} | ||
if err == gcache.NotFoundKeyError { | ||
request := revResRequest{address: address, done: make(chan struct{})} | ||
// we trigger a asynchronous reverse resolution when not cached | ||
select { | ||
case r.addresses <- request: | ||
if wait { | ||
<-request.done | ||
} | ||
default: | ||
} | ||
} | ||
return "", err | ||
} | ||
|
||
func (r *reverseResolver) loop() { | ||
throttle := time.Tick(time.Second / 10) | ||
for request := range r.addresses { | ||
<-throttle // rate limit our DNS resolutions | ||
// and check if the answer is already in the cache | ||
if _, err := r.cache.Get(request.address); err == nil { | ||
continue | ||
} | ||
names, err := r.resolver(request.address) | ||
if err == nil && len(names) > 0 { | ||
name := strings.TrimRight(names[0], ".") | ||
r.cache.Set(request.address, name) | ||
} | ||
close(request.done) | ||
} | ||
} | ||
|
||
// Stop the async reverse resolver | ||
func (r *reverseResolver) Stop() { | ||
close(r.addresses) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package endpoint | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
) | ||
|
||
func TestReverseResolver(t *testing.T) { | ||
tests := map[string]string{ | ||
"8.8.8.8": "google-public-dns-a.google.com", | ||
"8.8.4.4": "google-public-dns-b.google.com", | ||
} | ||
|
||
revRes := newReverseResolver() | ||
|
||
// use a mocked resolver function | ||
revRes.resolver = func(addr string) (names []string, err error) { | ||
if name, ok := tests[addr]; ok { | ||
return []string{name}, nil | ||
} | ||
return []string{}, errors.New("invalid IP") | ||
} | ||
|
||
// first time: no names are returned for our reverse resolutions | ||
for ip, _ := range tests { | ||
if have, err := revRes.Get(ip, true); have != "" || err == nil { | ||
t.Errorf("we didn't get an error, or the cache was not empty, when trying to resolve '%q'", ip) | ||
} | ||
} | ||
|
||
// so, if we check again these IPs, we should have the names now | ||
for ip, want := range tests { | ||
have, err := revRes.Get(ip, true) | ||
if err != nil { | ||
t.Errorf("%s: %v", ip, err) | ||
} | ||
if want != have { | ||
t.Errorf("%s: want %q, have %q", ip, want, have) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters