Documentation
¶
Overview ¶
Package syncx provides small concurrency primitives used across the core components: a single-flight group with freshness reporting and a spin lock for very short critical sections.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrTimeout = errors.New("syncx: borrow timeout")
ErrTimeout indicates a borrow timed out.
Functions ¶
Types ¶
type AtomicBool ¶
type AtomicBool uint32
AtomicBool is an atomic boolean.
func ForAtomicBool ¶
func ForAtomicBool(val bool) *AtomicBool
ForAtomicBool returns an AtomicBool initialized to val.
func (*AtomicBool) CompareAndSwap ¶
func (b *AtomicBool) CompareAndSwap(old, val bool) bool
CompareAndSwap swaps if the current value equals old.
type AtomicDuration ¶
type AtomicDuration int64
AtomicDuration is an atomic time.Duration.
func ForAtomicDuration ¶
func ForAtomicDuration(val time.Duration) *AtomicDuration
ForAtomicDuration returns an AtomicDuration initialized to val.
func NewAtomicDuration ¶
func NewAtomicDuration() *AtomicDuration
NewAtomicDuration returns a zero AtomicDuration.
func (*AtomicDuration) CompareAndSwap ¶
func (d *AtomicDuration) CompareAndSwap(old, val time.Duration) bool
CompareAndSwap swaps if the current value equals old.
func (*AtomicDuration) Load ¶
func (d *AtomicDuration) Load() time.Duration
Load returns the current value.
type AtomicFloat64 ¶
type AtomicFloat64 uint64
AtomicFloat64 is an atomic float64.
func ForAtomicFloat64 ¶
func ForAtomicFloat64(val float64) *AtomicFloat64
ForAtomicFloat64 returns an AtomicFloat64 initialized to val.
func NewAtomicFloat64 ¶
func NewAtomicFloat64() *AtomicFloat64
NewAtomicFloat64 returns a zero AtomicFloat64.
func (*AtomicFloat64) Add ¶
func (f *AtomicFloat64) Add(val float64) float64
Add adds val and returns the new value.
func (*AtomicFloat64) CompareAndSwap ¶
func (f *AtomicFloat64) CompareAndSwap(old, val float64) bool
CompareAndSwap swaps if the current value equals old.
type AtomicInt32 ¶
type AtomicInt32 int32
AtomicInt32 is an atomic int32.
func ForAtomicInt32 ¶
func ForAtomicInt32(val int32) *AtomicInt32
ForAtomicInt32 returns an AtomicInt32 initialized to val.
func (*AtomicInt32) Add ¶
func (i *AtomicInt32) Add(delta int32) int32
Add adds delta and returns the new value.
func (*AtomicInt32) CompareAndSwap ¶
func (i *AtomicInt32) CompareAndSwap(old, val int32) bool
CompareAndSwap swaps if the current value equals old.
type AtomicInt64 ¶
type AtomicInt64 int64
AtomicInt64 is an atomic int64.
func ForAtomicInt64 ¶
func ForAtomicInt64(val int64) *AtomicInt64
ForAtomicInt64 returns an AtomicInt64 initialized to val.
func (*AtomicInt64) Add ¶
func (i *AtomicInt64) Add(delta int64) int64
Add adds delta and returns the new value.
func (*AtomicInt64) CompareAndSwap ¶
func (i *AtomicInt64) CompareAndSwap(old, val int64) bool
CompareAndSwap swaps if the current value equals old.
type Barrier ¶
type Barrier struct {
// contains filtered or unexported fields
}
Barrier is a mutex guarding a shared resource.
type Cond ¶
type Cond struct {
// contains filtered or unexported fields
}
Cond is a channel-based condition primitive that supports timed waits. It is used by TimeoutLimit to wake borrowers when a resource is returned.
type DoneChan ¶
type DoneChan struct {
// contains filtered or unexported fields
}
DoneChan is a channel that can be closed multiple times safely.
type LockedCalls ¶
LockedCalls ensures calls sharing a key run sequentially: a caller whose key is already in flight waits for the in-flight call to finish, then retries, so it always runs its own fn (unlike SingleFlight, which shares results).
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool is a bounded resource pool. Unlike sync.Pool it caps the number of resources, can age resources out, and runs a custom destroy on them.
func NewPool ¶
func NewPool(n int, create func() any, destroy func(any), opts ...PoolOption) *Pool
NewPool returns a Pool of at most n resources, creating them with create and destroying them with destroy. It panics on a non-positive size.
type PoolOption ¶
type PoolOption func(*Pool)
PoolOption customizes a Pool.
func WithMaxAge ¶
func WithMaxAge(d time.Duration) PoolOption
WithMaxAge returns a PoolOption that ages out idle resources older than d.
type SingleFlight ¶
type SingleFlight interface {
Do(key string, fn func() (any, error)) (any, error)
// DoEx is like Do but also reports whether this call actually executed fn
// (fresh) or shared an in-flight result.
DoEx(key string, fn func() (any, error)) (val any, fresh bool, err error)
}
SingleFlight lets concurrent calls with the same key share a single execution and its result, preventing duplicate work (cache stampede protection). Calls with different keys are independent.
type SpinLock ¶
type SpinLock struct {
// contains filtered or unexported fields
}
SpinLock is a CAS-based spin lock for very short critical sections where the cost of a mutex's goroutine suspension outweighs a few busy iterations.
type TimeoutLimit ¶
type TimeoutLimit struct {
// contains filtered or unexported fields
}
TimeoutLimit bounds concurrent borrows with a timeout, built on the channel semaphore from pkg/core/limit plus a Cond to wake blocked borrowers.
func NewTimeoutLimit ¶
func NewTimeoutLimit(n int) TimeoutLimit
NewTimeoutLimit returns a TimeoutLimit of n concurrent borrows.
func (TimeoutLimit) Borrow ¶
func (l TimeoutLimit) Borrow(timeout time.Duration) error
Borrow borrows with a timeout, returning ErrTimeout when it cannot acquire a slot in time.
func (TimeoutLimit) Return ¶
func (l TimeoutLimit) Return() error
Return returns a borrowed slot, waking one blocked borrower.
func (TimeoutLimit) TryBorrow ¶
func (l TimeoutLimit) TryBorrow() bool
TryBorrow borrows without blocking.