-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathUtility.go
414 lines (369 loc) · 10.1 KB
/
Utility.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package s
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
redigo "github.com/gomodule/redigo/redis"
"github.com/ssgo/redis"
"github.com/ssgo/u"
)
var uidServerDate string
var uidServerStart int64
var uidServerIndex int64 = -1
var uidSec int
var uidIndexes = map[int]map[uint]bool{}
var uidLock = sync.Mutex{}
var uidShutdownHookSet = false
var fileLocksLock = sync.Mutex{}
var fileLocks = map[string]*sync.Mutex{}
func resetUtilityMemory() {
uidServerDate = ""
uidServerStart = 0
uidServerIndex = -1
uidSec = 0
uidIndexes = map[int]map[uint]bool{}
uidShutdownHookSet = false
fileLocks = map[string]*sync.Mutex{}
}
func trySetServerId(rdConn redigo.Conn, hkey string, sid int64) (bool, error) {
if rdConn != nil {
r, err := rdConn.Do("HSETNX", hkey, sid, true)
if err == nil {
if i, ok := r.(int64); ok && i == 1 {
return true, nil
}
}
return false, err
} else {
return false, nil
}
}
func uniqueId() []byte {
tm := time.Now()
date := tm.Format("0102")
// 检查每天重新排列的服务器编号
if date != uidServerDate {
rd1 := getRedis1()
var rdConn1 redigo.Conn
if rd1 != nil {
rdConn1 = rd1.GetConnection()
}
makeServerIndexTimes := 0
makeServerIndexOk := false
uidLock.Lock()
if date != uidServerDate {
uidServerDate = date
uidServerStart = tm.Unix()
if rdConn1 == nil {
// 尝试环境变量中指定的ServerId
if !makeServerIndexOk && os.Getenv("SERVER_ID") != "" {
uidServerIndex = u.Int64(os.Getenv("SERVER_ID"))
if uidServerIndex >= 0 && uidServerIndex < 238328 {
makeServerIndexOk = true
logInfo("get server id for unique id over env", "uidServerId", uidServerIndex)
}
}
// 尝试文件中保存的ServerId
if !makeServerIndexOk && u.FileExists(".server_id") {
serverIdInFile, err := u.ReadFile(".server_id")
if err == nil {
uidServerIndex = u.Int64(serverIdInFile)
if uidServerIndex >= 0 && uidServerIndex < 238328 {
makeServerIndexOk = true
logInfo("get server id for unique id over file", "uidServerId", uidServerIndex, "uidFile", serverIdInFile)
}
}
}
if !makeServerIndexOk {
uidServerIndex = u.GlobalRand1.Int63n(238328)
}
} else {
// 检查Hash
hkey := "USI" + date
hexists := false
if rdConn1 != nil {
r, err := rdConn1.Do("EXISTS", hkey)
if err == nil {
i, ok := r.(int64)
hexists = ok && i == 1
}
}
// 先尝试沿用旧ID
if uidServerIndex >= 0 {
if ok, _ := trySetServerId(rdConn1, hkey, uidServerIndex); ok {
makeServerIndexOk = true
logInfo("get server id for unique id over old", "uidServerId", uidServerIndex)
}
}
// 尝试环境变量中指定的ServerId
if !makeServerIndexOk && os.Getenv("SERVER_ID") != "" {
uidServerIndex = u.Int64(os.Getenv("SERVER_ID"))
if uidServerIndex >= 0 && uidServerIndex < 238328 {
if ok, _ := trySetServerId(rdConn1, hkey, uidServerIndex); ok {
makeServerIndexOk = true
logInfo("get server id for unique id over env", "uidServerId", uidServerIndex)
}
}
}
// 尝试文件中保存的ServerId
if !makeServerIndexOk && u.FileExists(".server_id") {
serverIdInFile, err := u.ReadFile(".server_id")
if err == nil {
uidServerIndex = u.Int64(serverIdInFile)
if uidServerIndex >= 0 && uidServerIndex < 238328 {
if ok, _ := trySetServerId(rdConn1, hkey, uidServerIndex); ok {
makeServerIndexOk = true
logInfo("get server id for unique id over file", "uidServerId", uidServerIndex, "uidFile", serverIdInFile)
}
}
}
}
// 如果沿用旧ID失败,尝试碰撞100次找到新的空闲索引
if !makeServerIndexOk {
for makeServerIndexTimes = 0; makeServerIndexTimes < 100; makeServerIndexTimes++ {
uidServerIndex = u.GlobalRand1.Int63n(238328)
if ok, err := trySetServerId(rdConn1, hkey, uidServerIndex); ok {
makeServerIndexOk = true
_ = u.WriteFile(".server_id", u.String(uidServerIndex))
logInfo("get server id for unique id over hit", "uidServerId", uidServerIndex)
break
} else if err != nil {
break
}
}
}
// 如果尝试100次碰撞失败,使用累加器来获得空闲索引
if !makeServerIndexOk {
// 1000次随机没有命中的话,使用计数器顺序使用
if rdConn1 != nil {
indexKey := fmt.Sprint("USI", date, "Index")
for {
makeServerIndexTimes++
r, err := rdConn1.Do("INCR", indexKey)
if err == nil {
if i, ok := r.(int64); ok {
if uidServerIndex >= 238328 {
break
}
if i == 1 {
// 第一次创建累加器,设置过期
_, _ = rdConn1.Do("EXPIRE", indexKey, 86400)
}
uidServerIndex = i
if ok, err := trySetServerId(rdConn1, hkey, uidServerIndex); ok {
makeServerIndexOk = true
_ = u.WriteFile(".server_id", u.String(uidServerIndex))
logInfo("get server id for unique id over incr", "uidServerId", uidServerIndex)
break
} else if err != nil {
break
}
} else {
break
}
} else {
break
}
}
}
}
if !makeServerIndexOk {
uidServerIndex = u.GlobalRand1.Int63n(238328)
}
// 第一次创建Hash,设置过期
if !hexists && rdConn1 != nil {
_, _ = rdConn1.Do("EXPIRE", hkey, 86400)
}
if makeServerIndexOk && !uidShutdownHookSet {
uidShutdownHookSet = true
AddShutdownHook(func() {
rd1 := getRedis1()
if rd1 != nil {
rd1.HDEL("USI"+uidServerDate, u.String(uidServerIndex))
}
})
}
}
}
uidLock.Unlock()
if rdConn1 != nil {
_ = rdConn1.Close()
rdConn1 = nil
}
if !makeServerIndexOk {
if rd1 != nil {
ServerLogger.Error("failed to make unique id server index", "times", makeServerIndexTimes)
}
} else if makeServerIndexTimes >= 100 {
ServerLogger.Warning("make unique id server index slowly", "times", makeServerIndexTimes)
}
}
// 检查秒内的索引值,换秒后重新计数
var secIndex uint
sec := int(tm.Unix() - uidServerStart)
makeSecIndexTimes := 0
makeSecIndexOk := false
uidLock.Lock()
if uidSec != sec {
// 清除多余的数据
for k := range uidIndexes {
if k != uidSec {
delete(uidIndexes, k)
}
}
// 创建新的每秒索引容器
uidIndexes[sec] = map[uint]bool{}
uidSec = sec
// uidSecIndex = 0
}
if uidIndexes[sec] == nil {
uidIndexes[sec] = map[uint]bool{}
}
if len(uidIndexes[sec]) < 200000 {
for makeSecIndexTimes = 0; makeSecIndexTimes < 10000; makeSecIndexTimes++ {
secIndex = uint(u.GlobalRand2.Int63n(238328))
if !uidIndexes[sec][secIndex] {
uidIndexes[sec][secIndex] = true
makeSecIndexOk = true
break
}
}
}
uidLock.Unlock()
if !makeSecIndexOk {
uid := u.AppendInt(nil, uint64(u.GlobalRand1.Intn(56800235583)))
uid = u.AppendInt(uid, uint64(u.GlobalRand1.Intn(56800235583)))
for len(uid) < 11 {
uid = u.AppendInt(uid, uint64(u.GlobalRand1.Intn(56800235583)))
}
if len(uid) > 11 {
uid = uid[0:11]
}
ServerLogger.Warning("failed to make unique id second index,use random unique id", "times", makeSecIndexTimes, "second", uidSec, "indexSize", len(uidIndexes[uidSec]), "randomUid", string(uid))
return uid
} else if makeSecIndexTimes >= 1000 {
ServerLogger.Warning("make unique id second index slowly", "times", makeSecIndexTimes, "second", uidSec, "indexSize", len(uidIndexes[uidSec]))
}
// 添加序号
uid := u.AppendInt(nil, uint64(secIndex))
// 添加时间戳
sec1 := u.Bytes(tm.Unix())
secLen := len(sec1)
sec2 := make([]byte, secLen+1)
for i := 0; i < secLen; i++ {
sec2[i+1] = sec1[secLen-i-1]
}
sec2[0] = byte(u.Int(u.GlobalRand2.Intn(10)) + 48)
timeStr := u.AppendInt(nil, u.Uint64(sec2))
for len(timeStr) < 7 {
timeStr = append(timeStr, '9')
}
uid = append(uid, timeStr...)
// 添加服务器序号
serverIndexStr := u.AppendInt(nil, uint64(uidServerIndex))
for len(serverIndexStr) < 3 {
serverIndexStr = append(serverIndexStr, '9')
}
uid = append(uid, serverIndexStr...)
return uid
}
func catUniqueId(size int) string {
id := uniqueId()
if len(id) > size {
return string(id[0:size])
}
for i := size - len(id); i > 0; i-- {
var c int
if i%2 == 0 {
c = u.GlobalRand1.Intn(62)
} else {
c = u.GlobalRand2.Intn(62)
}
id = append(id, u.EncodeInt(uint64(c))[0])
}
return string(id)
}
func UniqueId() string {
return string(uniqueId())
}
func UniqueId12() string {
return catUniqueId(12)
}
func UniqueId14() string {
return catUniqueId(14)
}
func UniqueId16() string {
return catUniqueId(16)
}
func UniqueId20() string {
return catUniqueId(20)
}
func Id6(space string) string {
return makeId(space, u.Id6)
}
func Id8(space string) string {
return makeId(space, u.Id8)
}
func Id10(space string) string {
return makeId(space, u.Id10)
}
func Id12(space string) string {
return makeId(space, u.Id12)
}
func Id6L(space string) string {
return makeId(space, makeId6L)
}
func Id8L(space string) string {
return makeId(space, makeId8L)
}
func Id10L(space string) string {
return makeId(space, makeId10L)
}
func Id12L(space string) string {
return makeId(space, makeId12L)
}
func makeId6L() string {
return strings.ToLower(u.Id6())
}
func makeId8L() string {
return strings.ToLower(u.Id8())
}
func makeId10L() string {
return strings.ToLower(u.Id10())
}
func makeId12L() string {
return strings.ToLower(u.Id12())
}
// 分配唯一编号
func makeId(space string, idMaker func() string) string {
var rd1 *redis.Redis
if Config.IdServer != "" {
rd1 = redis.GetRedis(Config.IdServer, ServerLogger)
}
var id string
for i := 0; i < 10000; i++ {
id = idMaker()
key := fmt.Sprint("ID", space, id[0:2])
field := id[2:]
if rd1 != nil {
if rd1.HEXISTS(key, field) {
continue
} else {
rd1.HSET(key, field, "")
return id
}
} else {
idFile := filepath.Join("data", "ids", id[0:2], id[2:4], id)
if u.FileExists(idFile) {
continue
} else {
_ = u.WriteFile(idFile, "")
return id
}
}
}
return id
}