From 611af8eab36a1d119f49895a1a8bd995dc9dc347 Mon Sep 17 00:00:00 2001 From: chengshiwen Date: Sat, 13 Jul 2024 19:09:01 +0800 Subject: [PATCH] fix data race in meta client --- services/meta/client.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/services/meta/client.go b/services/meta/client.go index c4744d0..c3a2325 100644 --- a/services/meta/client.go +++ b/services/meta/client.go @@ -19,6 +19,7 @@ import ( "reflect" "sort" "sync" + "sync/atomic" "time" "github.com/gogo/protobuf/proto" @@ -246,8 +247,8 @@ func (c *Client) acquireLease(name string) (*Lease, error) { return nil, ErrServiceUnavailable } server := c.metaServers[0] - c.mu.RUnlock() url := fmt.Sprintf("%s/lease?name=%s&nodeid=%d", c.url(server), name, c.nodeID) + c.mu.RUnlock() resp, err := c.client.Get(url) if err != nil { @@ -1280,7 +1281,8 @@ func (c *Client) url(server string) string { } func (c *Client) retryUntilSnapshot(idx uint64) *Data { - printErr := true + var errPrint atomic.Value + errPrint.Store(true) currentServer := 0 for { // get the index to look from and the server to poll @@ -1313,12 +1315,12 @@ func (c *Client) retryUntilSnapshot(idx uint64) *Data { return data } - if printErr { + if errPrint.Load().(bool) { c.logger.Info("Failure getting snapshot", zap.String("server", server), zap.Error(err)) - printErr = false + errPrint.Store(false) go func() { <-time.After(30 * errSleep) - printErr = true + errPrint.Store(true) }() } time.Sleep(errSleep)