@@ -58,15 +58,13 @@ func NewRandomPicker(maxNumOfEndpoints int) *RandomPicker {
58
58
return & RandomPicker {
59
59
typedName : plugins.TypedName {Type : RandomPickerType , Name : RandomPickerType },
60
60
maxNumOfEndpoints : maxNumOfEndpoints ,
61
- randomGenerator : rand .New (rand .NewSource (time .Now ().UnixNano ())),
62
61
}
63
62
}
64
63
65
64
// RandomPicker picks random pod(s) from the list of candidates.
66
65
type RandomPicker struct {
67
66
typedName plugins.TypedName
68
67
maxNumOfEndpoints int
69
- randomGenerator * rand.Rand // randomGenerator for randomly pick endpoint on tie-break
70
68
}
71
69
72
70
// WithName sets the name of the picker.
@@ -85,8 +83,13 @@ func (p *RandomPicker) Pick(ctx context.Context, _ *types.CycleState, scoredPods
85
83
log .FromContext (ctx ).V (logutil .DEBUG ).Info (fmt .Sprintf ("Selecting maximum '%d' pods from %d candidates randomly: %+v" , p .maxNumOfEndpoints ,
86
84
len (scoredPods ), scoredPods ))
87
85
86
+ // TODO: merge this with the logic in MaxScorePicker
87
+ // Rand package is not safe for concurrent use, so we create a new instance.
88
+ // Source: https://pkg.go.dev/math/rand#pkg-overview
89
+ randomGenerator := rand .New (rand .NewSource (time .Now ().UnixNano ()))
90
+
88
91
// Shuffle in-place
89
- p . randomGenerator .Shuffle (len (scoredPods ), func (i , j int ) {
92
+ randomGenerator .Shuffle (len (scoredPods ), func (i , j int ) {
90
93
scoredPods [i ], scoredPods [j ] = scoredPods [j ], scoredPods [i ]
91
94
})
92
95
0 commit comments