Documentation
¶
Overview ¶
Package orbitid implements the Orbit ID v1 unsigned 64-bit format.
Index ¶
- Constants
- func Encode(fields Fields) (uint64, error)
- func FromDecimalString(input string) (uint64, error)
- func FromUnixTimeMs(unixMs int64) int64
- func GetNode(id any) (int, error)
- func GetSequence(id any) (int, error)
- func GetTimestamp(id any) (uint64, error)
- func GetType(id any) (int, error)
- func IsValid(id any) bool
- func ToDecimalString(id uint64) string
- func ToHexString(id uint64) string
- func ToUnixTimeMs(timestamp uint64) uint64
- type Clock
- type DecisionAction
- type Error
- type ErrorCode
- type Fields
- type GenerateDecision
- type Generator
- func (g *Generator) Decide(typ int, nowTimestamp ...int64) GenerateDecision
- func (g *Generator) Generate(typ int) (uint64, error)
- func (g *Generator) GetLastTimestamp() uint64
- func (g *Generator) GetSequence() int
- func (g *Generator) Node() int
- func (g *Generator) RestoreState(lastTimestamp uint64, sequence int) error
- type GeneratorOptions
- type SequenceExhaustedMode
Constants ¶
const ( OrbitEpochUnixMs int64 = 1767225600000 TimestampBits = 41 TypeBits = 6 NodeBits = 7 SequenceBits = 10 TimestampShift = 23 TypeShift = 17 NodeShift = 10 MaxTimestamp uint64 = (1 << TimestampBits) - 1 MaxType int = (1 << TypeBits) - 1 MaxNode int = (1 << NodeBits) - 1 MaxSequence int = (1 << SequenceBits) - 1 DefaultClockRollbackToleranceMs int64 = 5000 )
Variables ¶
This section is empty.
Functions ¶
func FromDecimalString ¶
FromDecimalString accepts only canonical, unsigned base-10 uint64 strings.
func FromUnixTimeMs ¶
FromUnixTimeMs converts Unix milliseconds to an Orbit timestamp. A negative result is invalid for encoding and will be rejected by Generator/Encode.
func GetSequence ¶
func GetTimestamp ¶
GetTimestamp returns milliseconds since Orbit Epoch.
func ToDecimalString ¶
func ToHexString ¶
func ToUnixTimeMs ¶
Types ¶
type Clock ¶
type Clock interface {
CurrentOrbitTimestampMs() int64
}
Clock supplies milliseconds since Orbit Epoch. It is injectable for tests.
func SystemClock ¶
func SystemClock() Clock
SystemClock returns the production wall-clock Orbit timestamp source.
type DecisionAction ¶
type DecisionAction string
const ( DecisionIssue DecisionAction = "issue" DecisionWait DecisionAction = "wait" DecisionWaitNextMs DecisionAction = "wait_next_ms" DecisionError DecisionAction = "error" )
type ErrorCode ¶
type ErrorCode string
ErrorCode is a stable, machine-readable Orbit error code.
const ( InvalidType ErrorCode = "INVALID_TYPE" InvalidNode ErrorCode = "INVALID_NODE" InvalidSequence ErrorCode = "INVALID_SEQUENCE" InvalidTimestamp ErrorCode = "INVALID_TIMESTAMP" InvalidDecimal ErrorCode = "INVALID_DECIMAL" ClockRollback ErrorCode = "CLOCK_ROLLBACK" SequenceExhausted ErrorCode = "SEQUENCE_EXHAUSTED" NodeOwnershipLost ErrorCode = "NODE_OWNERSHIP_LOST" )
type Fields ¶
Fields are the decoded Orbit ID fields. Timestamp is milliseconds since OrbitEpochUnixMs.
type GenerateDecision ¶
type GenerateDecision struct {
Action DecisionAction
Timestamp uint64
Sequence int
WaitUntilTimestamp uint64
FromTimestamp uint64
Error ErrorCode
}
GenerateDecision describes the next safe generator action.
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator issues Orbit IDs for one exclusively assigned node.
func NewGenerator ¶
func NewGenerator(options GeneratorOptions) (*Generator, error)
func (*Generator) Decide ¶
func (g *Generator) Decide(typ int, nowTimestamp ...int64) GenerateDecision
Decide evaluates a generation request without changing state.
func (*Generator) Generate ¶
Generate serializes state changes and returns a newly issued ID. Type 0 is reserved and is rejected.
func (*Generator) GetLastTimestamp ¶
func (*Generator) GetSequence ¶
type GeneratorOptions ¶
type GeneratorOptions struct {
Node int
Clock Clock
ClockRollbackToleranceMs int64
OnSequenceExhausted SequenceExhaustedMode
// ConfirmOwnership may fail closed when a node lease is lost.
ConfirmOwnership func() bool
}
type SequenceExhaustedMode ¶
type SequenceExhaustedMode string
const ( SequenceExhaustedWait SequenceExhaustedMode = "wait" SequenceExhaustedFail SequenceExhaustedMode = "fail" )