@@ -24,6 +24,7 @@ import (
24
24
"math/rand"
25
25
"os"
26
26
"sort"
27
+ "sync"
27
28
"time"
28
29
29
30
"google.golang.org/grpc/resolver"
@@ -64,6 +65,7 @@ type JSONGateResolverBuilder struct {
64
65
affinityField string
65
66
affinityValue string
66
67
68
+ mu sync.RWMutex
67
69
targets map [string ][]targetHost
68
70
resolvers []* JSONGateResolver
69
71
@@ -83,7 +85,6 @@ type JSONGateResolver struct {
83
85
target resolver.Target
84
86
clientConn resolver.ClientConn
85
87
poolType string
86
- affinity string
87
88
}
88
89
89
90
var (
@@ -156,7 +157,7 @@ func (b *JSONGateResolverBuilder) start() error {
156
157
157
158
parseCount .Add ("changed" , 1 )
158
159
159
- log .Infof ("loaded targets, pool types %v, affinity groups %v" , poolTypes , affinityTypes )
160
+ log .Infof ("loaded targets, pool types %v, affinity %s, groups %v" , poolTypes , * affinityValue , affinityTypes )
160
161
161
162
// Start a config watcher
162
163
b .ticker = time .NewTicker (1 * time .Second )
@@ -292,17 +293,30 @@ func (b *JSONGateResolverBuilder) parse() (bool, error) {
292
293
targetCount .Set (poolType , int64 (len (targets [poolType ])))
293
294
}
294
295
296
+ b .mu .Lock ()
295
297
b .targets = targets
298
+ b .mu .Unlock ()
296
299
297
300
return true , nil
298
301
}
299
302
300
- // Update the current list of hosts for the given resolver
301
- func (b * JSONGateResolverBuilder ) update (r * JSONGateResolver ) {
302
-
303
- log .V (100 ).Infof ("resolving target %s to %d connections\n " , r .target .URL .String (), * numConnections )
303
+ func (b * JSONGateResolverBuilder ) GetPools () []string {
304
+ b .mu .RLock ()
305
+ defer b .mu .RUnlock ()
306
+ var pools []string
307
+ for pool := range b .targets {
308
+ pools = append (pools , pool )
309
+ }
310
+ sort .Strings (pools )
311
+ return pools
312
+ }
304
313
305
- targets := b .targets [r .poolType ]
314
+ func (b * JSONGateResolverBuilder ) GetTargets (poolType string ) []targetHost {
315
+ // Copy the target slice
316
+ b .mu .RLock ()
317
+ targets := []targetHost {}
318
+ targets = append (targets , b .targets [poolType ]... )
319
+ b .mu .RUnlock ()
306
320
307
321
// Shuffle to ensure every host has a different order to iterate through, putting
308
322
// the affinity matching (e.g. same az) hosts at the front and the non-matching ones
@@ -315,7 +329,7 @@ func (b *JSONGateResolverBuilder) update(r *JSONGateResolver) {
315
329
for i := 0 ; i < n - 1 ; i ++ {
316
330
j := head + b .rand .Intn (tail - head + 1 )
317
331
318
- if r . affinity == "" || r . affinity == targets [j ].Affinity {
332
+ if * affinityField != "" && * affinityValue == targets [j ].Affinity {
319
333
targets [head ], targets [j ] = targets [j ], targets [head ]
320
334
head ++
321
335
} else {
@@ -324,6 +338,16 @@ func (b *JSONGateResolverBuilder) update(r *JSONGateResolver) {
324
338
}
325
339
}
326
340
341
+ return targets
342
+ }
343
+
344
+ // Update the current list of hosts for the given resolver
345
+ func (b * JSONGateResolverBuilder ) update (r * JSONGateResolver ) {
346
+
347
+ log .V (100 ).Infof ("resolving target %s to %d connections\n " , r .target .URL .String (), * numConnections )
348
+
349
+ targets := b .GetTargets (r .poolType )
350
+
327
351
var addrs []resolver.Address
328
352
for _ , target := range targets {
329
353
addrs = append (addrs , resolver.Address {Addr : target .Addr })
@@ -354,7 +378,6 @@ func (b *JSONGateResolverBuilder) Build(target resolver.Target, cc resolver.Clie
354
378
target : target ,
355
379
clientConn : cc ,
356
380
poolType : poolType ,
357
- affinity : b .affinityValue ,
358
381
}
359
382
360
383
b .update (r )
@@ -366,17 +389,17 @@ func (b *JSONGateResolverBuilder) Build(target resolver.Target, cc resolver.Clie
366
389
// debugTargets will return the builder's targets with a sorted slice of
367
390
// poolTypes for rendering debug output
368
391
func (b * JSONGateResolverBuilder ) debugTargets () any {
369
- var pools []string
392
+ pools := b .GetPools ()
393
+ targets := map [string ][]targetHost {}
370
394
for pool := range b .targets {
371
- pools = append ( pools , pool )
395
+ targets [ pool ] = b . GetTargets ( pool )
372
396
}
373
- sort .Strings (pools )
374
397
return struct {
375
398
Pools []string
376
399
Targets map [string ][]targetHost
377
400
}{
378
401
Pools : pools ,
379
- Targets : b . targets ,
402
+ Targets : targets ,
380
403
}
381
404
}
382
405
0 commit comments