-
Notifications
You must be signed in to change notification settings - Fork 134
/
uuid.go
37 lines (31 loc) · 1.05 KB
/
uuid.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
package util
import (
"math/rand"
"strings"
"sync"
"time"
"github.com/oklog/ulid"
)
var (
entropy = ulid.Monotonic(rand.New(rand.NewSource(time.Now().UnixNano())), 0)
m sync.Mutex
)
// NewULID returns a new ULID for the current time generated from a global RNG.
// The ULID is returned as a string converted to lower-case to ensure it is a valid DNS subdomain name; see
// https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names
func NewULID() string {
return StringFromUlid(ULID())
}
// StringFromUlid returns a string representation of a proto UUID.
// Because Kubernetes requires ids to be valid DNS subdomain names, the string is returned in lower-case; see
// https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names
func StringFromUlid(id ulid.ULID) string {
return strings.ToLower(id.String())
}
// ULID returns a new ULID for the current time generated from a global RNG.
func ULID() ulid.ULID {
m.Lock()
defer m.Unlock()
id := ulid.MustNew(ulid.Now(), entropy)
return id
}