Skip to content

Commit

Permalink
New asynchronous, caching DNS resolver for reverse resolutions
Browse files Browse the repository at this point in the history
Add nodes for the remote side of connections iff we have a DNS reverse resolution for the IP.
  • Loading branch information
inercia committed Sep 1, 2015
1 parent e867d16 commit 5bcceb2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
15 changes: 14 additions & 1 deletion probe/endpoint/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

// Node metadata keys.
const (
Name = "name"
Addr = "addr" // typically IPv4
Port = "port"
)
Expand All @@ -26,6 +27,7 @@ type Reporter struct {
includeNAT bool
conntracker *Conntracker
natmapper *natmapper
revResolver *ReverseResolver
}

// SpyDuration is an exported prometheus metric
Expand Down Expand Up @@ -64,12 +66,16 @@ func NewReporter(hostID, hostName string, includeProcesses bool, useConntrack bo
log.Printf("Failed to start natMapper: %v", err)
}
}

revRes := NewReverseResolver(rAddrCacheLen)

return &Reporter{
hostID: hostID,
hostName: hostName,
includeProcesses: includeProcesses,
conntracker: conntracker,
natmapper: natmapper,
revResolver: revRes,
}
}

Expand Down Expand Up @@ -132,6 +138,11 @@ func (r *Reporter) addConnection(rpt *report.Report, localAddr, remoteAddr strin

// Update address topology
{
revRemoteAddr := ""
if r, err := r.revResolver.Get(remoteAddr); err != nil {
revRemoteAddr = r
}

var (
localAddressNodeID = report.MakeAddressNodeID(r.hostID, localAddr)
remoteAddressNodeID = report.MakeAddressNodeID(r.hostID, remoteAddr)
Expand All @@ -143,7 +154,8 @@ func (r *Reporter) addConnection(rpt *report.Report, localAddr, remoteAddr strin
report.HostNodeID: hostNodeID,
})
remoteNode = report.MakeNodeMetadataWith(map[string]string{
Addr: remoteAddr,
"name": revRemoteAddr,
Addr: remoteAddr,
})
)

Expand All @@ -157,6 +169,7 @@ func (r *Reporter) addConnection(rpt *report.Report, localAddr, remoteAddr strin

rpt.Address = rpt.Address.WithNode(localAddressNodeID, localNode)
rpt.Address = rpt.Address.WithNode(remoteAddressNodeID, remoteNode)

countTCPConnection(rpt.Address.EdgeMetadatas, edgeID)
}

Expand Down
2 changes: 1 addition & 1 deletion probe/endpoint/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var (
LocalAddress: fixLocalAddress,
LocalPort: fixLocalPort,
RemoteAddress: fixRemoteAddress,
RemotePort: fixRemotePortB,
RemotePort: fixRemotePort,
Proc: procspy.Proc{
PID: fixProcessPID,
Name: fixProcessName,
Expand Down
69 changes: 69 additions & 0 deletions probe/endpoint/resolver.go
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
// 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)
}

0 comments on commit 5bcceb2

Please sign in to comment.