github.com/vugu/vugu@v0.3.6-0.20240430171613-3f6f402e014b/comp-key.go (about) 1 package vugu 2 3 import ( 4 "math/rand" 5 "time" 6 7 "github.com/vugu/xxhash" 8 ) 9 10 // MakeCompKeyID forms a value for CompKey.ID from the given time the uint32 you provide for the lower 32 bits. 11 func MakeCompKeyID(t time.Time, data uint32) uint64 { 12 var ret = uint64(t.Unix()) << 32 13 ret |= uint64(data) 14 return ret 15 } 16 17 // MakeCompKeyIDTimeHash forms a value for CompKey.ID from the given time and a hash of the bytes you provide. 18 func MakeCompKeyIDTimeHash(t time.Time, b []byte) uint64 { 19 h := xxhash.New() 20 _, err := h.Write(b) 21 if err != nil { 22 panic(err) 23 } 24 return MakeCompKeyID(t, uint32(h.Sum64())) 25 } 26 27 var compKeyRand *rand.Rand 28 29 // MakeCompKeyIDNowRand generates a value for CompKey.ID based on the current unix timestamp in seconds for the top 32 bits and 30 // the bottom 32 bits populated from a random source 31 func MakeCompKeyIDNowRand() uint64 { 32 if compKeyRand == nil { 33 compKeyRand = rand.New(rand.NewSource(time.Now().UnixNano())) 34 } 35 var ret = uint64(time.Now().Unix()) << 32 36 ret |= uint64(compKeyRand.Int63() & 0xFFFFFFFF) 37 return ret 38 }