Skip to content

Commit 7301a31

Browse files
authored
c2p: add random number to xDS node ID in google-c2p resolver (#4519)
1 parent d30e2c9 commit 7301a31

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

internal/grpcrand/grpcrand.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,30 @@ var (
3131
mu sync.Mutex
3232
)
3333

34+
// Int implements rand.Int on the grpcrand global source.
35+
func Int() int {
36+
mu.Lock()
37+
defer mu.Unlock()
38+
return r.Int()
39+
}
40+
3441
// Int63n implements rand.Int63n on the grpcrand global source.
3542
func Int63n(n int64) int64 {
3643
mu.Lock()
37-
res := r.Int63n(n)
38-
mu.Unlock()
39-
return res
44+
defer mu.Unlock()
45+
return r.Int63n(n)
4046
}
4147

4248
// Intn implements rand.Intn on the grpcrand global source.
4349
func Intn(n int) int {
4450
mu.Lock()
45-
res := r.Intn(n)
46-
mu.Unlock()
47-
return res
51+
defer mu.Unlock()
52+
return r.Intn(n)
4853
}
4954

5055
// Float64 implements rand.Float64 on the grpcrand global source.
5156
func Float64() float64 {
5257
mu.Lock()
53-
res := r.Float64()
54-
mu.Unlock()
55-
return res
58+
defer mu.Unlock()
59+
return r.Float64()
5660
}

xds/googledirectpath/googlec2p.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"google.golang.org/grpc/grpclog"
3636
"google.golang.org/grpc/internal/googlecloud"
3737
internalgrpclog "google.golang.org/grpc/internal/grpclog"
38+
"google.golang.org/grpc/internal/grpcrand"
3839
"google.golang.org/grpc/internal/xds/env"
3940
"google.golang.org/grpc/resolver"
4041
_ "google.golang.org/grpc/xds" // To register xds resolvers and balancers.
@@ -152,13 +153,15 @@ var ipv6EnabledMetadata = &structpb.Struct{
152153
},
153154
}
154155

156+
var id = fmt.Sprintf("C2P-%d", grpcrand.Int())
157+
155158
// newNode makes a copy of defaultNode, and populate it's Metadata and
156159
// Locality fields.
157160
func newNode(zone string, ipv6Capable bool) *v3corepb.Node {
158161
ret := &v3corepb.Node{
159162
// Not all required fields are set in defaultNote. Metadata will be set
160163
// if ipv6 is enabled. Locality will be set to the value from metadata.
161-
Id: "C2P",
164+
Id: id,
162165
UserAgentName: gRPCUserAgentName,
163166
UserAgentVersionType: &v3corepb.Node_UserAgentVersion{UserAgentVersion: grpc.Version},
164167
ClientFeatures: []string{clientFeatureNoOverprovisioning},

xds/googledirectpath/googlec2p_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func TestBuildXDS(t *testing.T) {
194194
}
195195

196196
wantNode := &v3corepb.Node{
197-
Id: "C2P",
197+
Id: id,
198198
Metadata: nil,
199199
Locality: &v3corepb.Locality{Zone: testZone},
200200
UserAgentName: gRPCUserAgentName,

0 commit comments

Comments
 (0)