sk-matrix-project/backend/golang/matrix-middle-service/pkg/utils/sonyflake/sonyflake.go
2023-03-24 11:46:51 +08:00

32 lines
584 B
Go

package sonyflake
import (
"fmt"
"github.com/sony/sonyflake"
"matrix-common/pkg/logger"
sf "matrix-middle-service/pkg/config/sonyflake"
"time"
)
var generator *sonyflake.Sonyflake
func Init(config *sf.Config) {
logger.Log().Debug("[sonyFlake] 初始化")
var s sonyflake.Settings
s.StartTime, _ = time.Parse("2006-01-02", config.StartTime)
generator = sonyflake.NewSonyflake(s)
}
func SonyFlake() *sonyflake.Sonyflake {
return generator
}
func NextId() uint64 {
id, _ := generator.NextID()
return id
}
func NextStrId() string {
return fmt.Sprintf("%d", NextId())
}