You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool/nrandom/snowflake/snowflake.go

38 lines
934 B
Go

1 year ago
package snowflake
import (
"fmt"
)
type SnowWorker struct {
*SnowWorkerOffset
}
func NewSnowWorker(options *Options) *SnowWorker {
options.Method = 2
return &SnowWorker{
NewSnowWorkerOffset(options),
}
}
func (s *SnowWorker) NextID() int64 {
s.Lock()
defer s.Unlock()
currentTimeTick := s.currentTimeTick()
if s.lastTimeTick == currentTimeTick {
s.currentSeqNumber++
if s.currentSeqNumber > s.maxSeqNumber {
s.currentSeqNumber = s.minSeqNumber
currentTimeTick = s.nextTimeTick()
}
} else {
s.currentSeqNumber = s.minSeqNumber
}
if currentTimeTick < s.lastTimeTick {
fmt.Printf("Time error for %d milliseconds", s.lastTimeTick-currentTimeTick)
}
s.lastTimeTick = currentTimeTick
id := currentTimeTick<<s.timestampShift + int64(s.workerId<<s.seqBitLength) + int64(s.currentSeqNumber)
return id
}