Skip to content

Commit

Permalink
Add MinDNSResolutionRate Option
Browse files Browse the repository at this point in the history
  • Loading branch information
HomayoonAlimohammadi committed Apr 5, 2024
1 parent c31cec3 commit 2765325
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
14 changes: 10 additions & 4 deletions internal/resolver/dns/dns_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ import (
"google.golang.org/grpc/serviceconfig"
)

// EnableSRVLookups controls whether the DNS resolver attempts to fetch gRPCLB
// addresses from SRV records. Must not be changed after init time.
var EnableSRVLookups = false
var (
// EnableSRVLookups controls whether the DNS resolver attempts to fetch gRPCLB
// addresses from SRV records. Must not be changed after init time.
EnableSRVLookups = false

// MinResolutionRate is the minimum rate at which re-resolutions are
// allowed. This helps to prevent excessive re-resolution.
MinResolutionRate = 30 * time.Second
)

// ResolvingTimeout specifies the maximum duration for a DNS resolution request.
// If the timeout expires before a response is received, the request will be canceled.
Expand Down Expand Up @@ -208,7 +214,7 @@ func (d *dnsResolver) watcher() {
// Success resolving, wait for the next ResolveNow. However, also wait 30
// seconds at the very least to prevent constantly re-resolving.
backoffIndex = 1
waitTime = internal.MinResolutionRate
waitTime = MinResolutionRate
select {
case <-d.ctx.Done():
return
Expand Down
38 changes: 35 additions & 3 deletions internal/resolver/dns/dns_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func overrideNetResolver(t *testing.T, r *testNetResolver) {

// Override the DNS Min Res Rate used by the resolver.
func overrideResolutionRate(t *testing.T, d time.Duration) {
origMinResRate := dnsinternal.MinResolutionRate
dnsinternal.MinResolutionRate = d
t.Cleanup(func() { dnsinternal.MinResolutionRate = origMinResRate })
origMinResRate := dns.MinResolutionRate
dns.MinResolutionRate = d
t.Cleanup(func() { dns.MinResolutionRate = origMinResRate })
}

// Override the timer used by the DNS resolver to fire after a duration of d.
Expand Down Expand Up @@ -1258,3 +1258,35 @@ func (s) TestResolveTimeout(t *testing.T) {
}
}
}

// Test verifies that changing [MinResolutionRate] variable correctly effects
// the resolution behaviour
func (s) TestMinResolutionRate(t *testing.T) {
const target = "foo.bar.com"

overrideResolutionRate(t, 1*time.Millisecond)
tr := &testNetResolver{
hostLookupTable: map[string][]string{
"foo.bar.com": {"1.2.3.4", "5.6.7.8"},
},
txtLookupTable: map[string][]string{
"_grpc_config.foo.bar.com": txtRecordServiceConfig(txtRecordGood),
},
}
overrideNetResolver(t, tr)

r, stateCh, _ := buildResolverWithTestClientConn(t, target)

wantAddrs := []resolver.Address{{Addr: "1.2.3.4" + colonDefaultPort}, {Addr: "5.6.7.8" + colonDefaultPort}}
wantSC := scJSON

for i := 0; i < 5; i++ {
// set context timeout slightly higher than the resolution rate to make sure resolutions
// happen successfully
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()

verifyUpdateFromResolver(ctx, t, stateCh, wantAddrs, nil, wantSC)
r.ResolveNow(resolver.ResolveNowOptions{})
}
}
4 changes: 0 additions & 4 deletions internal/resolver/dns/internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ var (

// The following vars are overridden from tests.
var (
// MinResolutionRate is the minimum rate at which re-resolutions are
// allowed. This helps to prevent excessive re-resolution.
MinResolutionRate = 30 * time.Second

// TimeAfterFunc is used by the DNS resolver to wait for the given duration
// to elapse. In non-test code, this is implemented by time.After. In test
// code, this can be used to control the amount of time the resolver is
Expand Down
9 changes: 9 additions & 0 deletions resolver/dns/dns_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,12 @@ func SetResolvingTimeout(timeout time.Duration) {
func NewBuilder() resolver.Builder {
return dns.NewBuilder()
}

// SetMinResolutionRate sets the default minimum rate at which DNS re-resolutions are
// allowed. This helps to prevent excessive re-resolution.
//
// Using this option overwrites the default [MinResolutionRate] specified
// in the internal dns resolver package.
func SetMinResolutionRate(d time.Duration) {
dns.MinResolutionRate = d
}

0 comments on commit 2765325

Please sign in to comment.