Make first WebUI random page stick

This commit is contained in:
Deluan
2024-05-19 12:35:30 -04:00
parent 653b4d97f9
commit 3977ef6e0f
6 changed files with 75 additions and 21 deletions
+37 -17
View File
@@ -1,6 +1,12 @@
package hasher
import "hash/maphash"
import (
"hash/maphash"
"math"
"strconv"
"github.com/navidrome/navidrome/utils/random"
)
var instance = NewHasher()
@@ -8,37 +14,51 @@ func Reseed(id string) {
instance.Reseed(id)
}
func SetSeed(id string, seed string) {
instance.SetSeed(id, seed)
}
func HashFunc() func(id, str string) uint64 {
return instance.HashFunc()
}
type hasher struct {
seeds map[string]maphash.Seed
type Hasher struct {
seeds map[string]string
hashSeed maphash.Seed
}
func NewHasher() *hasher {
h := new(hasher)
h.seeds = make(map[string]maphash.Seed)
func NewHasher() *Hasher {
h := new(Hasher)
h.seeds = make(map[string]string)
h.hashSeed = maphash.MakeSeed()
return h
}
// Reseed generates a new seed for the given id
func (h *hasher) Reseed(id string) {
h.seeds[id] = maphash.MakeSeed()
// SetSeed sets a seed for the given id
func (h *Hasher) SetSeed(id string, seed string) {
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.FormatInt(random.Int64(math.MaxInt64), 10)
h.seeds[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 {
func (h *Hasher) HashFunc() func(id, str string) uint64 {
return func(id, str string) uint64 {
var hash maphash.Hash
var seed maphash.Seed
var seed string
var ok bool
if seed, ok = h.seeds[id]; !ok {
seed = maphash.MakeSeed()
h.seeds[id] = seed
seed = h.reseed(id)
}
hash.SetSeed(seed)
_, _ = hash.WriteString(str)
return hash.Sum64()
return maphash.Bytes(h.hashSeed, []byte(seed+str))
}
}