From 2b008c598b3a2d7d51ce54dc22e5d46c35104faa Mon Sep 17 00:00:00 2001 From: Artur Melanchyk Date: Fri, 13 Dec 2024 15:02:46 +0100 Subject: [PATCH] fix: replace mutex with atomic (#20762) --- cmd/metacache-set.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/cmd/metacache-set.go b/cmd/metacache-set.go index 509d451da..91c31d320 100644 --- a/cmd/metacache-set.go +++ b/cmd/metacache-set.go @@ -29,6 +29,7 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "time" jsoniter "github.com/json-iterator/go" @@ -182,8 +183,7 @@ func (o *listPathOptions) gatherResults(ctx context.Context, in <-chan metaCache resultsDone := make(chan metaCacheEntriesSorted) // Copy so we can mutate resCh := resultsDone - var done bool - var mu sync.Mutex + var done atomic.Bool resErr := io.EOF go func() { @@ -194,9 +194,7 @@ func (o *listPathOptions) gatherResults(ctx context.Context, in <-chan metaCache // past limit continue } - mu.Lock() - returned = done - mu.Unlock() + returned = done.Load() if returned { resCh = nil continue @@ -250,9 +248,7 @@ func (o *listPathOptions) gatherResults(ctx context.Context, in <-chan metaCache return func() (metaCacheEntriesSorted, error) { select { case <-ctx.Done(): - mu.Lock() - done = true - mu.Unlock() + done.Store(true) return metaCacheEntriesSorted{}, ctx.Err() case r := <-resultsDone: return r, resErr