-
Notifications
You must be signed in to change notification settings - Fork 712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reverse resolutions for remote side of connections. #404
+173
−26
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,76 @@ | ||
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) | ||
|
||
// ReverseResolver is a caching, reverse resolver | ||
type ReverseResolver struct { | ||
addresses chan string | ||
cache gcache.Cache | ||
Throttle <-chan time.Time // Made public for mocking | ||
Resolver revResFunc | ||
} | ||
|
||
// NewReverseResolver starts a new reverse resolver that | ||
// performs reverse resolutions and caches the result. | ||
func NewReverseResolver() *ReverseResolver { | ||
r := ReverseResolver{ | ||
addresses: make(chan string, rAddrBacklog), | ||
cache: gcache.New(rAddrCacheLen).LRU().Expiration(rAddrCacheExpiration).Build(), | ||
Throttle: time.Tick(time.Second / 10), | ||
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) (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) loop() { | ||
for request := range r.addresses { | ||
<-r.Throttle // rate limit our DNS resolutions | ||
// and check if the answer is already in the cache | ||
if _, err := r.cache.Get(request); err == nil { | ||
continue | ||
} | ||
names, err := r.Resolver(request) | ||
if err == nil && len(names) > 0 { | ||
name := strings.TrimRight(names[0], ".") | ||
r.cache.Set(request, name) | ||
} | ||
} | ||
} | ||
|
||
// 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,38 @@ | ||
package endpoint_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
"time" | ||
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong. |
||
|
||
. "github.com/weaveworks/scope/probe/endpoint" | ||
"github.com/weaveworks/scope/test" | ||
) | ||
|
||
func TestReverseResolver(t *testing.T) { | ||
tests := map[string]string{ | ||
"1.2.3.4": "test.domain.name", | ||
"4.3.2.1": "im.a.little.tea.pot", | ||
This comment was marked as abuse.
Sorry, something went wrong. |
||
} | ||
|
||
revRes := NewReverseResolver() | ||
defer revRes.Stop() | ||
|
||
// 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") | ||
} | ||
|
||
// Up the rate limit so the test runs faster | ||
revRes.Throttle = time.Tick(time.Millisecond) | ||
|
||
for ip, hostname := range tests { | ||
test.Poll(t, 100*time.Millisecond, hostname, func() interface{} { | ||
result, _ := revRes.Get(ip) | ||
return result | ||
}) | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as abuse.
Sorry, something went wrong.