🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Fix race condition
Deluand046c180bf2b
diff --git a/utils/hasher/hasher.go b/utils/hasher/hasher.goindex 7c5b0e1ae..b07c17c31 100644--- a/utils/hasher/hasher.go+++ b/utils/hasher/hasher.go@@ -3,6 +3,7 @@ package hasherimport ("hash/maphash""strconv"+ "sync""github.com/navidrome/navidrome/utils/random")@@ -23,6 +24,7 @@ func HashFunc() func(id, str string) uint64 {type Hasher struct {seeds map[string]string+ mutex sync.RWMutexhashSeed maphash.Seed}@@ -35,6 +37,8 @@ func NewHasher() *Hasher {// SetSeed sets a seed for the given idfunc (h *Hasher) SetSeed(id string, seed string) {+ h.mutex.Lock()+ defer h.mutex.Unlock()h.seeds[id] = seed}@@ -45,16 +49,17 @@ func (h *Hasher) Reseed(id string) {func (h *Hasher) reseed(id string) string {seed := strconv.FormatUint(random.Uint64(), 36)- h.seeds[id] = seed+ h.SetSeed(id, seed)return seed}// HashFunc returns a function that hashes a string using the seed for the given idfunc (h *Hasher) HashFunc() func(id, str string) uint64 {return func(id, str string) uint64 {- var seed string- var ok bool- if seed, ok = h.seeds[id]; !ok {+ h.mutex.RLock()+ seed, ok := h.seeds[id]+ h.mutex.RUnlock()+ if !ok {seed = h.reseed(id)}diff --git a/utils/hasher/hasher_test.go b/utils/hasher/hasher_test.goindex 1d201e3d6..30cda3d05 100644--- a/utils/hasher/hasher_test.go+++ b/utils/hasher/hasher_test.go@@ -1,6 +1,7 @@package hasher_testimport (+ "strconv""testing""github.com/navidrome/navidrome/utils/hasher"@@ -54,4 +55,14 @@ var _ = Describe("HashFunc", func() {hasher.SetSeed(id, "original_seed")Expect(sum).To(Equal(hashFunc(id, input)))})++ It("does not cause race conditions", func() {+ for i := 0; i < 1000; i++ {+ go func() {+ hashFunc := hasher.HashFunc()+ sum := hashFunc(strconv.Itoa(i), input)+ Expect(sum).ToNot(BeZero())+ }()+ }+ })})