idgen

package
v1.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2023 License: MIT Imports: 4 Imported by: 41

Documentation

Overview

Package idgen is a snowflake id generator

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractTime added in v1.3.0

func ExtractTime(id int64) time.Time

func NextId

func NextId() int64

NextId .

func SetIdGenerator

func SetIdGenerator(options *IdGeneratorOptions)

SetIdGenerator .

Types

type DefaultIdGenerator

type DefaultIdGenerator struct {
	Options              *IdGeneratorOptions
	SnowWorker           ISnowWorker
	IdGeneratorException IdGeneratorException
}

func NewDefaultIdGenerator

func NewDefaultIdGenerator(options *IdGeneratorOptions) *DefaultIdGenerator

func (DefaultIdGenerator) ExtractTime added in v1.3.0

func (dig DefaultIdGenerator) ExtractTime(id int64) time.Time

func (DefaultIdGenerator) NewLong

func (dig DefaultIdGenerator) NewLong() int64

type IIdGenerator

type IIdGenerator interface {
	NewLong() uint64
}

type ISnowWorker

type ISnowWorker interface {
	NextId() int64
}

func NewSnowWorkerM1

func NewSnowWorkerM1(options *IdGeneratorOptions) ISnowWorker

NewSnowWorkerM1 .

func NewSnowWorkerM2

func NewSnowWorkerM2(options *IdGeneratorOptions) ISnowWorker

type IdGeneratorException

type IdGeneratorException struct {
	// contains filtered or unexported fields
}

func (IdGeneratorException) IdGeneratorException

func (e IdGeneratorException) IdGeneratorException(message ...interface{})

type IdGeneratorOptions

type IdGeneratorOptions struct {
	Method            uint16 // 雪花计算方法,(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(与计算能力有关)
}

func NewIdGeneratorOptions

func NewIdGeneratorOptions(workerId uint16) *IdGeneratorOptions

type OverCostActionArg

type OverCostActionArg struct {
	ActionType             int32
	TimeTick               int64
	WorkerId               uint16
	OverCostCountInOneTerm int32
	GenCountInOneTerm      int32
	TermIndex              int32
}

func (OverCostActionArg) OverCostActionArg

func (ocaa OverCostActionArg) OverCostActionArg(workerId uint16, timeTick int64, actionType int32, overCostCountInOneTerm int32, genCountWhenOverCost int32, index int32)

type SnowWorkerM1

type SnowWorkerM1 struct {
	BaseTime          int64  // 基础时间
	WorkerId          uint16 // 机器码
	WorkerIdBitLength byte   // 机器码位长
	SeqBitLength      byte   // 自增序列数位长
	MaxSeqNumber      uint32 // 最大序列数(含)
	MinSeqNumber      uint32 // 最小序列数(含)
	TopOverCostCount  uint32 // 最大漂移次数

	sync.Mutex
	// contains filtered or unexported fields
}

SnowWorkerM1 .

func (*SnowWorkerM1) BeginOverCostAction

func (m1 *SnowWorkerM1) BeginOverCostAction(useTimeTick int64)

func (*SnowWorkerM1) BeginTurnBackAction

func (m1 *SnowWorkerM1) BeginTurnBackAction(useTimeTick int64)

func (*SnowWorkerM1) CalcId added in v1.2.1

func (m1 *SnowWorkerM1) CalcId(useTimeTick int64) int64

CalcID .

func (*SnowWorkerM1) CalcTurnBackId added in v1.2.1

func (m1 *SnowWorkerM1) CalcTurnBackId(useTimeTick int64) int64

CalcTurnBackID .

func (*SnowWorkerM1) DoGenIdAction added in v1.2.1

func (m1 *SnowWorkerM1) DoGenIdAction(arg *OverCostActionArg)

DoGenIDAction .

func (*SnowWorkerM1) EndOverCostAction

func (m1 *SnowWorkerM1) EndOverCostAction(useTimeTick int64)

func (*SnowWorkerM1) EndTurnBackAction

func (m1 *SnowWorkerM1) EndTurnBackAction(useTimeTick int64)

func (*SnowWorkerM1) GetCurrentTimeTick

func (m1 *SnowWorkerM1) GetCurrentTimeTick() int64

GetCurrentTimeTick .

func (*SnowWorkerM1) GetNextTimeTick

func (m1 *SnowWorkerM1) GetNextTimeTick() int64

GetNextTimeTick .

func (*SnowWorkerM1) NextId

func (m1 *SnowWorkerM1) NextId() int64

NextId .

func (*SnowWorkerM1) NextNormalId added in v1.2.1

func (m1 *SnowWorkerM1) NextNormalId() int64

NextNormalID .

func (*SnowWorkerM1) NextOverCostId added in v1.2.1

func (m1 *SnowWorkerM1) NextOverCostId() int64

type SnowWorkerM2

type SnowWorkerM2 struct {
	*SnowWorkerM1
}

func (SnowWorkerM2) NextId added in v1.2.1

func (m2 SnowWorkerM2) NextId() int64

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL