feat: id utils

This commit is contained in:
Bobo
2025-06-04 15:52:46 +08:00
parent 3f713e489b
commit 99c8f6e4c1
22 changed files with 859 additions and 304 deletions

25
id/sonyflake.go Normal file
View File

@@ -0,0 +1,25 @@
package id
import (
"sync"
"github.com/sony/sonyflake"
)
var (
sf *sonyflake.Sonyflake
sfMu sync.Mutex
)
func NewSonyflakeID() (uint64, error) {
// 64 位 ID = 39 位时间戳 + 8 位机器 ID + 16 位序列号
sfMu.Lock()
defer sfMu.Unlock()
if sf == nil {
sf = sonyflake.NewSonyflake(sonyflake.Settings{})
}
return sf.NextID()
}