From 2fc9c7845c2fc6b9fca279c2c27550294029883b Mon Sep 17 00:00:00 2001 From: Michael Demmer Date: Wed, 24 Jul 2024 18:32:17 +0200 Subject: [PATCH] add a lock for safety (#467) --- 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)})