forked from hwholiday/learning_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnow_flake_52.go
More file actions
65 lines (59 loc) · 1.15 KB
/
Copy pathsnow_flake_52.go
File metadata and controls
65 lines (59 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"errors"
"fmt"
"sync"
"time"
)
const (
numberBits52 uint8 = 11
numberMax52 int64 = -1 ^ (-1 << numberBits52)
timeShift52 uint8 = 11
startTime52 int64 = 1626408485000 // 如果在程序跑了一段时间修改了epoch这个值 可能会导致生成相同的ID
)
type SnowWorker struct {
mu sync.Mutex
timestamp int64
number int64
}
// NewSnowWorker 一毫秒发2047个
// 41位时间戳 11位自旋
func NewSnowWorker() *SnowWorker {
// 生成一个新节点
return &SnowWorker{
timestamp: 0,
number: 0,
}
}
func (w *SnowWorker) GetTime() int64 {
return time.Now().UnixNano() / 1e6
}
func (w *SnowWorker) GetId() (int64, error) {
w.mu.Lock()
defer w.mu.Unlock()
now := w.GetTime()
if w.timestamp == now {
w.number++
if w.number > numberMax52 {
for now <= w.timestamp {
now = w.GetTime()
}
}
} else {
w.number = 0
w.timestamp = now
}
if now < w.timestamp {
return 0, errors.New("clock move backwards")
}
ID := int64((now-startTime52)<<timeShift52 | (w.number))
return ID, nil
}
func main() {
// 生成节点实例
node := NewSnowWorker()
for {
fmt.Println(node.GetId())
return
}
}