uid

package
v0.54.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package uid provides functions for generating short, time-ordered, URL-safe unique identifiers and normalizing/validating ID strings.

Architecture & Mechanism

The primary generator, GenerateShortID, packs a microsecond Unix timestamp and a 4-bit sequence counter into a single 64-bit integer:

composite = (ts << 4) | counter

The composite value is then encoded into Crockford Base32 (using the 32-character alphabet "0123456789abcdefghjkmnpqrstvwxyz" which excludes ambiguous characters like I, L, O, and U) and lowercased to yield an 11-character string (e.g. "sa4rc789wxg"). State updates (timestamp tracking and counter incrementing) are guarded by a package-level mutex.

Comparison: support/uid vs Go 1.27's Standard Library uuid (UUID v7)

Go 1.27 introduces a standard library uuid package generating 128-bit UUID v7 identifiers (combining a 48-bit millisecond timestamp with ~62 bits of CSPRNG randomness, formatted as standard 36-character hyphenated strings).

While UUID v7 is the right choice when generating IDs across independent, uncoordinated machines or microservices, support/uid's design offers significant advantages across three primary pillars:

1. Zero Randomness / No System Entropy Overhead:

  • UUID v7 requires cryptographically secure pseudorandom number generation (crypto/rand) to fill ~62 bits on every generation call.
  • support/uid is fully deterministic based on timestamp and sequence counter, eliminating CSPRNG calls and system entropy consumption under high-volume ID generation.

2. Sub-Millisecond Precision & Sub-Tick Ordering:

  • UUID v7 encodes time down to millisecond resolution; IDs generated within the same millisecond are sorted by random bits rather than exact creation sequence.
  • support/uid uses microsecond timestamps paired with an explicit counter (0–15) that increments on ties, guaranteeing strict creation-order monotonicity for up to 16 IDs per microsecond.

3. Human-Optimized Ergonomics & URL/Filename Safety:

  • Compact Length: An 11-character ID is dramatically easier to read over the phone, retype from a support ticket, or eyeball in log files compared to a 36-character UUID string.
  • No Visual Ambiguity: Crockford Base32 removes ambiguous characters (I, L, O, U), eliminating 1/I/l and 0/O confusion that causes human transcription errors.
  • Case-Insensitive & Normalization-Friendly: Designed to be case-insensitive. The NormalizeID helper ensures that habituated capitalization or uppercase pipeline steps do not break database lookups.
  • Hyphen-Free: Free of hyphens, eliminating copy-paste failure modes caused by misplaced or missing dashes in standard UUIDs (e.g., xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

4. Minimal Dependency Footprint:

  • The package depends exclusively on standard library primitives (strings, sync, time), making it lightweight, fast, and trivial to audit end-to-end.

Comparison: support/uid vs xid (github.com/rs/xid)

xid is a popular 12-byte identifier composed of a 4-byte timestamp (seconds), a 3-byte machine identifier, a 2-byte process ID, and a 3-byte counter, encoded as a 20-character base32hex string.

Where support/uid Wins:

  • Compactness: 11 characters vs xid's 20 characters. support/uid is significantly shorter.
  • Simplicity: ~80 lines of pure standard library Go code with zero platform-specific files (unlike xid which uses OS-specific hostid implementations like hostid_linux.go, hostid_darwin.go).

Where xid Wins:

  • Uncoordinated Multi-Host Safety: xid bakes the machine ID and process ID directly into every ID via hostid resolution and os.Getpid(), allowing multiple replicas or pods to generate IDs safely out-of-the-box without central coordination.
  • Lock-Free Concurrency: xid uses atomic counter operations instead of a sync.Mutex, scaling better across multiple CPU cores under extremely heavy concurrent generation.
  • Guaranteed Per-Host Throughput: xid guarantees uniqueness for up to 16,777,216 IDs per second per host/process via its 24-bit counter.

Summary & Trade-offs (Cons):

1. Single-Process / Single-Instance Scope:

  • Uniqueness relies on in-memory state (lastTimestamp and counter) guarded by a mutex.
  • Unlike UUID v7 (entropy) or xid (host ID + PID), support/uid requires external coordination if generated across multiple independent processes or pods.

2. Burst Throttling under Extreme Concurrency:

  • If more than 16 IDs are requested within a single microsecond, the generator pauses execution via time.Sleep(1 * time.Millisecond) to advance the clock tick and prevent counter overflow/duplicates.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateShortID

func GenerateShortID() string

GenerateShortID creates a new 11-character lowercase short ID. It encodes the current microsecond timestamp in Crockford Base32. Thread-safe via mutex to prevent duplicate IDs under concurrency.

Business Logic:

  • Uses time.Now().UnixMicro() as the uniqueness base.
  • Maintains package-level lastTimestamp and counter across calls.
  • Counter increments when the timestamp equals the previous call; resets to 0 when the timestamp changes.
  • The counter is 4 bits (0-15), giving 16 unique IDs per timestamp tick.
  • On Windows the system timer resolution is ~1 ms, so UnixMicro returns the same value for every call within the same millisecond.
  • Once the counter exceeds 15 it would overflow and wrap back to 0, producing the exact same composite value and therefore a duplicate ID.
  • Sleeping 1 ms guarantees the timestamp advances to the next tick.
  • Packs timestamp and counter into a single int64: (ts << 4) | counter.
  • The timestamp occupies the high bits; the counter occupies the low 4 bits.
  • Encodes the composite value using Crockford Base32 alphabet.
  • Converts the result to lowercase.
  • Returns an 11-character string (e.g. "sa4rc789wxg").

func IsShortID

func IsShortID(id string) bool

IsShortID checks whether the given string matches short ID lengths (11 or 21 chars).

func NormalizeID

func NormalizeID(id string) string

NormalizeID normalizes an ID to lowercase for consistent lookups.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL