Repository navigation
Expand file tree
/
Copy pathgenerator.go
More file actions
167 lines (131 loc) · 3.31 KB
/
Copy pathgenerator.go
File metadata and controls
167 lines (131 loc) · 3.31 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Package snowflake provides a sharded Snowflake ID generator.
package snowflake
import (
"math/bits"
"runtime"
"sync/atomic"
"time"
_ "unsafe"
)
// pinCurrentProcessor keeps a goroutine on one processor slot while it uses shard-local state.
//
//go:linkname pinCurrentProcessor runtime.procPin
func pinCurrentProcessor() int
// unpinCurrentProcessor releases the pin created by pinCurrentProcessor.
//
//go:linkname unpinCurrentProcessor runtime.procUnpin
func unpinCurrentProcessor()
const (
epochMs = int64(1767225600000)
nodeBits = 10
seqBits = 12
maxNode = (1 << nodeBits) - 1
maxSeq = (1 << seqBits) - 1
nodeShift = seqBits
timeShift = nodeBits + seqBits
)
type shard struct {
state atomic.Int64
nodeID int64
pad [48]byte
}
// Generator creates Snowflake IDs with one shard per logical processor.
type Generator struct {
shards []shard
}
// NewGenerator creates a generator for the given node ID.
func NewGenerator(nodeID int64) *Generator {
shardCount := runtime.GOMAXPROCS(0)
shardBits := bits.Len64(uint64(shardCount - 1))
if shardBits > nodeBits {
panic("snowflake: GOMAXPROCS exceeds node ID space")
}
nodeMask := int64((1 << (nodeBits - shardBits)) - 1)
baseNode := (nodeID & nodeMask) << shardBits
gen := &Generator{
shards: make([]shard, shardCount),
}
for idx := range gen.shards {
gen.shards[idx].nodeID = baseNode | int64(idx)
}
return gen
}
// Next returns one Snowflake ID.
func (gen *Generator) Next() int64 {
shard, nodeField := gen.pinShard()
for {
state := shard.state.Load()
ts, seq := unpackState(state)
nextTs := ts
nextSeq := seq + 1
if state == 0 || seq == maxSeq {
unpinCurrentProcessor()
clockTs := loadClockTs()
shard, nodeField = gen.pinShard()
state = shard.state.Load()
ts, _ = unpackState(state)
nextTs = nextTsAfterClock(clockTs, state, ts)
nextSeq = 0
}
nextState := packState(nextTs, nextSeq)
if shard.state.CompareAndSwap(state, nextState) {
unpinCurrentProcessor()
return composeID(nextTs, nodeField, nextSeq)
}
}
}
// NextBatch fills destination with sequential Snowflake IDs.
func (gen *Generator) NextBatch(dst []int64) {
if len(dst) == 0 {
return
}
shard, nodeField := gen.pinShard()
state := shard.state.Load()
ts, seq := unpackState(state)
if state == 0 {
// Fresh shards should emit sequence 0 in the current millisecond.
ts = loadPinnedTs()
seq = -1
}
for idx := range dst {
seq++
if seq > maxSeq {
clockTs := loadPinnedTs()
if clockTs > ts {
ts = clockTs
} else {
ts++
}
seq = 0
}
dst[idx] = composeID(ts, nodeField, seq)
}
shard.state.Store(packState(ts, seq))
unpinCurrentProcessor()
}
func (gen *Generator) pinShard() (*shard, int64) {
idx := pinCurrentProcessor()
shard := &gen.shards[idx]
return shard, shard.nodeID << nodeShift
}
func loadClockTs() int64 {
return clockNowMilliseconds() - epochMs
}
func loadPinnedTs() int64 {
return time.Now().UnixMilli() - epochMs
}
func nextTsAfterClock(clockTs, state, ts int64) int64 {
if state != 0 && clockTs <= ts {
return ts + 1
}
return clockTs
}
func packState(ts, seq int64) int64 {
return (ts << seqBits) | seq
}
func unpackState(state int64) (ts int64, seq int64) {
return state >> seqBits, state & maxSeq
}
func composeID(ts, nodeField, seq int64) int64 {
return (ts << timeShift) | nodeField | seq
}