Documentation
¶
Overview ¶
Package snowflake generates unique, roughly time-ordered 64-bit IDs.
An ID is a positive int64 laid out as:
0 | 41 bits: milliseconds since epoch | 10 bits: node ID | 12 bits: sequence
which gives about 69 years of IDs per epoch, up to 1024 nodes, and up to 4096 IDs per millisecond per node.
Index ¶
Constants ¶
const (
// MaxNodeID is the largest node ID a Node accepts.
MaxNodeID = 1<<nodeBits - 1
)
Variables ¶
var ( // ErrInvalidNodeID is returned when a node ID is outside [0, MaxNodeID]. ErrInvalidNodeID = errors.New("snowflake: invalid node ID") // ErrInvalidEpoch is returned when the epoch is in the future. ErrInvalidEpoch = errors.New("snowflake: epoch is in the future") // ErrTimeOverflow is returned once the 41-bit time range since the epoch is used up. ErrTimeOverflow = errors.New("snowflake: time since epoch exceeds 41 bits") // ErrLeaseLost is returned when the node ID lease from a NodeRegistry is lost, // because another node may now be using the same node ID. ErrLeaseLost = errors.New("snowflake: node ID lease lost") )
DefaultEpoch is the epoch used when no WithEpoch option is given.
var ErrNoNodeIDAvailable = errors.New("snowflake: no node ID available")
ErrNoNodeIDAvailable is returned by a NodeRegistry when all node IDs are in use.
Functions ¶
This section is empty.
Types ¶
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node generates IDs for a single node ID. It is safe for concurrent use.
func New ¶
New creates a Node with the given node ID. Two nodes running at the same time must never share a node ID; use NewWithRegistry to have one assigned automatically.
func NewWithRegistry ¶
NewWithRegistry acquires a node ID from registry and creates a Node with it. Once the registry reports the node ID as lost, Generate returns ErrLeaseLost.
Call registry.Release when the Node is no longer needed.
func (*Node) Generate ¶
Generate returns a new unique ID. If 4096 IDs were already generated in the current millisecond, it waits for the next one.
type NodeRegistry ¶
type NodeRegistry interface {
// Acquire reserves a free node ID in [0, MaxNodeID] for this process.
Acquire(ctx context.Context) (int64, error)
// Release gives the node ID back so another process can use it.
Release(ctx context.Context) error
// Lost returns a channel that is closed if the node ID can no longer be
// guaranteed to be ours. It may return nil if that can never happen.
Lost() <-chan struct{}
}
NodeRegistry assigns node IDs so that no two running nodes share one.
The mysql and mongodb subpackages provide registries that lease node IDs from a database table or collection.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
lease
Package lease keeps a node ID lease alive by renewing it in the background.
|
Package lease keeps a node ID lease alive by renewing it in the background. |
|
Package mongodb provides a snowflake.NodeRegistry that leases node IDs from a MongoDB collection.
|
Package mongodb provides a snowflake.NodeRegistry that leases node IDs from a MongoDB collection. |
|
Package mysql provides a snowflake.NodeRegistry that leases node IDs from a MySQL table.
|
Package mysql provides a snowflake.NodeRegistry that leases node IDs from a MySQL table. |
|
Package postgres provides a snowflake.NodeRegistry that leases node IDs from a PostgreSQL table.
|
Package postgres provides a snowflake.NodeRegistry that leases node IDs from a PostgreSQL table. |