generator

package
v0.0.0-...-b628505 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	TimeBit     = 39
	NodeBit     = 16
	SequenceBit = 8
)

Variables

This section is empty.

Functions

func Decompose

func Decompose(id uint64) map[string]uint64

Decompose 根据id分解为mapping包含这组数据的所有信息

func IpToUint16

func IpToUint16(ip net.IP) (uint16, error)

IpToUint16 将IP地址转化为uint16

func LocalIpToUint16

func LocalIpToUint16() (uint16, error)

LocalIpToUint16 本地IP转化为uint16

Types

type Generator

type Generator interface {
	// NextID 获取下一个ID
	NextID() (uint64, error)
}

Generator 发号器

func NewSnowflake

func NewSnowflake(startTime time.Time, nodeID uint16) Generator

NewSnowflake 初始化 StartTime 起始时间 NodeID 服务器ID

Example
// 生成对象
generate := NewSnowflake(time.Now(), 1)
nid, err := generate.NextID()
if err != nil {
	// 处理错误
}
_ = nid
Output:

type Snowflake

type Snowflake struct {
	// 互斥锁
	sync.Mutex
	// contains filtered or unexported fields
}

Snowflake is a distributed unique ID generator.

func (*Snowflake) NextID

func (s *Snowflake) NextID() (uint64, error)

NextID 获取下一个ID

Jump to

Keyboard shortcuts

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