Skip to content

Commit 44475a5

Browse files
authored
xds/resolver: minor cleanup in the config selector implementation (#8609)
This PR injects the dependencies of `configSelector` at creation time, instead of passing a reference to the `xdsResolver` and having the former directly access fields from the latter. RELEASE NOTES: none
1 parent 51b15ad commit 44475a5

File tree

3 files changed

+16
-22
lines changed

3 files changed

+16
-22
lines changed

internal/xds/resolver/serviceconfig.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,11 @@ func (cs *erroringConfigSelector) SelectConfig(iresolver.RPCInfo) (*iresolver.RP
151151
func (cs *erroringConfigSelector) stop() {}
152152

153153
type configSelector struct {
154-
r *xdsResolver
155-
xdsNodeID string
154+
channelID uint64 // Static hash when hash policy is HashPolicyTypeChannelID
155+
xdsNodeID string // xDS node ID, for annotating errors.
156+
sendNewServiceConfig func() // Function to send a new service config to gRPC.
157+
158+
// Configuration received from the xDS management server.
156159
virtualHost virtualHost
157160
routes []route
158161
clusters map[string]*clusterInfo
@@ -214,9 +217,7 @@ func (cs *configSelector) SelectConfig(rpcInfo iresolver.RPCInfo) (*iresolver.RP
214217
if v := atomic.AddInt32(ref, -1); v == 0 {
215218
// This entry will be removed from activeClusters when
216219
// producing the service config for the empty update.
217-
cs.r.serializer.TrySchedule(func(context.Context) {
218-
cs.r.onClusterRefDownToZero()
219-
})
220+
cs.sendNewServiceConfig()
220221
}
221222
},
222223
Interceptor: interceptor,
@@ -281,7 +282,7 @@ func (cs *configSelector) generateHash(rpcInfo iresolver.RPCInfo, hashPolicies [
281282
generatedPolicyHash = true
282283
case xdsresource.HashPolicyTypeChannelID:
283284
// Use the static channel ID as the hash for this policy.
284-
policyHash = cs.r.channelID
285+
policyHash = cs.channelID
285286
generatedHash = true
286287
generatedPolicyHash = true
287288
}
@@ -358,9 +359,7 @@ func (cs *configSelector) stop() {
358359
// selector; we need another update to delete clusters from the config (if
359360
// we don't have another update pending already).
360361
if needUpdate {
361-
cs.r.serializer.TrySchedule(func(context.Context) {
362-
cs.r.onClusterRefDownToZero()
363-
})
362+
cs.sendNewServiceConfig()
364363
}
365364
}
366365

internal/xds/resolver/serviceconfig_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"google.golang.org/grpc/internal/grpctest"
3030
"google.golang.org/grpc/internal/grpcutil"
3131
iresolver "google.golang.org/grpc/internal/resolver"
32-
"google.golang.org/grpc/internal/testutils"
3332
_ "google.golang.org/grpc/internal/xds/balancer/cdsbalancer" // To parse LB config
3433
"google.golang.org/grpc/internal/xds/xdsclient/xdsresource"
3534
"google.golang.org/grpc/metadata"
@@ -64,12 +63,8 @@ func (s) TestPruneActiveClusters(t *testing.T) {
6463

6564
func (s) TestGenerateRequestHash(t *testing.T) {
6665
const channelID = 12378921
67-
cs := &configSelector{
68-
r: &xdsResolver{
69-
cc: &testutils.ResolverClientConn{Logger: t},
70-
channelID: channelID,
71-
},
72-
}
66+
cs := &configSelector{channelID: channelID}
67+
7368
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
7469
defer cancel()
7570
tests := []struct {

internal/xds/resolver/xds_resolver.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,13 @@ func (r *xdsResolver) sendNewServiceConfig(cs stoppableConfigSelector) bool {
330330
// Only executed in the context of a serializer callback.
331331
func (r *xdsResolver) newConfigSelector() *configSelector {
332332
cs := &configSelector{
333-
r: r,
333+
channelID: r.channelID,
334334
xdsNodeID: r.xdsClient.BootstrapConfig().Node().GetId(),
335+
sendNewServiceConfig: func() {
336+
r.serializer.TrySchedule(func(context.Context) {
337+
r.sendNewServiceConfig(r.curConfigSelector)
338+
})
339+
},
335340
virtualHost: virtualHost{
336341
httpFilterConfigOverride: r.currentVirtualHost.HTTPFilterConfigOverride,
337342
retryConfig: r.currentVirtualHost.RetryConfig,
@@ -607,8 +612,3 @@ func (r *xdsResolver) onRouteConfigResourceError(name string, err error) {
607612
}
608613
r.onResourceError(err)
609614
}
610-
611-
// Only executed in the context of a serializer callback.
612-
func (r *xdsResolver) onClusterRefDownToZero() {
613-
r.sendNewServiceConfig(r.curConfigSelector)
614-
}

0 commit comments

Comments
 (0)