-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathid2.go
142 lines (123 loc) · 2.66 KB
/
id2.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package goid
import (
"crypto/rand"
"github.com/beevik/ntp"
"math/big"
"sync/atomic"
"time"
)
func NewID2() *ID2 {
return &ID2{
delta: 1,
maxBacktrackWait: 3 * time.Second,
}
}
var _id2 = NewID2()
func GetID2() *ID2 {
return _id2
}
func GenID2() int64 {
return _id2.Generate()
}
func ResolveID2(id int64, oid *ID2) (timestamp int64, counter uint32) {
return id >> 20, uint32(id) & uint32((1<<(20-oid.nodeBits))-1)
}
type ID2 struct {
id int64
maxBacktrackWait time.Duration
ntpServer string
delta uint32
randomDelta uint32
node uint32
nodeBits uint8
}
func (i *ID2) Generate() int64 {
for {
old := atomic.LoadInt64(&i.id)
nt := time.Now().Unix()
lt := (old >> 20) & ((1 << 33) - 1)
cBits := 20 - i.nodeBits
mask := uint32((1 << cBits) - 1)
ct := uint32(old) & mask
if nt < lt {
if time.Duration(lt-nt)*time.Second <= i.maxBacktrackWait {
time.Sleep(time.Millisecond)
continue
}
if i.ntpServer == "" {
panic("ntp server not set")
}
ntTime, err := ntp.Time(i.ntpServer)
if err != nil {
panic(err)
}
nt = ntTime.Unix()
if nt < lt {
panic("ntp time error")
}
}
if nt == lt {
ct += i.getDelta()
if ct > mask {
time.Sleep(time.Millisecond)
continue
}
} else {
ct = i.getDelta()
}
now := (nt << 20) | int64(ct)
if i.nodeBits > 0 {
now |= int64(i.node) << cBits
}
if atomic.CompareAndSwapInt64(&i.id, old, now) {
return now
}
}
}
func (i *ID2) getDelta() uint32 {
if i.randomDelta > 0 {
if de, err := rand.Int(rand.Reader, big.NewInt(int64(i.randomDelta))); err == nil {
return uint32(de.Int64() + 1)
}
}
return i.delta
}
func (i *ID2) SetDelta(d uint32) {
if d == 0 || d >= (1<<(20-i.nodeBits)-1) {
panic("delta too large or invalid")
}
i.delta = d
}
func (i *ID2) GetDelta() uint32 {
return i.delta
}
func (i *ID2) SetRandomDelta(r uint32) {
if r == 0 || r >= (1<<(20-i.nodeBits)-1) {
panic("random delta too large or invalid")
}
i.randomDelta = r
}
func (i *ID2) GetRandomDelta() uint32 {
return i.randomDelta
}
func (i *ID2) SetNode(node uint32, nodeBits uint8) {
if nodeBits < 2 || nodeBits > 18 ||
node > (1<<nodeBits-1) ||
i.delta >= (1<<(20-nodeBits)-1) ||
i.randomDelta >= (1<<(20-nodeBits)-1) {
panic("node or nodeBits is invalid")
}
i.node, i.nodeBits = node, nodeBits
}
func (i *ID2) GetNode() (node uint32, nodeBits uint8) {
return i.node, i.nodeBits
}
func (i *ID2) SetMaxBacktrackWait(d time.Duration) {
i.maxBacktrackWait = d
}
func (i *ID2) GetMaxBacktrackWait() time.Duration {
return i.maxBacktrackWait
}
func (i *ID2) SetNTPServer(s string) {
i.ntpServer = s
}