Initial commit

This commit is contained in:
jdl
2023-10-13 11:43:27 +02:00
commit 71eb6b0c7e
121 changed files with 11493 additions and 0 deletions

32
lib/idgen/gen.go Normal file
View File

@@ -0,0 +1,32 @@
package idgen
import (
"sync"
"time"
)
var (
lock sync.Mutex
ts uint64 = uint64(time.Now().Unix())
counter uint64 = 1
counterMax uint64 = 1 << 28
)
// Next can generate ~268M ints per second for ~1000 years.
func Next() uint64 {
lock.Lock()
defer lock.Unlock()
tt := uint64(time.Now().Unix())
if tt > ts {
ts = tt
counter = 1
} else {
counter++
if counter == counterMax {
panic("Too many IDs.")
}
}
return ts<<28 + counter
}

11
lib/idgen/gen_test.go Normal file
View File

@@ -0,0 +1,11 @@
package idgen
import (
"testing"
)
func BenchmarkNext(b *testing.B) {
for i := 0; i < b.N; i++ {
Next()
}
}