diff --git a/Go/source/core/snowWorkerM1.go b/Go/source/core/snowWorkerM1.go index 30a783c..b5fc7db 100644 --- a/Go/source/core/snowWorkerM1.go +++ b/Go/source/core/snowWorkerM1.go @@ -7,7 +7,6 @@ package core import ( - "math" "sync" "time" "yitidgen/contract" @@ -53,7 +52,7 @@ func NewSnowWorkerM1(options *contract.IdGeneratorOptions) contract.ISnowWorker if options.MaxSeqNumber > 0 { maxSeqNumber = options.MaxSeqNumber } else { - maxSeqNumber = uint32(math.Pow(2, float64(options.SeqBitLength))) - 1 + maxSeqNumber = (1 << seqBitLength) - 1 } var minSeqNumber = options.MinSeqNumber var topOverCostCount = options.TopOverCostCount diff --git a/Go/source/core/snowWorkerM2.go b/Go/source/core/snowWorkerM2.go index 7a08d01..48ebcd3 100644 --- a/Go/source/core/snowWorkerM2.go +++ b/Go/source/core/snowWorkerM2.go @@ -9,7 +9,6 @@ package core import ( "fmt" "strconv" - "sync/atomic" "yitidgen/contract" ) @@ -24,15 +23,17 @@ func NewSnowWorkerM2(options *contract.IdGeneratorOptions) contract.ISnowWorker } func (m2 SnowWorkerM2) NextId() uint64 { + m2.Lock() + defer m2.Unlock() currentTimeTick := m2.GetCurrentTimeTick() if m2._LastTimeTick == currentTimeTick { - atomic.AddUint32(&m2._CurrentSeqNumber, 1) + m2._CurrentSeqNumber++ if m2._CurrentSeqNumber > m2.MaxSeqNumber { - atomic.StoreUint32(&m2._CurrentSeqNumber, uint32(m2.MinSeqNumber)) + m2._CurrentSeqNumber = m2.MinSeqNumber currentTimeTick = m2.GetNextTimeTick() } } else { - atomic.StoreUint32(&m2._CurrentSeqNumber, uint32(m2.MinSeqNumber)) + m2._CurrentSeqNumber = m2.MinSeqNumber } if currentTimeTick < m2._LastTimeTick { fmt.Println("Time error for {0} milliseconds", strconv.FormatInt(m2._LastTimeTick-currentTimeTick, 10))