-
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
98 additions
and
1 deletion.
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,69 @@ | ||
package endpoint | ||
|
||
import ( | ||
"net" | ||
"time" | ||
|
||
"github.com/bluele/gcache" | ||
) | ||
|
||
const ( | ||
rAddrCacheLen = 500 // Default cache length | ||
rAddrBacklog = 1000 | ||
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 r.run() | ||
return &r | ||
} | ||
|
||
// Get the reverse resolution for an IP address if already in the cache, gcache.NotFoundKeyError otherwise | ||
// Note: it returns one of the 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 err == gcache.NotFoundKeyError { | ||
// we trigger a asynchronous reverse resolution when not cached | ||
select { | ||
case r.addresses <- address: | ||
default: | ||
} | ||
} | ||
return "", err | ||
} | ||
|
||
func (r *ReverseResolver) run() { | ||
throttle := time.Tick(time.Second / 10) | ||
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]) | ||
} | ||
} | ||
} | ||
|
||
// 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