Documentation
¶
Index ¶
Constants ¶
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).
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
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 ¶
MakeCounters creates a new Counters with the specified number of counters.
func (*Counters) Add ¶
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 ¶
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.
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.