-
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.
- Loading branch information
Showing
3 changed files
with
95 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package endpoint | ||
|
||
import ( | ||
"log" | ||
"net" | ||
"time" | ||
|
||
"github.com/bluele/gcache" | ||
) | ||
|
||
const RAddrCacheLen = 500 | ||
|
||
const raddrBacklog = 1000 | ||
const rAddrCacheExpiration = 30 * time.Minute | ||
|
||
type reverseResolver struct { | ||
addresses chan string | ||
cache gcache.Cache | ||
} | ||
|
||
// newReverseResolver starts a new reverse resolver that | ||
// performs reverse resolutions and caches the result. | ||
func NewReverseResolver(cacheLen int) *reverseResolver { | ||
r := reverseResolver{ | ||
addresses: make(chan string, raddrBacklog), | ||
cache: gcache.New(cacheLen).LRU().Expiration(rAddrCacheExpiration).Build(), | ||
} | ||
|
||
go func() { | ||
rate := time.Second / 10 | ||
throttle := time.Tick(rate) | ||
for address := range r.addresses { | ||
<-throttle // rate limit our DNS resolutions | ||
// TODO: we should control "r.addresses" length, otherwise the Get'ers could block... | ||
names, err := net.LookupAddr(address) | ||
if err != nil { | ||
log.Printf("could not resolve address %s: %v", address, err) | ||
continue | ||
} | ||
if len(names) > 0 { | ||
r.cache.Set(address, names[0]) | ||
} | ||
} | ||
}() | ||
|
||
return &r | ||
} | ||
|
||
// Get the reverse resolution for a | ||
func (r *reverseResolver) Get(ip net.IP) (string, error) { | ||
address := ip.String() | ||
val, err := r.cache.Get(address) | ||
if err != nil { | ||
if err == gcache.NotFoundKeyError { | ||
// we trigger a asynchronous reverse resolution when not cached, but we return | ||
// immediately so the client does not block... | ||
r.addresses <- address | ||
} else { | ||
log.Printf("could not GET %s from cache: %v", address, err) | ||
} | ||
return "", err | ||
} | ||
return val.(string), nil | ||
} | ||
|
||
// 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