-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnowflake.go
77 lines (70 loc) · 1.4 KB
/
snowflake.go
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
66
67
68
69
70
71
72
73
74
75
76
77
package snowflake
import (
"errors"
"sync"
"time"
)
var (
ErrWorkerIdOutOfRange = errors.New("snowflake: WorkerId out of range")
)
const (
workerBits uint8 = 10
numBits uint8 = 12
workerMax int64 = ^(-1 << workerBits)
numMax int64 = ^(-1 << numBits)
timeShift = workerBits + numBits
workShift = numBits
epoch int64 = 1629973119
)
//var epoch int64
type Worker struct {
mu sync.Mutex
timeMsStamp int64 // ms时间戳
workerId int64
step int64 //当前毫秒已经生成的id序列号
}
func NewWorker(workerId int64) (*Worker, error) {
if workerId < 0 || workerId > workerMax {
return nil, ErrWorkerIdOutOfRange
}
return &Worker{
timeMsStamp: 0,
workerId: workerId,
step: 0,
}, nil
}
func (w *Worker) Now() int64 {
return time.Now().Unix()
}
func (w *Worker) GetId() int64 {
w.mu.Lock()
defer w.mu.Unlock()
now := w.Now()
if now == w.timeMsStamp {
w.step++
if w.step > numMax {
w.step = 0
for now < w.timeMsStamp {
now = w.Now()
}
w.timeMsStamp = now
}
} else if now > w.timeMsStamp {
w.step = 0
w.timeMsStamp = now
} else {
//服务器发生了时钟回拨
for now < w.timeMsStamp {
now = w.Now()
}
w.step++
if w.step > numMax {
w.step = 0
for now < w.timeMsStamp {
now = w.Now()
}
w.timeMsStamp = now
}
}
return (now-epoch)<<timeShift | (w.workerId << workShift) | w.step
}