Skip to content

Commit

Permalink
refactor(mxresolv/mxresolv.go): adding DefaultResolver to the package
Browse files Browse the repository at this point in the history
  • Loading branch information
rneillj committed Mar 3, 2022
1 parent 3a97a67 commit 52f5b91
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
14 changes: 5 additions & 9 deletions mxresolv/mxresolv.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ var (

// It is modified only in tests to make them deterministic.
shuffle = true

DefaultResolver = net.DefaultResolver
)

func init() {
Expand All @@ -39,13 +41,7 @@ func init() {
// does not have explicit MX records, and its A record is returned instead.
//
// It uses an LRU cache with a timeout to reduce the number of network requests.
func Lookup(ctx context.Context, hostname string, r *net.Resolver) ([]string, bool, error) {
var resolver *net.Resolver
if r == nil {
resolver = net.DefaultResolver
} else {
resolver = r
}
func Lookup(ctx context.Context, hostname string) ([]string, bool, error) {
if cachedVal, ok := lookupResultCache.Get(hostname); ok {
lookupResult := cachedVal.(lookupResult)
return lookupResult.mxHosts, lookupResult.implicit, lookupResult.err
Expand All @@ -54,15 +50,15 @@ func Lookup(ctx context.Context, hostname string, r *net.Resolver) ([]string, bo
if err != nil {
return nil, false, errors.Wrap(err, "invalid hostname")
}
mxRecords, err := resolver.LookupMX(ctx, asciiHostname)
mxRecords, err := DefaultResolver.LookupMX(ctx, asciiHostname)
if err != nil {
var timeouter interface{ Timeout() bool }
if errors.As(err, &timeouter) && timeouter.Timeout() {
return nil, false, errors.WithStack(err)
}
var netDNSError *net.DNSError
if errors.As(err, &netDNSError) && netDNSError.Err == "no such host" {
if _, err := resolver.LookupIPAddr(ctx, asciiHostname); err != nil {
if _, err := DefaultResolver.LookupIPAddr(ctx, asciiHostname); err != nil {
return cacheAndReturn(hostname, nil, false, errors.WithStack(err))
}
return cacheAndReturn(hostname, []string{asciiHostname}, true, nil)
Expand Down
12 changes: 6 additions & 6 deletions mxresolv/mxresolv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestLookup(t *testing.T) {
fmt.Printf("Test case #%d: %s, %s\n", i, tc.inDomainName, tc.desc)
// When
ctx, cancel := context.WithTimeout(context.Background(), 3*clock.Second)
mxHosts, explictMX, err := Lookup(ctx, tc.inDomainName, nil)
mxHosts, explictMX, err := Lookup(ctx, tc.inDomainName)
cancel()
// Then
assert.NoError(t, err)
Expand All @@ -67,7 +67,7 @@ func TestLookup(t *testing.T) {

// The second lookup returns the cached result, that only shows on the
// coverage report.
mxHosts, explictMX, err = Lookup(ctx, tc.inDomainName, nil)
mxHosts, explictMX, err = Lookup(ctx, tc.inDomainName)
assert.NoError(t, err)
assert.Equal(t, tc.outMXHosts, mxHosts)
assert.Equal(t, tc.outImplicitMX, explictMX)
Expand Down Expand Up @@ -96,14 +96,14 @@ func TestLookupError(t *testing.T) {
fmt.Printf("Test case #%d: %s, %s\n", i, tc.inHostname, tc.desc)
// When
ctx, cancel := context.WithTimeout(context.Background(), 3*clock.Second)
_, _, err := Lookup(ctx, tc.inHostname, nil)
_, _, err := Lookup(ctx, tc.inHostname)
cancel()
// Then
assert.Regexp(t, regexp.MustCompile(tc.outError), err.Error())

// The second lookup returns the cached result, that only shows on the
// coverage report.
_, _, err = Lookup(ctx, tc.inHostname, nil)
_, _, err = Lookup(ctx, tc.inHostname)
assert.Regexp(t, regexp.MustCompile(tc.outError), err.Error())
}
}
Expand All @@ -121,10 +121,10 @@ func TestLookupShuffle(t *testing.T) {
// When
ctx, cancel := context.WithTimeout(context.Background(), 3*clock.Second)
defer cancel()
shuffle1, _, err := Lookup(ctx, "test-mx.definbox.com", nil)
shuffle1, _, err := Lookup(ctx, "test-mx.definbox.com")
assert.NoError(t, err)
resetCache()
shuffle2, _, err := Lookup(ctx, "test-mx.definbox.com", nil)
shuffle2, _, err := Lookup(ctx, "test-mx.definbox.com")
assert.NoError(t, err)

// Then
Expand Down

0 comments on commit 52f5b91

Please sign in to comment.