cuid2

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: MIT Imports: 11 Imported by: 0

README

CUID2

Go implementation of CUID2 - Collision-resistant ids optimized for horizontal scaling and performance.

Installation

go get github.com/PatrickChoDev/cuid2

Usage

import "github.com/PatrickChoDev/cuid2"

// Generate a CUID with default settings
id := cuid2.Generate() // Example: "tz4a98xxat96"

// Initialize with custom options
generator, err := cuid2.Init(
    cuid2.WithLength(10),                 // Custom length (2-32)
    cuid2.WithRandomFunc(rand.Float64),   // Custom random function
    cuid2.WithFingerprint("myapp"),       // Custom fingerprint
)
if err != nil {
    // Handle error
}
id = generator() // Generate using custom settings

// Validate a CUID
valid := cuid2.IsCuid("tz4a98xxat96") // Returns true/false

Features

  • Thread-safe ID generation
  • Configurable length (2-32 characters)
  • Customizable random function
  • System fingerprinting for distributed systems
  • Validation function
  • Zero external dependencies (except crypto/sha3)

License

MIT License

Documentation

Index

Constants

View Source
const (
	DefaultIdLength int = 24
	MinIdLength     int = 2
	MaxIdLength     int = 32

	// ~22k hosts before 50% chance of initial counter collision
	MaxSessionCount int64 = 476782367
)

Variables

View Source
var Generate, _ = Init()

Generates Cuids using default config options

Functions

func Init

func Init(options ...Option) (func() string, error)

Initializes the Cuid generator with default or user-defined config options

Returns a function that can be called to generate Cuids using the initialized config

func IsCuid

func IsCuid(cuid string) bool

Checks whether a given Cuid has a valid form and length

Types

type Config

type Config struct {
	// A custom function that can generate a floating-point value between 0 and 1
	RandomFunc func() float64

	// A counter that will be used to affect the entropy of successive id
	// generation calls
	SessionCounter Counter

	// Length of the generated Cuid, min = 2, max = 32
	Length int

	// A unique string that will be used by the Cuid generator to help prevent
	// collisions when generating Cuids in a distributed system.
	Fingerprint string
}

type Counter

type Counter interface {
	Increment() int64
}

type Option

type Option func(*Config) error

func WithFingerprint

func WithFingerprint(fingerprint string) Option

A unique string that will be used by the id generator to help prevent collisions when generating Cuids in a distributed system.

func WithLength

func WithLength(length int) Option

Configures the length of the generated Cuid

Min Length = 2, Max Length = 32

func WithRandomFunc

func WithRandomFunc(randomFunc func() float64) Option

A custom function that will generate a random floating-point value between 0 and 1

func WithSessionCounter

func WithSessionCounter(sessionCounter Counter) Option

A custom counter that will be used to affect the entropy of successive id generation calls

type SessionCounter

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

func NewSessionCounter

func NewSessionCounter(initialCount int64) *SessionCounter

func (*SessionCounter) Increment

func (sc *SessionCounter) Increment() int64

Jump to

Keyboard shortcuts

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