snoflinga

package module
v0.0.0-...-dd0d1ad Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2016 License: MIT Imports: 5 Imported by: 4

README

snöflinga

GoDocBuild Status

snöflinga: snowflake like IDs

The timestamp is from Unix Epoch.

The sequence number starts at a random number and increases with each snowflake and rolls over. It should not be relied on for anything.

The ID is 8 bytes and no assumptions are made about its contents or layout. The snowflake generator is used for a single ID.

128 bit

128 bit snowflakes have the following layout:

bits    
0-51     Timestamp, in microseconds.
52-63    Sequence number.
64-127   ID

Documentation

Overview

Package snöflinga generates snowflake like 128-bit ids. The first 52 bits is a timestamp representing time since Unix epoch, in microseconds. The next 12 bits is a sequence number which is increased with each snowflake request, for collision avoidance. The start of the sequence is randomly selected. The final 64 bits is the id.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Flake

type Flake [16]byte

Flake is the type for a 128 bit snowflake.

func (*Flake) ID

func (f *Flake) ID() []byte

ID returns the Flake's ID as a []byte. A snowflake's ID is the last 8 bytes of the Flake. No assumptiosn are made about either the contents of those bytes or their layout: that is left up to the user.

func (*Flake) Time

func (f *Flake) Time() int64

Time returns the Flake's timestamp, in microseconds, as an int64.

type Generator

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

Generator creates snowflakes for a given id

func New

func New(id []byte) Generator

New returns an initialized generator. If the passed byte slice's length is greater than 8 bytes, the first 8 bytes will be used for the generator's id. If the passed byte slice's is less than 8 bytes, the id will be left-padded with 0s, zeros. The generator's sequence is initialized with a random number.

func (*Generator) ID

func (g *Generator) ID() []byte

ID returns the ID bytes associated with this generator.

func (*Generator) Snowflake

func (g *Generator) Snowflake() Flake

Snowflake generates an Flake from the current time and next sequence value.

Directories

Path Synopsis
Package sne generates snowflake like 128 bit ids that uses milliseconds for the time element.
Package sne generates snowflake like 128 bit ids that uses milliseconds for the time element.
Package snö generates snowflake like 128 bit ids that uses milliseconds for the time element, a 7 byte ID, and a 20 bit secondary id.
Package snö generates snowflake like 128 bit ids that uses milliseconds for the time element, a 7 byte ID, and a 20 bit secondary id.

Jump to

Keyboard shortcuts

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