From be422e83017854072975b91a0e76cacd228284c1 Mon Sep 17 00:00:00 2001 From: Michael Demmer Date: Wed, 24 Jul 2024 17:18:46 +0200 Subject: [PATCH] add a lock for safety --- go/vt/vtgateproxy/discovery.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/go/vt/vtgateproxy/discovery.go b/go/vt/vtgateproxy/discovery.go index 7a5df534e96..8bef891b485 100644 --- a/go/vt/vtgateproxy/discovery.go +++ b/go/vt/vtgateproxy/discovery.go @@ -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) {} @@ -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)})