-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
141 lines (113 loc) · 3.53 KB
/
generator.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
// The idutils package generates unique IDs
//
// Implementation is based on Twitter Snowflake
// https://github.com/twitter/snowflake/blob/snowflake-2010/src/main/scala/com/twitter/service/snowflake/IdWorker.scala
package idutils
import (
"fmt"
"sync"
"time"
)
const (
CustomEpoch = int64(1288834974657) // Thu, 04 Nov 2010 01:42:54 GMT
WorkerIdBits = 5
DatacenterIdBits = 5
MaxWorkerId = -1 ^ (-1 << 5) // -1 ^ (-1 << workerIdBits)
MaxDatacenterId = -1 ^ (-1 << 5) // -1 ^ (-1 << datacenterIdBits)
SequenceBits = 12
WorkerIdShift = 12 // sequenceBits
DatacenterIdShift = 17 // sequenceBits + workerIdBits
TimestampLeftShift = 22 // sequenceBits + workerIdBits + datacenterIdBits
SequenceMask = -1 ^ (-1 << 12) // -1 ^ (-1 << sequenceBits)
)
var (
CustomEpochTime = time.Unix(0, CustomEpoch*1000000)
)
type TimeId = int64
type Timestamp = int64
func timeGen() int64 {
return time.Now().UnixNano() / 1000000
}
type Generator struct {
WorkerId int64
DatacenterId int64
sequence int64
lastTimestamp Timestamp
timeGen func() Timestamp
mutex sync.Mutex
}
func NewGenerator(workerId int64, datacenterId int64) (*Generator, error) {
if workerId > MaxWorkerId || workerId < 0 {
return nil, fmt.Errorf("worker Id cannot be greater than %d or less than 0", MaxWorkerId)
}
if datacenterId > MaxDatacenterId || datacenterId < 0 {
return nil, fmt.Errorf("datacenter Id cannot be greater than %d or less than 0", MaxDatacenterId)
}
g := &Generator{
WorkerId: workerId,
DatacenterId: datacenterId,
sequence: 0,
lastTimestamp: -1,
timeGen: timeGen,
}
return g, nil
}
func (g *Generator) buildId() TimeId {
return ((g.lastTimestamp - CustomEpoch) << TimestampLeftShift) |
(g.DatacenterId << DatacenterIdShift) |
(g.WorkerId << WorkerIdShift) |
g.sequence
}
func (g *Generator) NextId() (TimeId, error) {
g.mutex.Lock()
defer g.mutex.Unlock()
timestamp := g.timeGen()
if timestamp < g.lastTimestamp {
return 0, fmt.Errorf("Clock moved backwards. Refusing to generate id for %d milliseconds", g.lastTimestamp-timestamp)
}
if g.lastTimestamp == timestamp {
g.sequence = (g.sequence + 1) & SequenceMask
if g.sequence == 0 {
timestamp = g.tilNextMillis(g.lastTimestamp)
}
} else {
g.sequence = 0
}
g.lastTimestamp = timestamp
return g.buildId(), nil
}
func (g *Generator) tilNextMillis(lastTimestamp Timestamp) Timestamp {
timestamp := g.timeGen()
for timestamp <= lastTimestamp {
timestamp = g.timeGen()
}
return timestamp
}
func IdToTimestamp(id TimeId) Timestamp {
return (id >> TimestampLeftShift) + CustomEpoch
}
func IdToTime(id TimeId) time.Time {
return time.Unix(0, IdToTimestamp(id)*1000000).UTC()
}
func IdEndOfTimestamp(timestamp Timestamp) TimeId {
if timestamp < CustomEpoch {
panic("idutils.IdEndOfTimestamp timestamp must be greater than CustomEpoch")
}
return (timestamp-CustomEpoch)<<TimestampLeftShift | ((1 << TimestampLeftShift) - 1)
}
func IdEndOfTime(t time.Time) TimeId {
return IdEndOfTimestamp(t.UnixNano() / 1000000)
}
func IdStartOfTimestamp(timestamp Timestamp) TimeId {
if timestamp < CustomEpoch {
panic("idutils.IdEndOfTimestamp timestamp must be greater than CustomEpoch")
}
return (timestamp - CustomEpoch) << TimestampLeftShift
}
func IdStartOfTime(t time.Time) TimeId {
return IdStartOfTimestamp(t.UnixNano() / 1000000)
}
func IdAddDuration(id TimeId, d time.Duration) TimeId {
millis := Timestamp(d / 1000000)
return id + (millis << TimestampLeftShift)
}