You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

41 lines
786 B

package utils
import (
"github.com/bwmarrin/snowflake"
"github.com/cloudwego/kitex/pkg/klog"
)
type SnowflakeHandle struct {
node *snowflake.Node
}
func NewSnowflakeHandle(node int64) *SnowflakeHandle {
n, err := snowflake.NewNode(node)
if err != nil {
panic(err)
}
return &SnowflakeHandle{
node: n,
}
}
func (h *SnowflakeHandle) GetId() snowflake.ID {
return h.node.Generate()
}
var defaultNode *snowflake.Node
// 1 Bit Unused | 41 Bit Timestamp | 10 Bit NodeID | 12 Bit Sequence ID
// 传入的 node 用于控制 10bit 的 NodeID,确保不同机器唯一
func InitSnowFlake(node int64) {
n, err := snowflake.NewNode(node)
if err != nil {
klog.Info(err)
return
}
defaultNode = n
}
func GetSnowFlakeId() snowflake.ID {
return defaultNode.Generate()
}