github.com/vugu/vugu@v0.3.5/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 h.Write(b) 21 return MakeCompKeyID(t, uint32(h.Sum64())) 22 } 23 24 var compKeyRand *rand.Rand 25 26 // MakeCompKeyIDNowRand generates a value for CompKey.ID based on the current unix timestamp in seconds for the top 32 bits and 27 // the bottom 32 bits populated from a random source 28 func MakeCompKeyIDNowRand() uint64 { 29 if compKeyRand == nil { 30 compKeyRand = rand.New(rand.NewSource(time.Now().UnixNano())) 31 } 32 var ret = uint64(time.Now().Unix()) << 32 33 ret |= uint64(compKeyRand.Int63() & 0xFFFFFFFF) 34 return ret 35 }