-
Notifications
You must be signed in to change notification settings - Fork 0
/
snowflake.go
50 lines (39 loc) · 1.05 KB
/
snowflake.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
package snowflake
import (
"sync/atomic"
"time"
)
const (
timeMask = (1 << 42) - 1
generatorMask = (1 << 10) - 1
sequenceMask = (1 << 12) - 1
timeShift = 10 + 12
)
// Generator of snowflake ids.
// Lock free and thread safe.
// Loops back sequence when it reaches the limit within the same time and generator id.
type Generator struct {
state uint64 // 42b time | 10b generator(empty) | 12b sequence
generator uint64 // 10b fixed generator part of id
}
func NewGenerator(generatorID uint16) *Generator {
return &Generator{generator: (uint64(generatorID) & generatorMask) << 12}
}
func (g *Generator) Next() uint64 {
var state uint64
for {
t := uint64(time.Now().UnixMilli()) & timeMask
current := atomic.LoadUint64(&g.state)
currentTime := (current >> timeShift) & timeMask
currentSeq := current & sequenceMask
if t > currentTime || currentSeq == sequenceMask {
state = t << timeShift
} else {
state = current + 1
}
if atomic.CompareAndSwapUint64(&g.state, current, state) {
break
}
}
return state | g.generator
}