You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool/nrandom/snowflake/options.go

46 lines
2.1 KiB
Go

1 year ago
package snowflake
import "time"
type Method byte
const (
MethodSnowflakeOffset Method = iota + 1
MethodSnowflake
)
type Options struct {
Method Method // 雪花计算方法,1-漂移算法|2-传统算法默认1
BaseTime int64 // 基础时间ms单位不能超过当前系统时间
WorkerId uint16 // 机器码,必须由外部设定,最大值 2^WorkerIdBitLength-1
WorkerIdBitLength byte // 机器码位长默认值6取值范围 [1, 15](要求:序列数位长+机器码位长不超过22
SeqBitLength byte // 序列数位长默认值6取值范围 [3, 21](要求:序列数位长+机器码位长不超过22
MaxSeqNumber uint32 // 最大序列数(含),设置范围 [MinSeqNumber, 2^SeqBitLength-1]默认值0表示最大序列数取最大值2^SeqBitLength-1]
MinSeqNumber uint32 // 最小序列数默认值5取值范围 [5, MaxSeqNumber]每毫秒的前5个序列数对应编号0-4是保留位其中1-4是时间回拨相应预留位0是手工新值预留位
TopOverCostCount uint32 // 最大漂移次数默认2000推荐范围500-10000与计算能力有关
}
var defaultOptions = &Options{
Method: MethodSnowflakeOffset,
BaseTime: time.Date(2022, time.February, 1, 0, 0, 0, 0, time.Local).UnixNano() / 1e6, // 2022-01-01 00:00:00 local
WorkerId: 1,
WorkerIdBitLength: 6,
SeqBitLength: 16, // 50w并发建议10 500w建议14-16
MaxSeqNumber: 0,
MinSeqNumber: 5,
TopOverCostCount: 5000, // 500w并发建议5000-8000
}
func NewOptions(workerId uint16) *Options {
return &Options{
WorkerId: workerId,
Method: defaultOptions.Method,
BaseTime: defaultOptions.BaseTime,
WorkerIdBitLength: defaultOptions.WorkerIdBitLength,
SeqBitLength: defaultOptions.SeqBitLength,
MaxSeqNumber: defaultOptions.MaxSeqNumber,
MinSeqNumber: defaultOptions.MinSeqNumber,
TopOverCostCount: defaultOptions.TopOverCostCount,
}
}