Browse Source

go实现,获取下一时间戳前,暂停1ms

tags/v1.3.1
yitter 2 years ago
parent
commit
c916112dd7
3 changed files with 21 additions and 8 deletions
  1. +1
    -0
      Go/source/idgen/SnowWorkerM1.go
  2. +15
    -8
      Go/source/idgen/YitIdHelper.go
  3. +5
    -0
      Go/source/regworkerid/reghelper.go

+ 1
- 0
Go/source/idgen/SnowWorkerM1.go View File

@@ -232,6 +232,7 @@ func (m1 *SnowWorkerM1) GetCurrentTimeTick() int64 {
func (m1 *SnowWorkerM1) GetNextTimeTick() int64 {
tempTimeTicker := m1.GetCurrentTimeTick()
for tempTimeTicker <= m1._LastTimeTick {
time.Sleep(time.Duration(1) * time.Millisecond)
tempTimeTicker = m1.GetCurrentTimeTick()
}
return tempTimeTicker


+ 15
- 8
Go/source/idgen/YitIdHelper.go View File

@@ -1,3 +1,10 @@
/*
* 版权属于:yitter(yitter@126.com)
* 代码编辑:guoyahao
* 代码修订:yitter
* 开源地址:https://github.com/yitter/idgenerator
*/

package idgen

import (
@@ -17,14 +24,14 @@ func SetIdGenerator(options *IdGeneratorOptions) {

// NextId .
func NextId() int64 {
if idGenerator == nil {
singletonMutex.Lock()
defer singletonMutex.Unlock()
if idGenerator == nil {
options := NewIdGeneratorOptions(1)
idGenerator = NewDefaultIdGenerator(options)
}
}
//if idGenerator == nil {
// singletonMutex.Lock()
// defer singletonMutex.Unlock()
// if idGenerator == nil {
// options := NewIdGeneratorOptions(1)
// idGenerator = NewDefaultIdGenerator(options)
// }
//}

return idGenerator.NewLong()
}


+ 5
- 0
Go/source/regworkerid/reghelper.go View File

@@ -1,3 +1,8 @@
/*
* 版权属于:yitter(yitter@126.com)
* 开源地址:https://github.com/yitter/idgenerator
*/

package regworkerid

import (


Loading…
Cancel
Save