-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.go
78 lines (61 loc) · 1.49 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
package fastid
import (
"errors"
"sync"
"time"
)
// Generator represents IDs with a given epoch and workedID.
type Generator struct {
mutex sync.RWMutex
epoch uint64
timestamp uint64
workerID uint64
sequence uint64
lastTimestamp uint64
lastID uint64
}
// NewGenerator creates a new generator for IDs with a given epoch and workerID.
func NewGenerator(epoch int64, workerID int) (*Generator, error) {
if workerID > MaxWorkerID {
return nil, errors.New("workerID is too big")
}
g := &Generator{
epoch: uint64(epoch),
workerID: uint64(workerID),
}
return g, nil
}
// Next returns a next ID.
func (g *Generator) Next() ID {
g.mutex.Lock()
nowNano := time.Now().UnixNano()
g.timestamp = uint64(nowNano) / uint64(time.Millisecond)
if g.timestamp <= g.lastTimestamp {
g.sequence = (g.sequence + 1) & sequenceMask
if g.sequence == 0 {
g.timestamp = g.lastTimestamp + 1
}
} else {
g.sequence = 0
}
ts := (g.timestamp - g.epoch) << timestampShift
id := g.workerID << workerIDShift
seq := g.sequence
nextID := ts | id | seq
g.lastID = nextID
g.lastTimestamp = g.timestamp
g.mutex.Unlock()
return ID(nextID)
}
// LastID returns a last generated ID.
func (g *Generator) LastID() ID {
return ID(g.lastID)
}
// LastSequence returns a last generated sequence.
func (g *Generator) LastSequence() int {
return int(g.sequence)
}
// LastTimestamp returns a last generated timestamp.
func (g *Generator) LastTimestamp() int64 {
return int64(g.lastTimestamp)
}