snowflake

package
v0.0.0-...-cda2eac Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2023 License: GPL-3.0 Imports: 6 Imported by: 0

README

snowflake

Twitter snowflake worker - Twitter雪花ID计算器

twitter announcing

Introduction

The default Twitter format shown below.

snowflake.png

  • Default: 1-41-10(5-5)-12, time accurecy is millsecond
  • Baidu-Generator: 1-28-22-13, time accurecy is second
  • nodeBits = DatacenterBits + workerBits
Usage

import trellis.tech/trellis/common.v1/snowflake



// You can set your epoch time
// but do not set the time before 69 years ago, then you should get the overflowed number

worker, _ := snowflake.NewWorker()  // below pow(2, nodeBits) = 1024

// get by for
id := worker.Next()

// get by sleep until Millisecond
id = worker.NextSleep()
Benchmark
go test -bench=. -benchmem  -run=none

goos: darwin
goarch: amd64
pkg: trellis.tech/trellis/common.v1/snowflake
cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkNext-8                   	 4927034	     244.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkNextMaxSequence-8        	 9595654	     119.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkNextNoSequence-8         	    1213	    998895 ns/op	       0 B/op	       0 allocs/op
BenchmarkNextSleep-8              	     934	   1375943 ns/op	       0 B/op	       0 allocs/op
BenchmarkNextSleepMaxSequence-8   	 9475443	     119.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkNextSleepNoSequence-8    	     914	   1367458 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	trellis.tech/trellis/common.v1/snowflake	8.104s

goos: darwin
goarch: amd64
pkg: trellis.tech/trellis/common.v1/snowflake
cpu: VirtualApple @ 2.50GHz
BenchmarkNext-10                    	 4931802	     244.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkNextMaxSequence-10         	19899342	     59.60 ns/op	       0 B/op	       0 allocs/op
BenchmarkNextNoSequence-10          	    1219	    998952 ns/op	       0 B/op	       0 allocs/op
BenchmarkNextSleep-10               	 4119868	     375.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkNextSleepMaxSequence-10    	17207970	     58.34 ns/op	       0 B/op	       0 allocs/op
BenchmarkNextSleepNoSequence-10     	    1066	   1160042 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	trellis.tech/trellis/common.v1/snowflake	8.529s

Documentation

Index

Constants

View Source
const (
	DefEpoch        int64 = 1504426600000 // 2017-09-03 16:16:40.000
	DefTimeAccuracy int64 = 1000000       // Millisecond 1000000
	DefMaxBits      uint8 = 63            // the max length of number (math:pow(2, 63)). without check generated id.
	DefNodeBits     uint8 = 10            // 1024 workers
	DefSequenceBits uint8 = 12            // 4096 numbers of per time accuracy
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

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

Config 配置

type ID

type ID int64

func ParseBase2

func ParseBase2(id string) (ID, error)

ParseBase2 converts a Base2 string into a snowflake ID

func ParseBase64

func ParseBase64(id string) (ID, error)

ParseBase64 converts a base64 string into a snowflake ID

func ParseBytes

func ParseBytes(id []byte) (ID, error)

ParseBytes converts a byte slice into a snowflake ID

func ParseString

func ParseString(id string) (ID, error)

ParseString converts a string into a snowflake ID

func (ID) Base2

func (p ID) Base2() string

Base2 returns a string base2 of the snowflake ID

func (ID) Base64

func (p ID) Base64() string

Base64 returns a base64 string of the snowflake ID

func (ID) Bytes

func (p ID) Bytes() []byte

Bytes returns a byte slice of the snowflake ID

func (ID) String

func (p ID) String() string

String returns a string of the snowflake ID

type Option

type Option func(*Config)

func Epoch

func Epoch(epoch int64) Option

func MaxBits

func MaxBits(maxBits uint8) Option

func NodeID

func NodeID(id int64) Option

func NodesBits

func NodesBits(nodesBits uint8) Option

func SequenceBits

func SequenceBits(sequenceBits uint8) Option

type Worker

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

Worker 工作对象

func NewWorker

func NewWorker(opts ...Option) (*Worker, error)

NewWorker 生产工作对象

func (*Worker) GetEpochTime

func (p *Worker) GetEpochTime() int64

GetEpochTime 获取开始时间

func (*Worker) Next

func (p *Worker) Next() ID

Next 获取下一个ID值 统一时刻只能被调用一次

func (*Worker) NextSleep

func (p *Worker) NextSleep() ID

NextSleep 获取下一个ID值 时间戳一样,则沉睡1个单位时间 统一时刻只能被调用一次

Jump to

Keyboard shortcuts

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