1
0
mirror of synced 2025-12-17 02:28:12 +08:00
Files
SnowFlake-IdGenerator/Go/source/idgen/DefaultIdGenerator.go
2021-04-01 02:38:11 +08:00

76 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* 版权属于yitter(yitter@126.com)
* 代码编辑guoyahao
* 代码修订yitter
* 开源地址https://gitee.com/yitter/idgenerator
*/
package idgen
import (
"time"
"yitidgen/contract"
"yitidgen/core"
)
type DefaultIdGenerator struct {
Options *contract.IdGeneratorOptions
SnowWorker contract.ISnowWorker
IdGeneratorException contract.IdGeneratorException
}
func NewDefaultIdGenerator(options *contract.IdGeneratorOptions) *DefaultIdGenerator {
if options == nil {
panic("dig.Options error.")
}
minTime := int64(631123200000) // time.Now().AddDate(-30, 0, 0).UnixNano() / 1e6
if options.BaseTime < minTime || options.BaseTime > time.Now().UnixNano()/1e6 {
panic("BaseTime error.")
}
if options.SeqBitLength+options.WorkerIdBitLength > 22 {
panic("errorWorkerIdBitLength + SeqBitLength <= 22")
}
maxWorkerIdNumber := uint16(1<<options.WorkerIdBitLength) - 1
if options.WorkerId > maxWorkerIdNumber {
panic("WorkerId error. (range:[1, " + string(maxWorkerIdNumber) + "]")
}
if options.SeqBitLength < 2 || options.SeqBitLength > 21 {
panic("SeqBitLength error. (range:[2, 21])")
}
maxSeqNumber := uint32(1<<options.SeqBitLength) - 1
if options.MaxSeqNumber > maxSeqNumber {
panic("MaxSeqNumber error. (range:[1, " + string(maxSeqNumber) + "]")
}
if options.MinSeqNumber > maxSeqNumber {
panic("MinSeqNumber error. (range:[1, " + string(maxSeqNumber) + "]")
}
var snowWorker contract.ISnowWorker
switch options.Method {
case 1:
snowWorker = core.NewSnowWorkerM1(options)
case 2:
snowWorker = core.NewSnowWorkerM2(options)
default:
snowWorker = core.NewSnowWorkerM1(options)
}
if options.Method == 1 {
time.Sleep(time.Duration(500) * time.Microsecond)
}
return &DefaultIdGenerator{
Options: options,
SnowWorker: snowWorker,
}
}
func (dig DefaultIdGenerator) NewLong() uint64 {
return dig.SnowWorker.NextId()
}