goid

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: MIT Imports: 10 Imported by: 0

README

go-id

id generator, which generates globally unique id, mini Snowflake

特性

  1. 支持秒级、毫秒级ID生成
  2. ID递增、不重复
  3. 支持分布式(配置节点)ID生成
  4. 采用53位整型,支持 json 整型传输解析,不会超限导致解析错误(虽然有部分库已经支持bigint,但需要手动配置,so,不超过53位就无任何顾虑)
  5. 采用无锁技术,生成速度极快,每秒可生成两百多万不重复且递增的ID(受53位长度限制)
  6. 每(毫)秒生成超限会等待直至下一(毫)秒,计数器会重置从 1 开始重新计数
  7. 服务器时钟回拨也不会生成重复ID(支持等待及获取网络时间,默认 3s)
  8. 使用简单(单机使用无需任何配置,分布式使用,设置节点即可)

采用三段设计:

第一段长度 32 ~ 43 位,存放(毫)秒级时间戳
第二段长度 0 ~ 20 位,存放机器码
第三段长度 2 ~ 21 位,存放计数器(递增)

名称 方案 最大支持时间 极限速度(N=0) 说明
ID Second 32+N+(21-N) 2106-02-07 14:28:15 2,097,151 / s N = 0 OR (N >1 AND N <20)
ID2 Second 33+N+(20-N) 2242-03-16 20:56:31 1,048,575 / s N = 0 OR (N >1 AND N <19)
ID3 Millisecond 42+N+(11-N) 2109-05-15 15:35:11 2,047 / ms N = 0 OR (N >1 AND N <10)
ID3 Millisecond 43+N+(10-N) 2248-09-26 23:10:22 1,023 / ms N = 0 OR (N >1 AND N <9)

N 表示节点占用位长度,可为 0 (适合单机使用)

使用

go get github.com/ace-zhaoy/go-id
快速使用
id := goid.GenID()
id2 := goid.GenID2()
id3 := goid.GenID3()
设置节点(分布式)
// 节点长度为 10(支持 1023 台机器),当前节点标识是1
goid.GetID().SetNode(1, 10)

节点越多,每个节点每秒生成的ID数量就越低,所以根据预期节点按需设置

设置序列号起始值以及间隔值
// id末尾的序列号间隔 1024
goid.GetID().SetDelta(1024)
设置随机增量
// id末尾的序列号每次在 1 ~ 1024 之间随机增加,范围不宜太大
goid.GetID().SetRandomDelta(1024)

在机器不多的情况下,可以设置随机增量来避免多机器ID重复

不使用包自带的全局变量
// 保存好变量
myID := goid.NewID()
id := myID.Generate()
设置服务器时钟回拨最大等待时间及 NTP Server
goid.GetID().SetMaxBacktrackWait(10 * time.Second)
goid.GetID().SetNTPServer("pool.ntp.org")

Documentation

Index

Constants

View Source
const (
	MaxBits = 53
)

Variables

This section is empty.

Functions

func GenID

func GenID() int64

func GenID2

func GenID2() int64

func GenID3

func GenID3() int64

func GenerateMachineCode added in v1.0.3

func GenerateMachineCode(bits int8) (code int, err error)

func GetLocalIP added in v1.0.3

func GetLocalIP() (ip string, err error)

func ResolveID

func ResolveID(id int64, oid *ID) (timestamp int64, counter uint32)

func ResolveID2

func ResolveID2(id int64, oid *ID2) (timestamp int64, counter uint32)

func ResolveID3

func ResolveID3(id int64, oid *ID3) (timestamp int64, counter uint16)

Types

type ID

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

func GetID

func GetID() *ID

func NewID

func NewID() *ID

func (*ID) Generate

func (i *ID) Generate() int64

func (*ID) GetDelta

func (i *ID) GetDelta() uint32

func (*ID) GetMaxBacktrackWait added in v1.0.2

func (i *ID) GetMaxBacktrackWait() time.Duration

func (*ID) GetNTPServer added in v1.0.2

func (i *ID) GetNTPServer() string

func (*ID) GetNode

func (i *ID) GetNode() (node uint32, nodeBits uint8)

func (*ID) GetRandomDelta

func (i *ID) GetRandomDelta() uint32

func (*ID) SetDelta

func (i *ID) SetDelta(d uint32)

func (*ID) SetMaxBacktrackWait added in v1.0.2

func (i *ID) SetMaxBacktrackWait(d time.Duration)

func (*ID) SetNTPServer added in v1.0.2

func (i *ID) SetNTPServer(s string)

func (*ID) SetNode

func (i *ID) SetNode(node uint32, nodeBits uint8)

func (*ID) SetRandomDelta

func (i *ID) SetRandomDelta(r uint32)

type ID2

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

func GetID2

func GetID2() *ID2

func NewID2

func NewID2() *ID2

func (*ID2) Generate

func (i *ID2) Generate() int64

func (*ID2) GetDelta

func (i *ID2) GetDelta() uint32

func (*ID2) GetMaxBacktrackWait added in v1.0.2

func (i *ID2) GetMaxBacktrackWait() time.Duration

func (*ID2) GetNode

func (i *ID2) GetNode() (node uint32, nodeBits uint8)

func (*ID2) GetRandomDelta

func (i *ID2) GetRandomDelta() uint32

func (*ID2) SetDelta

func (i *ID2) SetDelta(d uint32)

func (*ID2) SetMaxBacktrackWait added in v1.0.2

func (i *ID2) SetMaxBacktrackWait(d time.Duration)

func (*ID2) SetNTPServer added in v1.0.2

func (i *ID2) SetNTPServer(s string)

func (*ID2) SetNode

func (i *ID2) SetNode(node uint32, nodeBits uint8)

func (*ID2) SetRandomDelta

func (i *ID2) SetRandomDelta(r uint32)

type ID3

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

func GetID3

func GetID3() *ID3

func NewID3

func NewID3() *ID3

func (*ID3) Generate

func (i *ID3) Generate() int64

func (*ID3) GetDelta

func (i *ID3) GetDelta() uint16

func (*ID3) GetNode

func (i *ID3) GetNode() (node uint16, nodeBits uint8)

func (*ID3) GetRandomDelta

func (i *ID3) GetRandomDelta() uint16

func (*ID3) SetBits

func (i *ID3) SetBits(bits uint8)

func (*ID3) SetDelta

func (i *ID3) SetDelta(d uint16)

func (*ID3) SetNode

func (i *ID3) SetNode(node uint16, nodeBits uint8)

func (*ID3) SetRandomDelta

func (i *ID3) SetRandomDelta(r uint16)

Jump to

Keyboard shortcuts

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