counter

package
v0.2.0-dev.1 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

README

Counter

Vendor-agnostic interface for atomic sequential number generation.

Interface

Counter

Generates unique, sequential values scoped to a domain string.

type Counter interface {
    Next(ctx context.Context, domain string) (int64, error)
}
  • domain: A string key that scopes the counter (max 255 characters). Each domain maintains its own independent sequence.
  • Next: Atomically increments and returns the next value. The first call for a new domain returns 1. Safe for concurrent use; values are unique but ordering is not guaranteed.

Usage

cnt := mysqlcounter.NewCounter(db)

// Generate sequential IDs for different domains
val, err := cnt.Next(ctx, "request/my-queue") // returns 1
val, err = cnt.Next(ctx, "request/my-queue")  // returns 2
val, err = cnt.Next(ctx, "request/other")     // returns 1

Implementing a Backend

  1. Create platform/extension/counter/{backend}/ directory
  2. Implement the Counter interface
  3. Add a schema file under platform/extension/counter/{backend}/schema/ if the backend requires it

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Counter

type Counter interface {
	// Next atomically increments the counter for the given domain and returns the new value.
	// The first call for a new domain returns 1.
	// The implementation should support at least 255 length domains.
	// The function is safe to be called concurrently and will give unique results, but the order of the values is not guaranteed.
	Next(ctx context.Context, domain string) (int64, error)
}

Counter provides atomic sequential number generation for a given domain. Each call to Next returns the next value in the sequence for the specified domain. The value is guaranteed to be unique within the domain throughout the system and persisted accordingly.

Directories

Path Synopsis
Package mock is a generated GoMock package.
Package mock is a generated GoMock package.

Jump to

Keyboard shortcuts

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