-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnode_workers.go
33 lines (28 loc) · 843 Bytes
/
node_workers.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
package main
import (
"fmt"
"github.com/kkrypt0nn/spaceflake"
)
func main() {
node := spaceflake.NewNode(5)
worker := node.NewWorker() // If BaseEpoch not changed, it will use the EPOCH constant
sf, err := worker.GenerateSpaceflake()
if err != nil {
panic(err)
}
fmt.Println(sf.Decompose()) // map[id:<Spaceflake> nodeID:5 sequence:1 time:<timestamp> workerID:1]
worker.ID = 5
worker.Sequence = 1337
sf, err = worker.GenerateSpaceflake()
if err != nil {
panic(err)
}
fmt.Println(sf.Decompose()) // map[id:<Spaceflake> nodeID:5 sequence:1337 time:<timestamp> workerID:5]
node.ID = 2
worker.Sequence = 0 // We reset to auto incremented sequence
sf, err = worker.GenerateSpaceflake()
if err != nil {
panic(err)
}
fmt.Println(sf.Decompose()) // map[id:<Spaceflake> nodeID:2 sequence:3 time:<timestamp> workerID:5]
}