feat: id utils
This commit is contained in:
25
id/sonyflake.go
Normal file
25
id/sonyflake.go
Normal 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()
|
||||
}
|
||||
Reference in New Issue
Block a user