-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathid_cfg.go
146 lines (127 loc) · 2.64 KB
/
id_cfg.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package xid
import (
"log"
"math"
"strings"
"time"
)
type idGeneratorsCfg struct {
types []IdType
maxBatchNum int
nodeAlloc NodeAllocator
currentNodeId int
snake *snakeConfig
redisXidNodesHashKey string
}
var defaultCfg = &idGeneratorsCfg{
types: []IdType{ID_Snake},
currentNodeId: -1,
maxBatchNum: 1000,
nodeAlloc: nil,
snake: &snakeConfig{
startTime: 0,
timeUnit: Second,
nodeBits: 4,
stepBits: 16,
},
redisXidNodesHashKey: "xid:nodes",
}
type Option func(*idGeneratorsCfg)
func Options(opts ...Option) {
for _, opt := range opts {
opt(defaultCfg)
}
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func Init() {
if defaultCfg.nodeAlloc == nil {
defaultCfg.nodeAlloc = NewNodeAllocationSingle()
}
// 同时支持多种 ID 类型时,需要获取最小的 nodeMax
nodeMax := math.MaxInt
for _, t := range defaultCfg.types {
switch t {
case ID_Snake:
nodeMax = min(nodeMax, -1^(-1<<defaultCfg.snake.nodeBits))
case ID_14:
nodeMax = min(nodeMax, 9)
}
}
defaultCfg.currentNodeId = defaultCfg.nodeAlloc.Node(nodeMax)
}
func GetIdTypes() []IdType {
return defaultCfg.types
}
func MaxBatchNum(num int) Option {
return func(o *idGeneratorsCfg) {
o.maxBatchNum = num
}
}
func NodeAlloc(nodeAlloc NodeAllocator) Option {
return func(o *idGeneratorsCfg) {
o.nodeAlloc = nodeAlloc
}
}
func SnakeStartTime(startData string) Option {
time, err := time.Parse("2006-01-02 15:04:05", startData)
if err != nil {
log.Fatal(err)
}
return func(o *idGeneratorsCfg) {
o.snake.startTime = time.Unix()
}
}
func SnakeTimeUnit(unitDesc string) Option {
unit := Second
switch unitDesc {
case "s":
unit = Second
case "ms":
unit = Millisecond
case "10ms":
unit = Millisecond10
case "100ms":
unit = Millisecond100
default:
log.Fatalf("时间单位错误:%s。只接受:s,ms,10ms,100ms", unitDesc)
}
return func(o *idGeneratorsCfg) {
o.snake.timeUnit = unit
}
}
func SnakeNodeBits(bits int) Option {
return func(o *idGeneratorsCfg) {
o.snake.nodeBits = uint(bits)
}
}
func SnakeStepBits(bits int) Option {
return func(o *idGeneratorsCfg) {
o.snake.stepBits = uint(bits)
}
}
func RedisXidNodesHashKey(key string) Option {
return func(o *idGeneratorsCfg) {
o.redisXidNodesHashKey = key
}
}
func RunTypes(types string) Option {
return func(o *idGeneratorsCfg) {
o.types = []IdType{}
types := strings.Split(types, "+")
for _, t := range types {
switch t {
case "snake":
o.types = append(o.types, ID_Snake)
case "id14":
o.types = append(o.types, ID_14)
default:
log.Fatalf("不支持的 ID 类型:%s", t)
}
}
}
}