crsync

package
v0.0.0-...-44ef894 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const UsingCockroachGo = false

UsingCockroachGo is true if the CockroachDB go runtime is in use.

Variables

This section is empty.

Functions

func CPUBiasedInt

func CPUBiasedInt() int

CPUBiasedInt returns an arbitrary non-negative integer that has a best-effort association with the current CPU.

Specifically, in the common case, the same value is returned on the same CPU; and different CPUs return different values.

When the CockroachDB go runtime is used, the returned value is simply the index of the current P (between 0 and GOMAXPROCS-1).

func NumShards

func NumShards() int

NumShards returns the recommended number of shards when CPUBiasedInt is used to select a shard.

Types

type Counter

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

Counter is a single logical counter backed by a sharded implementation (Counters) under the hood.

Properties:

  • Thread-safe increments: Add() can be called concurrently from many goroutines.
  • Low write contention: Writes are sharded to minimize cache-line ping‑pong.
  • Simple reads: Get() aggregates across shards to return the current value.
  • Construction: Use MakeCounter(). The zero value is NOT ready to use.
  • Performance: Add is O(1) with low contention; Get is O(NumShards()).
  • Consistency: Reads are best-effort snapshots without global locking. Each shard is read atomically, but the aggregation is not linearizable with respect to concurrent Add calls. This is typically acceptable for metrics and counters.

Example:

c := MakeCounter()
c.Add(1)
c.Add(41)
fmt.Println(c.Get()) // 42

func MakeCounter

func MakeCounter() Counter

MakeCounter initializes a new Counter.

func (*Counter) Add

func (c *Counter) Add(delta int64)

Add atomically adds delta to the counter. It is safe for concurrent use by multiple goroutines; delta may be negative (decrement).

Add is very efficient: a single atomic increment on a mostly uncontended cache line.

func (*Counter) Get

func (c *Counter) Get() int64

Get the current value of the counter.

It safe to call Get() while there are concurrent Add() calls (but there is no guarantee wrt which of those are reflected).

Get is O(NumShards()) so it is more expensive than Add().

type Counters

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

Counters is a sharded set of logical counters that can be incremented concurrently with low contention.

Use when you need N independent counters that are updated from many goroutines (e.g., metrics like hits/misses/errors, per-state tallies).

Properties:

  • Thread-safe increments: Add() can be called concurrently from many goroutines.
  • Low write contention: Writes are sharded to minimize cache-line ping‑pong.
  • Simple reads: Get() aggregates across shards to return the current value.
  • Construction: Use MakeCounter(). The zero value is NOT ready to use.
  • Performance: Add is O(1) with low contention; Get is O(NumShards());
  • Consistency: Reads are best-effort snapshots without global locking. Each shard is read atomically, but the aggregation is not linearizable with respect to concurrent Add calls. This is typically acceptable for metrics and counters.

func MakeCounters

func MakeCounters(numCounters int) Counters

MakeCounters creates a new Counters with the specified number of counters.

func (*Counters) Add

func (c *Counters) Add(counter int, delta int64)

Add atomically adds delta to the specified counter. It is safe for concurrent use by multiple goroutines; delta may be negative (decrement).

Add is very efficient: a single atomic increment on a mostly uncontended cache line.

func (*Counters) All

func (c *Counters) All() iter.Seq[int64]

All iterates through the current values of all counters (in order).

Complexity is O(NumShards() * numCounters). All is safe for concurrent use, but there are no ordering guarantees w.r.t. concurrent updates.

All is designed to minimize disruption to concurrent Add() calls and is preferable to multiple Get() calls when all counter values are needed.

func (*Counters) Get

func (c *Counters) Get(counter int) int64

Get the current value of the specified counter.

It safe to call Get() while there are concurrent Add() calls (but there is no guarantee wrt which of those are reflected).

Get is O(NumShards()) so it is more expensive than Add().

type TypedAtomicInt64

type TypedAtomicInt64[T ~int64] struct {
	// contains filtered or unexported fields
}

TypedAtomicInt64 is a thin wrapper around atomic.Int64 that provides type safety.

func (*TypedAtomicInt64[T]) Add

func (x *TypedAtomicInt64[T]) Add(delta T) (new T)

Add atomically adds delta to x and returns the new value.

func (*TypedAtomicInt64[T]) CompareAndSwap

func (x *TypedAtomicInt64[T]) CompareAndSwap(old, new T) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for x.

func (*TypedAtomicInt64[T]) Load

func (x *TypedAtomicInt64[T]) Load() T

Load atomically loads and returns the value stored in x.

func (*TypedAtomicInt64[T]) Store

func (x *TypedAtomicInt64[T]) Store(val T)

Store atomically stores val into x.

func (*TypedAtomicInt64[T]) Swap

func (x *TypedAtomicInt64[T]) Swap(new T) (old T)

Swap atomically stores new into x and returns the previous value.

Jump to

Keyboard shortcuts

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