-
Notifications
You must be signed in to change notification settings - Fork 1
/
id3.go
145 lines (127 loc) · 2.85 KB
/
id3.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
143
144
145
package goid
import (
"crypto/rand"
"github.com/beevik/ntp"
"math/big"
"sync/atomic"
"time"
)
const (
MaxBits = 53
)
type ID3 struct {
id int64
maxBacktrackWait time.Duration
ntpServer string
randomDelta uint16
node uint16
delta uint16
nodeBits uint8
bits uint8
}
func (i *ID3) Generate() int64 {
for {
old := atomic.LoadInt64(&i.id)
nt := time.Now().UnixMilli()
ncbits := MaxBits - i.bits
lt := (old >> ncbits) & ((1 << i.bits) - 1)
cBits := ncbits - i.nodeBits
mask := uint16((1 << cBits) - 1)
ct := uint16(old) & mask
if nt < lt {
if time.Duration(lt-nt)*time.Millisecond <= 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.UnixMilli()
if nt < lt {
panic("ntp time error")
}
}
if nt == lt {
ct += i.getDelta()
if ct > mask {
continue
}
} else {
ct = i.getDelta()
}
now := (nt << ncbits) | int64(ct)
if i.nodeBits > 0 {
now |= int64(i.node) << cBits
}
if atomic.CompareAndSwapInt64(&i.id, old, now) {
return now
}
}
}
func (i *ID3) getDelta() uint16 {
if i.randomDelta > 0 {
if de, err := rand.Int(rand.Reader, big.NewInt(int64(i.randomDelta))); err == nil {
return uint16(de.Int64() + 1)
}
}
return i.delta
}
func (i *ID3) SetDelta(d uint16) {
if d == 0 || d >= (1<<(MaxBits-i.bits-i.nodeBits)-1) {
panic("delta too large or invalid")
}
i.delta = d
}
func (i *ID3) GetDelta() uint16 {
return i.delta
}
func (i *ID3) SetRandomDelta(r uint16) {
if r == 0 || r >= (1<<(MaxBits-i.bits-i.nodeBits)-1) {
panic("random delta too large or invalid")
}
i.randomDelta = r
}
func (i *ID3) GetRandomDelta() uint16 {
return i.randomDelta
}
func (i *ID3) SetNode(node uint16, nodeBits uint8) {
if nodeBits < 2 || nodeBits > (MaxBits-i.bits-2) ||
node > (1<<nodeBits-1) ||
i.delta >= (1<<(MaxBits-i.bits-nodeBits)-1) ||
i.randomDelta >= (1<<(MaxBits-i.bits-nodeBits)-1) {
panic("node or nodeBits is invalid")
}
i.node, i.nodeBits = node, nodeBits
}
func (i *ID3) GetNode() (node uint16, nodeBits uint8) {
return i.node, i.nodeBits
}
func (i *ID3) SetBits(bits uint8) {
if bits < 42 || bits > 43 ||
i.delta >= (1<<(MaxBits-bits-i.nodeBits)-1) ||
i.randomDelta >= (1<<(MaxBits-bits-i.nodeBits)-1) {
panic("bits is invalid")
}
i.bits = bits
}
func NewID3() *ID3 {
return &ID3{
delta: 1,
bits: 42,
maxBacktrackWait: 3 * time.Second,
}
}
var _id3 = NewID3()
func GetID3() *ID3 {
return _id3
}
func GenID3() int64 {
return _id3.Generate()
}
func ResolveID3(id int64, oid *ID3) (timestamp int64, counter uint16) {
return id >> (MaxBits - oid.bits), uint16(id) & uint16((1<<(MaxBits-oid.bits-oid.nodeBits))-1)
}