24
24
// Remember, you have a total 22 bits to share between Node/Step
25
25
StepBits uint8 = 12
26
26
27
+ // DEPRECATED: the below four variables will be removed in a future release.
28
+ mu sync.Mutex
27
29
nodeMax int64 = - 1 ^ (- 1 << NodeBits )
28
30
nodeMask = nodeMax << StepBits
29
31
stepMask int64 = - 1 ^ (- 1 << StepBits )
@@ -79,6 +81,12 @@ type Node struct {
79
81
time int64
80
82
node int64
81
83
step int64
84
+
85
+ nodeMax int64
86
+ nodeMask int64
87
+ stepMask int64
88
+ timeShift uint8
89
+ nodeShift uint8
82
90
}
83
91
84
92
// An ID is a custom type used for a snowflake ID. This is used so we can
@@ -90,21 +98,28 @@ type ID int64
90
98
func NewNode (node int64 ) (* Node , error ) {
91
99
92
100
// re-calc in case custom NodeBits or StepBits were set
101
+ // DEPRECATED: the below block will be removed in a future release.
102
+ mu .Lock ()
93
103
nodeMax = - 1 ^ (- 1 << NodeBits )
94
104
nodeMask = nodeMax << StepBits
95
105
stepMask = - 1 ^ (- 1 << StepBits )
96
106
timeShift = NodeBits + StepBits
97
107
nodeShift = StepBits
98
-
99
- if node < 0 || node > nodeMax {
100
- return nil , errors .New ("Node number must be between 0 and " + strconv .FormatInt (nodeMax , 10 ))
108
+ mu .Unlock ()
109
+
110
+ n := Node {}
111
+ n .node = node
112
+ n .nodeMax = - 1 ^ (- 1 << NodeBits )
113
+ n .nodeMask = n .nodeMax << StepBits
114
+ n .stepMask = - 1 ^ (- 1 << StepBits )
115
+ n .timeShift = NodeBits + StepBits
116
+ n .nodeShift = StepBits
117
+
118
+ if n .node < 0 || n .node > n .nodeMax {
119
+ return nil , errors .New ("Node number must be between 0 and " + strconv .FormatInt (n .nodeMax , 10 ))
101
120
}
102
121
103
- return & Node {
104
- time : 0 ,
105
- node : node ,
106
- step : 0 ,
107
- }, nil
122
+ return & n , nil
108
123
}
109
124
110
125
// Generate creates and returns a unique snowflake ID
@@ -115,7 +130,7 @@ func (n *Node) Generate() ID {
115
130
now := time .Now ().UnixNano () / 1000000
116
131
117
132
if n .time == now {
118
- n .step = (n .step + 1 ) & stepMask
133
+ n .step = (n .step + 1 ) & n . stepMask
119
134
120
135
if n .step == 0 {
121
136
for now <= n .time {
@@ -128,8 +143,8 @@ func (n *Node) Generate() ID {
128
143
129
144
n .time = now
130
145
131
- r := ID ((now - Epoch )<< timeShift |
132
- (n .node << nodeShift ) |
146
+ r := ID ((now - Epoch )<< n . timeShift |
147
+ (n .node << n . nodeShift ) |
133
148
(n .step ),
134
149
)
135
150
@@ -253,16 +268,19 @@ func (f ID) IntBytes() [8]byte {
253
268
}
254
269
255
270
// Time returns an int64 unix timestamp of the snowflake ID time
271
+ // DEPRECATED: the below function will be removed in a future release.
256
272
func (f ID ) Time () int64 {
257
273
return (int64 (f ) >> timeShift ) + Epoch
258
274
}
259
275
260
276
// Node returns an int64 of the snowflake ID node number
277
+ // DEPRECATED: the below function will be removed in a future release.
261
278
func (f ID ) Node () int64 {
262
279
return int64 (f ) & nodeMask >> nodeShift
263
280
}
264
281
265
282
// Step returns an int64 of the snowflake step (or sequence) number
283
+ // DEPRECATED: the below function will be removed in a future release.
266
284
func (f ID ) Step () int64 {
267
285
return int64 (f ) & stepMask
268
286
}
0 commit comments