Skip to content

Commit

Permalink
add a lock for safety
Browse files Browse the repository at this point in the history
  • Loading branch information
demmer committed Jul 24, 2024
1 parent 6ee8654 commit 0248ccc
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions go/vt/vtgateproxy/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type JSONGateResolver struct {
clientConn resolver.ClientConn
poolType string
currentAddrs []resolver.Address
mu *sync.Mutex
}

func (r *JSONGateResolver) ResolveNow(o resolver.ResolveNowOptions) {}
Expand Down Expand Up @@ -398,6 +399,11 @@ func (b *JSONGateResolverBuilder) update(r *JSONGateResolver) error {

targets := b.getTargets(r.poolType)

// There should only ever be a single goroutine calling update on a given Resolver,
// but add a lock just in case to ensure that the r.currentAddrs are in fact synchronized
r.mu.Lock()
defer r.mu.Unlock()

var addrs []resolver.Address
for _, target := range targets {
addrs = append(addrs, resolver.Address{Addr: target.Addr, Attributes: attributes.New(PoolTypeAttr, r.poolType)})
Expand Down Expand Up @@ -437,6 +443,7 @@ func (b *JSONGateResolverBuilder) Build(target resolver.Target, cc resolver.Clie
target: target,
clientConn: cc,
poolType: poolType,
mu: &sync.Mutex{},
}

err := b.update(r)
Expand Down

0 comments on commit 0248ccc

Please sign in to comment.