idgen

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package idgen provides distributed ID generation without requiring central coordination.

IDs are 64-bit integers with the following structure:

  • 41 bits: Timestamp in milliseconds since epoch
  • 10 bits: Region ID (supports 1024 regions)
  • 12 bits: Sequence number (supports 4096 IDs per millisecond per region)

This design ensures globally unique IDs across multiple regions while maintaining the benefits of integer primary keys (smaller storage, faster indexes, compatibility with existing protocols).

Example usage:

generator := idgen.NewGenerator(1) // Region ID = 1
characterID := generator.NextID()   // 7123456789012345678

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseID

func ParseID(id int64) (timestamp, regionID, sequence int64)

ParseID extracts the timestamp, region ID, and sequence from a generated ID.

Types

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator generates globally unique 64-bit IDs for a specific region. It is safe for concurrent use.

func NewGenerator

func NewGenerator(regionID int64) *Generator

NewGenerator creates a new ID generator for the specified region. Region ID must be between 0 and 1023 (inclusive).

func (*Generator) NextID

func (g *Generator) NextID() int64

NextID generates the next unique ID. It panics if the system clock moves backward, as this could cause duplicate IDs.

func (*Generator) RegionID

func (g *Generator) RegionID() int64

RegionID returns the region ID this generator was created with.

Jump to

Keyboard shortcuts

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