算法代码
const (
workerBits uint8 = 10 //机器码位数
numberBits uint8 = 12 //序列号位数
workerMax int64 = -1 ^ (-1 << workerBits)
numberMax int64 = -1 ^ (-1 << numberBits)
timeShift uint8 = workerBits + numberBits //时间戳偏移量
workerShift uint8 = numberBits //机器码偏移量
startTime int64 = 1650793569000 // 起始时间戳(单位:ms)
)
type Worker struct {
mu sync.Mutex
timestamp int64
workerId int64
number int64
}
// NewWorker 生成一个新节点
func NewWorker(workerId int64) (*Worker, error) {
if workerId < 0 || workerId > workerMax {
return nil, errors.New("Worker ID excess of quantity")
}
return &Worker{
timestamp: 0,
workerId: workerId,
number: 0,
}, nil
}
func (w *Worker) GetId() int64 {
w.mu.Lock()
defer w.mu.Unlock()
now := time.Now().UnixNano() / 1e6
if w.timestamp == now {
w.number++
if w.number > numberMax {
for now <= w.timestamp {
now = time.Now().UnixNano() / 1e6
}
}
} else {
w.number = 0
w.timestamp = now
}
ID := int64((now-startTime)<<timeShift | (w.workerId << workerShift) | (w.number))
return ID
}
测试:
func main() {
// 生成节点实例
node, err := NewWorker(1)
node2, err := NewWorker(2)
if err != nil {
panic(err)
}
i := 10
for i > 0 {
if i%2 == 0 {
fmt.Println(node2.GetId())
} else {
fmt.Println(node.GetId())
}
i--
}
}
结果: