snowflake

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2023 License: MIT Imports: 2 Imported by: 1

Documentation

Index

Constants

View Source
const (
	BitLengthTimestamp   = 41
	BitLengthPartitionID = 10
	BitLengthSequence    = 63 - BitLengthTimestamp - BitLengthPartitionID

	TimestampBitShift   = BitLengthPartitionID + BitLengthSequence
	PartitionIDBitShift = BitLengthSequence

	PartitionIDBitMask = 1<<BitLengthPartitionID - 1
	SequenceBitMask    = 1<<BitLengthSequence - 1
)

+-------------------------------------------------------------------------------+ | 1 Bit Unused | 41 Bit Timestamp | 10 Bit PartitionID | 12 Bit Sequence ID | +-------------------------------------------------------------------------------+

Variables

View Source
var Default = New()

Functions

func NextID

func NextID() (uint64, error)

func PartitionID

func PartitionID(id uint64) uint64

func SetPartitionID

func SetPartitionID(partitionID int64)

Types

type DefaultSequence

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

func (*DefaultSequence) Next

func (s *DefaultSequence) Next(_ int16, epoch time.Time) (int64, int64, error)

type Option

type Option func(*Snowflake)

func WithCustomSequence

func WithCustomSequence(sequence Sequence) Option

func WithEpoch

func WithEpoch(epoch time.Time) Option

func WithPartitionID

func WithPartitionID(partition int64) Option

type Sequence

type Sequence interface {
	Next(partition int16, epoch time.Time) (int64, int64, error)
}

type Snowflake

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

func New

func New(opts ...Option) *Snowflake

func (*Snowflake) CustomNext

func (sf *Snowflake) CustomNext(partition int16, sequence Sequence) (uint64, error)

func (*Snowflake) Next

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

func (*Snowflake) Timestamp

func (sf *Snowflake) Timestamp(id uint64) time.Time

Jump to

Keyboard shortcuts

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