update
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
This commit is contained in:
74
utils/hasher/hasher.go
Normal file
74
utils/hasher/hasher.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package hasher
|
||||
|
||||
import (
|
||||
"hash/maphash"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"github.com/navidrome/navidrome/utils/random"
|
||||
)
|
||||
|
||||
var instance = NewHasher()
|
||||
|
||||
func Reseed(id string) {
|
||||
instance.Reseed(id)
|
||||
}
|
||||
|
||||
func SetSeed(id string, seed string) {
|
||||
instance.SetSeed(id, seed)
|
||||
}
|
||||
|
||||
func CurrentSeed(id string) string {
|
||||
instance.mutex.RLock()
|
||||
defer instance.mutex.RUnlock()
|
||||
return instance.seeds[id]
|
||||
}
|
||||
|
||||
func HashFunc() func(id, str string) uint64 {
|
||||
return instance.HashFunc()
|
||||
}
|
||||
|
||||
type Hasher struct {
|
||||
seeds map[string]string
|
||||
mutex sync.RWMutex
|
||||
hashSeed maphash.Seed
|
||||
}
|
||||
|
||||
func NewHasher() *Hasher {
|
||||
h := new(Hasher)
|
||||
h.seeds = make(map[string]string)
|
||||
h.hashSeed = maphash.MakeSeed()
|
||||
return h
|
||||
}
|
||||
|
||||
// SetSeed sets a seed for the given id
|
||||
func (h *Hasher) SetSeed(id string, seed string) {
|
||||
h.mutex.Lock()
|
||||
defer h.mutex.Unlock()
|
||||
h.seeds[id] = seed
|
||||
}
|
||||
|
||||
// Reseed generates a new random seed for the given id
|
||||
func (h *Hasher) Reseed(id string) {
|
||||
_ = h.reseed(id)
|
||||
}
|
||||
|
||||
func (h *Hasher) reseed(id string) string {
|
||||
seed := strconv.FormatUint(random.Uint64(), 36)
|
||||
h.SetSeed(id, seed)
|
||||
return seed
|
||||
}
|
||||
|
||||
// HashFunc returns a function that hashes a string using the seed for the given id
|
||||
func (h *Hasher) HashFunc() func(id, str string) uint64 {
|
||||
return func(id, str string) uint64 {
|
||||
h.mutex.RLock()
|
||||
seed, ok := h.seeds[id]
|
||||
h.mutex.RUnlock()
|
||||
if !ok {
|
||||
seed = h.reseed(id)
|
||||
}
|
||||
|
||||
return maphash.Bytes(h.hashSeed, []byte(seed+str))
|
||||
}
|
||||
}
|
||||
68
utils/hasher/hasher_test.go
Normal file
68
utils/hasher/hasher_test.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package hasher_test
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/navidrome/navidrome/utils/hasher"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestHasher(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Hasher Suite")
|
||||
}
|
||||
|
||||
var _ = Describe("HashFunc", func() {
|
||||
const input = "123e4567e89b12d3a456426614174000"
|
||||
|
||||
It("hashes the input and returns the sum", func() {
|
||||
hashFunc := hasher.HashFunc()
|
||||
sum := hashFunc("1", input)
|
||||
Expect(sum > 0).To(BeTrue())
|
||||
})
|
||||
|
||||
It("hashes the input, reseeds and returns a different sum", func() {
|
||||
hashFunc := hasher.HashFunc()
|
||||
sum := hashFunc("1", input)
|
||||
hasher.Reseed("1")
|
||||
sum2 := hashFunc("1", input)
|
||||
Expect(sum).NotTo(Equal(sum2))
|
||||
})
|
||||
|
||||
It("keeps different hashes for different ids", func() {
|
||||
hashFunc := hasher.HashFunc()
|
||||
sum := hashFunc("1", input)
|
||||
sum2 := hashFunc("2", input)
|
||||
|
||||
Expect(sum).NotTo(Equal(sum2))
|
||||
|
||||
Expect(sum).To(Equal(hashFunc("1", input)))
|
||||
Expect(sum2).To(Equal(hashFunc("2", input)))
|
||||
})
|
||||
|
||||
It("keeps the same hash for the same id and seed", func() {
|
||||
id := "1"
|
||||
hashFunc := hasher.HashFunc()
|
||||
hasher.SetSeed(id, "original_seed")
|
||||
sum := hashFunc(id, input)
|
||||
Expect(sum).To(Equal(hashFunc(id, input)))
|
||||
|
||||
hasher.Reseed(id)
|
||||
Expect(sum).NotTo(Equal(hashFunc(id, input)))
|
||||
|
||||
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())
|
||||
}()
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user