snowflake

package
v0.0.0-...-7b97725 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2020 License: MIT Imports: 8 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MemcachedServer

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

MemcachedServer implements memcached protocol. You can use get,gets to get IDs.

func NewMemcachedServer

func NewMemcachedServer(ln net.Listener, snowFlake *SnowFlake) *MemcachedServer

NewMemcachedServer returns a new MemcachedServer.

func (*MemcachedServer) Serve

func (ms *MemcachedServer) Serve()

Serve serves memcached server.

type Node

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

A Node struct holds the basic information needed for a snowflake generator node

func NewNode

func NewNode(node int64, epoch int64, nodeBits, stepBits uint8) (*Node, error)

NewNode returns a new snowflake node that can be used to generate snowflake IDs

func (*Node) Generate

func (n *Node) Generate() int64

Generate creates and returns a unique snowflake ID To help guarantee uniqueness - Make sure your system is keeping accurate system time - Make sure you never have multiple nodes running with the same node ID

func (*Node) GenerateBatch

func (n *Node) GenerateBatch(c uint16) []int64

GenerateBatch creates and returns a batch of unique snowflake ID To help guarantee uniqueness - Make sure your system is keeping accurate system time - Make sure you never have multiple nodes running with the same node ID

func (*Node) ParseServerID

func (n *Node) ParseServerID(id int64) int64

ParseServerID returns an int64 of the snowflake ID node number.

func (*Node) ParseStep

func (n *Node) ParseStep(id int64) int64

ParseStep returns an int64 of the snowflake step (or sequence) number.

func (*Node) ParseTime

func (n *Node) ParseTime(id int64) int64

ParseTime returns an int64 unix timestamp in milliseconds of the snowflake ID time.

type SnowFlake

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

SnowFlake twitter snowflake backend. It is based on https://github.com/bwmarrin/snowflake and - remove some unused methods - add GenerateBatch methods

**How it Works**. Each time you generate an ID, it works, like this.

A timestamp with millisecond precision is stored using 41 bits of the ID. Then the NodeID is added in subsequent bits. Then the Sequence Number is added, starting at 0 and incrementing for each ID generated in the same millisecond. If you generate enough IDs in the same millisecond that the sequence would roll over or overfill then the generate function will pause until the next millisecond.

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

func NewSnowFlake

func NewSnowFlake(serverID int64, epoch int64, nodeBits, stepBits uint8) *SnowFlake

NewSnowFlake created a new SnowFlake with given configs.

func (*SnowFlake) Get

func (sf *SnowFlake) Get(ctx context.Context, dummy int8, id *int64) error

Get gets a unique ID. Ignores the request (dummy).

func (*SnowFlake) GetBatch

func (sf *SnowFlake) GetBatch(ctx context.Context, count uint16, ids *[]int64) error

GetBatch gets a batch of ids.

func (*SnowFlake) GetServerID

func (sf *SnowFlake) GetServerID(ctx context.Context, dummy string, serverID *string) error

GetServerID returns server id. Ignores the request (dummy).

Jump to

Keyboard shortcuts

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