Documentation
¶
Overview ¶
Package monotime provides fast, lock-free, and strictly monotonic time and UUID v7 generators.
These generators are guaranteed to never go backward, even in the event of system clock adjustments or NTP jumps, ensuring strict monotonicity and uniqueness.
Index ¶
- type Gen
- type GenUUID
- type UUID
- func (u UUID) MarshalBinary() ([]byte, error)
- func (u UUID) MarshalText() ([]byte, error)
- func (u UUID) NodeID() int
- func (u UUID) Parse() (time.Time, int, bool)
- func (u *UUID) Scan(src any) error
- func (u UUID) String() string
- func (u UUID) Time() time.Time
- func (u *UUID) UnmarshalBinary(data []byte) error
- func (u *UUID) UnmarshalText(text []byte) error
- func (u UUID) Valid() bool
- func (u UUID) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Gen ¶ added in v0.1.1
type Gen struct {
// contains filtered or unexported fields
}
Gen provides a simple, lock-free, and strictly monotonic time source that generates nanosecond timestamps.
func NewGen ¶ added in v0.1.1
NewGen creates a new monotonic timestamp generator. The generator guarantees strictly monotonic, ever-increasing values, even if the system clock moves backwards or stops. If lastKnown is greater than the current system time, the generator continues from it.
func (*Gen) Next ¶ added in v0.1.1
Next returns the next strictly monotonic nanosecond timestamp. The returned value will always be greater than any previously returned value from this generator. Next never blocks, even if the system clock moves backwards or stops; it atomically increments the last timestamp by 1. Next is lock-free and safe to use from multiple goroutines.
type GenUUID ¶ added in v0.1.1
type GenUUID struct {
// contains filtered or unexported fields
}
GenUUID generates strictly monotonic, K-sortable UUID v7 identifiers.
The generator is lock-free and guarantees that each generated UUID is strictly greater than the previous one generated by the same node ID. It is resilient to system clock rollbacks or standstills (e.g., NTP adjustments), in which case it will increment the nanosecond counter logically to ensure monotonicity and uniqueness.
The generated UUIDs contain a custom magic prefix and zeroed rand_a field to distinguish them from standard UUID v7 implementations.
func NewGenUUID ¶ added in v0.1.1
NewGenUUID creates a new monotime UUID generator bound to the given node ID.
If lastKnown is ZeroUUID, the generator starts from the current system time. Otherwise, lastKnown must be a valid monotime UUID previously produced by the same node; its timestamp is used to ensure monotonicity.
Returns an error if:
- nodeID is zero or outside the 24-bit range,
- lastKnown is not a valid monotime UUID,
- lastKnown was generated by a different node.
type UUID ¶
type UUID [16]byte
UUID represents a 16-byte UUID v7 value following RFC 9562 and supporting the custom "monotime" extension format.
A monotime UUID embeds:
- a 48-bit timestamp in milliseconds,
- a 12-bit zeroed rand_a field (used as signature),
- a 62-bit custom rand_b field containing a fixed prefix, node ID (24 bits) and a 20-bit nanosecond remainder.
This layout allows round-tripping back to the original time with full nanosecond precision and provides detection of monotime-encoded UUIDs.
var ZeroUUID UUID
ZeroUUID represents an empty (zero) monotime UUID value which indicates that no prior UUID is known when initializing a generator.
func MinUUID ¶ added in v0.3.2
MinUUID returns a bare UUID containing only the millisecond portion. Primary use is to provide ranges for database scans.
func (UUID) MarshalBinary ¶
MarshalBinary implements encoding.BinaryMarshaler and returns a copy of the raw 16-byte UUID value.
func (UUID) MarshalText ¶
MarshalText implements encoding.TextMarshaler. It returns the canonical 36-byte string representation of the UUID.
func (UUID) NodeID ¶
NodeID returns the node ID embedded in the UUID. It returns 0 if the UUID is not a valid monotime UUID.
func (UUID) Parse ¶
Parse extracts time, node ID, and a flag indicating whether the UUID conforms to the monotime UUID format.
If the UUID is not a valid monotime UUID (incorrect version, variant, signature bits, or prefix), flag will be false. Otherwise, it returns the full timestamp reconstructed with nanosecond precision and the encoded node ID.
func (*UUID) Scan ¶
Scan implements sql.Scanner and loads a UUID from either binary or text SQL column types. It accepts:
- string (text form),
- []byte (binary form or text form),
and returns an error for unsupported types.
func (UUID) String ¶
String implements fmt.Stringer and returns the canonical string representation of the UUID (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
func (UUID) Time ¶
Time returns the timestamp embedded in the UUID. If the UUID is not a valid monotime UUID, it returns zero time. This method preserves full nanosecond precision reconstructed from the custom rand_b field.
func (*UUID) UnmarshalBinary ¶
UnmarshalBinary implements encoding.BinaryUnmarshaler and loads the UUID from a 16-byte binary slice.
func (*UUID) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler and parses a UUID from its canonical 36-character string form.