-
Notifications
You must be signed in to change notification settings - Fork 4
/
sequence.go
47 lines (39 loc) · 885 Bytes
/
sequence.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
package factory
import (
"sync"
)
type sequence struct {
first int64
value int64
roundStarted bool
mux sync.Mutex
}
// peek returns the current cursor number of the sequence
func (seq *sequence) peek() int64 {
seq.mux.Lock()
defer seq.mux.Unlock()
if seq.value < seq.first {
return seq.first
}
return seq.value
}
// next move the cursor of the sequence to next number
func (seq *sequence) next() int64 {
seq.mux.Lock()
defer seq.mux.Unlock()
if seq.value < seq.first || (seq.value == seq.first && !seq.roundStarted) {
seq.value = seq.first
seq.roundStarted = true
} else {
seq.value = seq.value + 1
}
return seq.value
}
// rewind moves the cursor of the sequence to the start number of the sequence
func (seq *sequence) rewind() {
seq.mux.Lock()
defer seq.mux.Unlock()
seq.value = seq.first
seq.roundStarted = false
return
}