Documentation
¶
Overview ¶
Package postgres provides a snowflake.NodeRegistry that leases node IDs from a PostgreSQL table.
The registry creates its table on first use and fills it with one row per node ID. Each process leases the lowest free row and renews the lease in the background; a crashed process's node ID becomes free again once its lease expires. All times are taken from the database clock, so clock differences between hosts don't matter.
Requires PostgreSQL 9.5 or later (FOR UPDATE SKIP LOCKED). The caller opens the *sql.DB, for example with github.com/jackc/pgx/v5/stdlib.
Index ¶
Constants ¶
const DefaultTable = "snowflake_nodes"
DefaultTable is the table name used when no WithTable option is given.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*config)
Option configures a Registry.
func WithOwner ¶
WithOwner sets the identifier recorded as the lease holder (at most 255 characters). Defaults to the hostname plus a random suffix.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry leases node IDs from a PostgreSQL table. It is safe for concurrent use.
func (*Registry) Acquire ¶
Acquire leases the lowest free node ID and starts renewing it in the background. It returns snowflake.ErrNoNodeIDAvailable if all node IDs are leased.