Skip to content

Commit 32b53fd

Browse files
Add MinDNSResolutionRate Option
1 parent 5ccf176 commit 32b53fd

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

internal/resolver/dns/dns_resolver.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ import (
4141
"google.golang.org/grpc/serviceconfig"
4242
)
4343

44-
// EnableSRVLookups controls whether the DNS resolver attempts to fetch gRPCLB
45-
// addresses from SRV records. Must not be changed after init time.
46-
var EnableSRVLookups = false
44+
var (
45+
// EnableSRVLookups controls whether the DNS resolver attempts to fetch gRPCLB
46+
// addresses from SRV records. Must not be changed after init time.
47+
EnableSRVLookups = false
48+
49+
// MinResolutionRate is the minimum rate at which re-resolutions are
50+
// allowed. This helps to prevent excessive re-resolution.
51+
MinResolutionRate = 30 * time.Second
52+
)
4753

4854
var logger = grpclog.Component("dns")
4955

@@ -201,7 +207,7 @@ func (d *dnsResolver) watcher() {
201207
// Success resolving, wait for the next ResolveNow. However, also wait 30
202208
// seconds at the very least to prevent constantly re-resolving.
203209
backoffIndex = 1
204-
waitTime = internal.MinResolutionRate
210+
waitTime = MinResolutionRate
205211
select {
206212
case <-d.ctx.Done():
207213
return

internal/resolver/dns/dns_resolver_test.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ func overrideNetResolver(t *testing.T, r *testNetResolver) {
6969

7070
// Override the DNS Min Res Rate used by the resolver.
7171
func overrideResolutionRate(t *testing.T, d time.Duration) {
72-
origMinResRate := dnsinternal.MinResolutionRate
73-
dnsinternal.MinResolutionRate = d
74-
t.Cleanup(func() { dnsinternal.MinResolutionRate = origMinResRate })
72+
origMinResRate := dns.MinResolutionRate
73+
dns.MinResolutionRate = d
74+
t.Cleanup(func() { dns.MinResolutionRate = origMinResRate })
7575
}
7676

7777
// Override the timer used by the DNS resolver to fire after a duration of d.
@@ -1215,3 +1215,35 @@ func (s) TestReportError(t *testing.T) {
12151215
}
12161216
}
12171217
}
1218+
1219+
// Test verifies that changing [MinResolutionRate] variable correctly effects
1220+
// the resolution behaviour
1221+
func (s) TestMinResolutionRate(t *testing.T) {
1222+
const target = "foo.bar.com"
1223+
1224+
overrideResolutionRate(t, 1*time.Millisecond)
1225+
tr := &testNetResolver{
1226+
hostLookupTable: map[string][]string{
1227+
"foo.bar.com": {"1.2.3.4", "5.6.7.8"},
1228+
},
1229+
txtLookupTable: map[string][]string{
1230+
"_grpc_config.foo.bar.com": txtRecordServiceConfig(txtRecordGood),
1231+
},
1232+
}
1233+
overrideNetResolver(t, tr)
1234+
1235+
r, stateCh, _ := buildResolverWithTestClientConn(t, target)
1236+
1237+
wantAddrs := []resolver.Address{{Addr: "1.2.3.4" + colonDefaultPort}, {Addr: "5.6.7.8" + colonDefaultPort}}
1238+
wantSC := scJSON
1239+
1240+
for i := 0; i < 5; i++ {
1241+
// set context timeout slightly higher than the resolution rate to make sure resolutions
1242+
// happen successfully
1243+
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
1244+
defer cancel()
1245+
1246+
verifyUpdateFromResolver(ctx, t, stateCh, wantAddrs, nil, wantSC)
1247+
r.ResolveNow(resolver.ResolveNowOptions{})
1248+
}
1249+
}

internal/resolver/dns/internal/internal.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ var (
5050

5151
// The following vars are overridden from tests.
5252
var (
53-
// MinResolutionRate is the minimum rate at which re-resolutions are
54-
// allowed. This helps to prevent excessive re-resolution.
55-
MinResolutionRate = 30 * time.Second
56-
5753
// TimeAfterFunc is used by the DNS resolver to wait for the given duration
5854
// to elapse. In non-test code, this is implemented by time.After. In test
5955
// code, this can be used to control the amount of time the resolver is

resolver/dns/dns_resolver.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
package dns
2525

2626
import (
27+
"time"
28+
2729
"google.golang.org/grpc/internal/resolver/dns"
2830
"google.golang.org/grpc/resolver"
2931
)
@@ -34,3 +36,12 @@ import (
3436
func NewBuilder() resolver.Builder {
3537
return dns.NewBuilder()
3638
}
39+
40+
// SetMinResolutionRate sets the default minimum rate at which DNS re-resolutions are
41+
// allowed. This helps to prevent excessive re-resolution.
42+
//
43+
// Using this option overwrites the default [MinResolutionRate] specified
44+
// in the internal dns resolver package.
45+
func SetMinResolutionRate(d time.Duration) {
46+
dns.MinResolutionRate = d
47+
}

0 commit comments

Comments
 (0)