Files
certmanager/pkg/auxid/genid.go
Олег Бородин 42cd5f4800 working changes
2024-07-30 23:14:54 +02:00

33 lines
506 B
Go

package auxid
import (
"math/rand"
"sync"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
var (
idMtx sync.Mutex
lastID int64
)
func GenID() int64 {
// 53 bit limit for js
// See https://stackoverflow.com/questions/1379934/large-numbers-erroneously-rounded-in-javascript
idMtx.Lock()
defer idMtx.Unlock()
for {
id := (time.Now().UnixNano() / 1000) // - 10000000000000
if id != lastID {
lastID = id
return id
}
time.Sleep(1 * time.Microsecond)
}
//10467328383814
}