-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgoid.go
68 lines (57 loc) · 1.24 KB
/
goid.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
package goid
import (
"math/rand"
"time"
)
const (
// Base64 web-safe chars, but ordered by ASCII.
charID = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
)
// GID is convert byte to string
type GID string
var lastTime int64
// New is used for generate 20 random id base on base64 web save chars
func New() GID {
id := [20]byte{}
timeMs := time.Now().UTC().UnixNano() / 1e6
lastID := generateLastID(timeMs, lastTime)
lastTime = timeMs
// generate last 12 byte of id
for i := 0; i < 12; i++ {
id[(len(id)-1)-i] = charID[lastID[i]]
}
// Genererate first 8 byte of id
for i := 7; i >= 0; i-- {
n := int(timeMs % 64)
id[i] = charID[n]
timeMs = timeMs / 64
}
return GID(id[:])
}
func (s GID) String() string {
return string(s)
}
var lastIDS [12]int
func generateLastID(timeMs, lastTime int64) [12]int {
var randNumber *rand.Rand
randNumber = rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
if timeMs == lastTime {
// incremen last rand id
lastIDS = inLastRandID(lastIDS)
} else {
for i := 0; i < len(lastIDS); i++ {
lastIDS[i] = randNumber.Intn(64)
}
}
return lastIDS
}
func inLastRandID(r [12]int) [12]int {
for i := 0; i < 12; i++ {
r[i]++
if r[i] < 64 {
break
}
r[i] = 0
}
return r
}