snowflake

package
v0.0.0-...-e41513e Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2020 License: Apache-2.0 Imports: 5 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IdWorker

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

结构为:

0---0000000000 0000000000 0000000000 0000000000 0 --- 00000 ---00000 ---0000000000 00 在上面的字符串中,第一位为未使用(实际上也可作为long的符号位),接下来的41位为毫秒级时间,然后5位datacenter标识位, 5位机器ID(并不算标识符,实际是为线程标识),然后12位该毫秒内的当前毫秒内的计数,加起来刚好64位,为一个Long型。

这样的好处是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由datacenter和机器ID作区分), 并且效率较高,经测试,snowflake每秒能够产生26万ID左右,完全满足需要。

func NewIdWorker

func NewIdWorker(workerId, datacenterId int64, twepoch int64) (*IdWorker, error)

NewIdWorker new a snowflake id generator object.

func (*IdWorker) NextId

func (id *IdWorker) NextId() (int64, error)

NextId get a snowflake id.

func (*IdWorker) NextIds

func (id *IdWorker) NextIds(num int) ([]int64, error)

NextIds get snowflake ids.

Jump to

Keyboard shortcuts

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