snowflake

package
v0.0.0-...-261f1bd Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2018 License: GPL-3.0, Apache-2.0 Imports: 3 Imported by: 0

README

Snowflake

Yet another Golang implementation of Twitter's Snowflake. This implementation is used by https://bitbucket.org/mudphilo/tinode/chat and as such it's up to date and supported.

Performance

Maximum theoretical performance is limited by the wait time on the sequence number. I.e. minimum time for a value to be generated is 1 ms / 4096 ~ 244 ns. Actual performance on average hardware is 246 ns.

Spec

ID is a 64 bit unsigned integer composed of:

  • the top bit is always zero for compatibility; for instance, Go's sql implementation requires top bit of uint64 to be 0
  • time - 41 bits (millisecond precision with a custom epoch, enough to cover until the year 2083)
  • configured machine id - 10 bits - gives us up to 1024 machines
  • sequence number - 12 bits - rolls over every 4096 per machine (with protection to avoid rollover in the same ms, and as such it may block for some hundreds of microseconds)

Differences from Twitter's Snowflake:

  • uint64 instead of int64
  • zero on error instead of -1
  • different epoc: 2014 instead of 2010

License

Apache License 2.0

Documentation

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 NewSnowFlake

func NewSnowFlake(workerId uint32) (*SnowFlake, error)

Initialize the generator.

func (*SnowFlake) Next

func (sf *SnowFlake) Next() (uint64, error)

Generate the next unique ID.

Jump to

Keyboard shortcuts

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