代码拉取完成,页面将自动刷新
package gotool
import (
"sync"
"time"
)
var snowflakeEntity *Snowflake
// Snowflake 结构体
type Snowflake struct {
mutex sync.Mutex
epoch int64
nodeID uint8
sequence uint16
lastMillis int64
}
// NewSnowflake 创建 Snowflake 实例
//
// epoch: 起始时间戳
// nodeID: 节点ID
func NewSnowflake(epoch int64, nodeID uint8) *Snowflake {
return &Snowflake{
epoch: epoch,
nodeID: nodeID,
sequence: 0,
}
}
// GenerateSnowflake 生成新的雪花码
func (s *Snowflake) GenerateSnowflake() int64 {
s.mutex.Lock()
defer s.mutex.Unlock()
// 时钟回拨
currentMillis := s.timeGen()
if currentMillis < s.lastMillis {
panic("Clock moved backwards. Refusing to generate id.")
}
// 相同毫秒内,序列号自增
if currentMillis == s.lastMillis {
s.sequence = (s.sequence + 1) & 0xFFF
if s.sequence == 0 {
currentMillis = s.tilNextMillis(s.lastMillis)
}
} else {
// 不同毫秒内,序列号置为 0
s.sequence = 0
}
s.lastMillis = currentMillis
id := ((currentMillis - s.epoch) << 22) |
(int64(s.nodeID) << 12) |
int64(s.sequence)
return id
}
// timeGen 获取当前时间戳
func (s *Snowflake) timeGen() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
// tilNextMillis 获取下一个时间戳
func (s *Snowflake) tilNextMillis(lastMillis int64) int64 {
millis := s.timeGen()
for millis <= lastMillis {
millis = s.timeGen()
}
return millis
}
// SignalSnowflake 单节点的雪花码
func SignalSnowflake() int64 {
if snowflakeEntity == nil {
snowflakeEntity = NewSnowflake(1600000000000, 1)
}
sf := snowflakeEntity
return sf.GenerateSnowflake()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。