Skip to content

Commit

Permalink
refactor uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
msaf1980 committed Feb 18, 2023
1 parent 9d21bb8 commit 3355350
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/rand"
"encoding/binary"
"encoding/hex"
"sync"
"sync/atomic"
)

Expand All @@ -15,19 +14,18 @@ const (
var (
uuidSeed [24]byte
uuidCounter uint64
uuidSetup sync.Once
// unitsSlice = []byte("kmgtp")
)

func init() {
// Setup seed & counter once
if _, err := rand.Read(uuidSeed[:]); err != nil {
return
}
uuidCounter = binary.LittleEndian.Uint64(uuidSeed[:8])
}

// UUID generates an universally unique identifier (UUID)
func UUID() string {
// Setup seed & counter once
uuidSetup.Do(func() {
if _, err := rand.Read(uuidSeed[:]); err != nil {
return
}
uuidCounter = binary.LittleEndian.Uint64(uuidSeed[:8])
})
if atomic.LoadUint64(&uuidCounter) <= 0 {
return emptyUUID
}
Expand Down

0 comments on commit 3355350

Please sign in to comment.