snowflake

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2022 License: MIT Imports: 4 Imported by: 1

README

snowflake

Twitter 的 Snowflake 算法 的 Go 实现

Documentation

Overview

Twitter 的 Snowflake 算法 的实现

SnowFlake 的结构如下(每部分用-分开): 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 - 最高位是符号位,正数是 0,负数是 1, id 一般是正数,因此最高位固定是 0; - 41 位时间戳(毫秒级),注意,41 位时间戳不是存储当前时间的时间戳,而是存储时间戳的差值(当前时间戳 - 开始时间戳),这样能存的时间更长,开始时间一般指定为项目启动时间,由程序指定。可以使用 69 年:`(1<<41)/(1000*60*60*24*365)`; - 10 位的机器相关位,可以部署在 1024 个节点,包括 5 位的 datacenterId(数据中心 ID) 和 5 位 workerId(工作机器 ID); - 12 位系列号,毫秒内的计数。12 位的计数顺序号支持每个节点每毫秒(同一机器,同一时间戳)产生 4096 个 ID 序号;

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SnowFlake

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

func New

func New() *SnowFlake

func NewWith

func NewWith(startTime time.Time, ids ...uint8) *SnowFlake

NewWith 给定开始时间和可选的 dataCenterID 和 workerID(注意两者的顺序) 如果 ids 没传,则使用 machineID

func (*SnowFlake) NextID

func (s *SnowFlake) NextID() int64

NextID 获取一个 ID

func (*SnowFlake) String

func (s *SnowFlake) String() string

Jump to

Keyboard shortcuts

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