fix: replace mutex with atomic (#20762)

This commit is contained in:
Artur Melanchyk 2024-12-13 15:02:46 +01:00 committed by GitHub
parent 86d02b17cf
commit 2b008c598b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -29,6 +29,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"time" "time"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
@ -182,8 +183,7 @@ func (o *listPathOptions) gatherResults(ctx context.Context, in <-chan metaCache
resultsDone := make(chan metaCacheEntriesSorted) resultsDone := make(chan metaCacheEntriesSorted)
// Copy so we can mutate // Copy so we can mutate
resCh := resultsDone resCh := resultsDone
var done bool var done atomic.Bool
var mu sync.Mutex
resErr := io.EOF resErr := io.EOF
go func() { go func() {
@ -194,9 +194,7 @@ func (o *listPathOptions) gatherResults(ctx context.Context, in <-chan metaCache
// past limit // past limit
continue continue
} }
mu.Lock() returned = done.Load()
returned = done
mu.Unlock()
if returned { if returned {
resCh = nil resCh = nil
continue continue
@ -250,9 +248,7 @@ func (o *listPathOptions) gatherResults(ctx context.Context, in <-chan metaCache
return func() (metaCacheEntriesSorted, error) { return func() (metaCacheEntriesSorted, error) {
select { select {
case <-ctx.Done(): case <-ctx.Done():
mu.Lock() done.Store(true)
done = true
mu.Unlock()
return metaCacheEntriesSorted{}, ctx.Err() return metaCacheEntriesSorted{}, ctx.Err()
case r := <-resultsDone: case r := <-resultsDone:
return r, resErr return r, resErr