snowflake

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2021 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package snowflake provides utilities for operating on Snowflake IDs. https://en.wikipedia.org/wiki/Snowflake_ID

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Generator

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

A Generator generates unique IDs. The zero value can be initialized with Init.

func NewGenerator

func NewGenerator(epoch time.Time, instance uint16) *Generator

NewGenerator returns a new generator that uses the given epoch and instance ID.

func (*Generator) Generate

func (gen *Generator) Generate() ID

Generate returns a unique ID. Generate must not be called concurrently on the same Generator.

func (*Generator) Init

func (gen *Generator) Init(epoch time.Time, instance uint16)

Init sets the generator's epoch and instance ID, and resets the generator's sequence number to zero.

type ID

type ID int64

ID is a Snowflake ID. It is 63 bits: the most significant bit should never be set (i.e. valid Snowflake IDs are non-negative).

func New

func New(timestamp uint64, instance, sequence uint16) ID

New builds a Snowflake ID from the component parts. Only the least significant 41, 10, and 12 bits are used from the timestamp, instance, and sequence arguments, respectively.

func (ID) Instance

func (id ID) Instance() uint16

Instance returns the 10-bit instance ID.

func (ID) Sequence

func (id ID) Sequence() uint16

Sequence returns the 12-bit sequence number.

func (ID) Timestamp

func (id ID) Timestamp() uint64

Timestamp returns the 41-bit timestamp value of the ID. This is conventionally a number of milliseconds since a known epoch value.

Jump to

Keyboard shortcuts

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