-
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
4 changed files
with
100 additions
and
13 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,71 @@ | ||
package endpoint | ||
|
||
import ( | ||
"log" | ||
"net" | ||
"time" | ||
|
||
"github.com/bluele/gcache" | ||
) | ||
|
||
// Default cache length | ||
const RAddrCacheLen = 500 | ||
|
||
const raddrBacklog = 1000 | ||
const rAddrCacheExpiration = 30 * time.Minute | ||
|
||
// ReverseResolver is a caching, reverse resolver | ||
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 | ||
names, err := net.LookupAddr(address) | ||
if err != nil { | ||
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 | ||
select { | ||
case r.addresses <- address: | ||
default: | ||
} | ||
} 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