1 Star 0 Fork 0

笑看风云 / gocodes

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ident-int.go 999 Bytes
一键复制 编辑 原始数据 按行查看 历史
笑看风云 提交于 2023-10-19 19:15 . 备份
package identx
import (
"math/rand"
"sync"
"sync/atomic"
"time"
)
func init() {
identInt = NewIdentInt()
identInt.resetObjectIDCounter()
//SetNode(3844 + rander.Intn(238328-3844))
}
var identInt *IdentInt
func GenTimeCount() int64 {
return identInt.GenOne()
}
func NewIdentInt() (obj *IdentInt) {
obj = &IdentInt{}
obj.rander = rand.New(rand.NewSource(time.Now().UnixNano()))
return
}
type IdentInt struct {
rander *rand.Rand
objectIDCounter uint32
mutex sync.Mutex
lastTimestamp int64
}
func (ii *IdentInt) GenOne() int64 {
ii.mutex.Lock()
defer ii.mutex.Unlock()
now := timeGen()
if now > ii.lastTimestamp {
ii.lastTimestamp = now
ii.resetObjectIDCounter()
}
count := uint64(atomic.AddUint32(&ii.objectIDCounter, 1))
countInt64 := int64(count << 32)
ident := countInt64 + ii.lastTimestamp
return ident
}
func (ii *IdentInt) resetObjectIDCounter() {
//ii.objectIDCounter = 0
ii.objectIDCounter = uint32(3844 + ii.rander.Intn(65535-3844))
}
Go
1
https://gitee.com/zhongguo168a/gocodes.git
git@gitee.com:zhongguo168a/gocodes.git
zhongguo168a
gocodes
gocodes
46752958e769

搜索帮助