Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidConfig is returned when New is called with a config that fails validation. ErrInvalidConfig = errors.New("invalid config") // ErrInvalidBitFormat is returned when format.timestampBits + format.sequenceBits != 63. ErrInvalidBitFormat = errors.New("bit format must sum to 63") // ErrEpochInFuture is returned when cfg.epoch is the time // after the current system time at the time of Node creation. ErrEpochInFuture = errors.New("configured epoch is in the future") // ErrInvalidBlockSize is returned when cfg.blockSize is not a positive integer // or exceeds the maximum sequence number for the given format (2^format.sequenceBits). ErrInvalidBlockSize = errors.New("block size must be positive and <= max sequence number for the given format") // ErrInvalidThreshold is returned when cfg.threshold exceeds cfg.blockSize. ErrInvalidThreshold = errors.New("threshold must not exceed block size") // ErrNilRegistry is returned when New is called with a nil Registry. ErrNilRegistry = errors.New("registry cannot be nil") // ErrClockBeforeEpoch is returned when the system clock is behind the configured epoch. ErrClockBeforeEpoch = errors.New("system clock is before the configured epoch") // ErrTimestampOverflow is returned when the current time // exceeds the maximum representable timestamp for the given format. ErrTimestampOverflow = errors.New("timestamp exceeds maximum for the given format") // ErrSequenceOverflow is returned when too many IDs are generated in the same second // and the sequence number exceeds the maximum for the given format. ErrSequenceOverflow = errors.New("too many IDs generated in the same second") // ErrInvalidSequence is returned when the sequence number acquired from the registry // is out of range for the given format.sequenceBits. ErrInvalidSequence = errors.New("invalid sequence number for given format") )
Functions ¶
This section is empty.
Types ¶
type FormatOption ¶
type FormatOption func(*format)
FormatOption configures the bit layout of generated IDs.
func WithSequenceBits ¶
func WithSequenceBits(bits uint8) FormatOption
WithSequenceBits sets the number of bits used for the sequence component.
func WithTimestampBits ¶
func WithTimestampBits(bits uint8) FormatOption
WithTimestampBits sets the number of bits used for the timestamp component.
type ID ¶
type ID int64
ID is a 63-bit Snowflake-style distributed identifier.
func (ID) MarshalJSON ¶
MarshalJSON encodes the ID as a quoted decimal string to avoid precision loss in JavaScript, which cannot represent 63-bit integers exactly.
func (*ID) UnmarshalJSON ¶
UnmarshalJSON decodes the ID from a quoted decimal string.
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node generates unique IDs from sequence blocks reserved in a Registry. The embedded timestamp reflects block-allocation time, not generation time, so IDs are not strictly time-ordered. It is safe for concurrent use by multiple goroutines.
type Option ¶
type Option func(*config)
Option configures a Node or Parser at creation time.
func WithBlockSize ¶
WithBlockSize sets the number of sequence numbers a Node reserves from the registry per allocation. It must be positive. The default is 10,000.
func WithEpoch ¶
WithEpoch sets the reference time from which timestamps are measured. The epoch must not be in the future. The default is 2026-01-01 00:00:00 UTC.
func WithFormat ¶
func WithFormat(opts ...FormatOption) Option
WithFormat sets the bit layout of generated IDs using the given format options.
func WithThreshold ¶
WithThreshold sets how many sequence numbers must remain in the current block before the Node asynchronously pre-allocates the next one. It must not exceed the block size. A value below 1 disables pre-allocation. The default is 5,000.