syncx

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2026 License: MIT Imports: 7 Imported by: 0

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

View Source
var ErrTimeout = errors.New("syncx: borrow timeout")

ErrTimeout indicates a borrow timed out.

Functions

func Guard

func Guard(lock sync.Locker, fn func())

Guard runs fn while holding lock.

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 NewAtomicBool

func NewAtomicBool() *AtomicBool

NewAtomicBool returns a zero AtomicBool.

func (*AtomicBool) CompareAndSwap

func (b *AtomicBool) CompareAndSwap(old, val bool) bool

CompareAndSwap swaps if the current value equals old.

func (*AtomicBool) Set

func (b *AtomicBool) Set(v bool)

Set sets the value.

func (*AtomicBool) True

func (b *AtomicBool) True() bool

True reports whether the value is true.

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.

func (*AtomicDuration) Set

func (d *AtomicDuration) Set(val time.Duration)

Set sets the 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.

func (*AtomicFloat64) Load

func (f *AtomicFloat64) Load() float64

Load returns the current value.

func (*AtomicFloat64) Set

func (f *AtomicFloat64) Set(val float64)

Set sets the value.

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 NewAtomicInt32

func NewAtomicInt32() *AtomicInt32

NewAtomicInt32 returns a zero AtomicInt32.

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.

func (*AtomicInt32) Load

func (i *AtomicInt32) Load() int32

Load returns the current value.

func (*AtomicInt32) Set

func (i *AtomicInt32) Set(val int32)

Set sets the value.

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 NewAtomicInt64

func NewAtomicInt64() *AtomicInt64

NewAtomicInt64 returns a zero AtomicInt64.

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.

func (*AtomicInt64) Load

func (i *AtomicInt64) Load() int64

Load returns the current value.

func (*AtomicInt64) Set

func (i *AtomicInt64) Set(val int64)

Set sets the value.

type Barrier

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

Barrier is a mutex guarding a shared resource.

func (*Barrier) Guard

func (b *Barrier) Guard(fn func())

Guard runs fn while holding the barrier's lock.

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.

func NewCond

func NewCond() *Cond

NewCond returns a Cond.

func (*Cond) Signal

func (c *Cond) Signal()

Signal wakes one waiting goroutine, if any.

func (*Cond) Wait

func (c *Cond) Wait()

Wait blocks until a signal.

func (*Cond) WaitWithTimeout

func (c *Cond) WaitWithTimeout(timeout time.Duration) (time.Duration, bool)

WaitWithTimeout blocks for a signal, returning the remaining timeout and whether a signal (rather than the timeout) was received.

type DoneChan

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

DoneChan is a channel that can be closed multiple times safely.

func NewDoneChan

func NewDoneChan() *DoneChan

NewDoneChan returns a DoneChan.

func (*DoneChan) Close

func (dc *DoneChan) Close()

Close closes the channel; it is safe to call more than once.

func (*DoneChan) Done

func (dc *DoneChan) Done() <-chan struct{}

Done returns a channel that is closed when Close is called.

type LockedCalls

type LockedCalls interface {
	Do(key string, fn func() (any, error)) (any, error)
}

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).

func NewLockedCalls

func NewLockedCalls() LockedCalls

NewLockedCalls returns a LockedCalls.

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.

func (*Pool) Get

func (p *Pool) Get() any

Get returns a resource, blocking until one is available.

func (*Pool) Put

func (p *Pool) Put(x any)

Put returns a resource to the pool.

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.

func NewSingleFlight

func NewSingleFlight() SingleFlight

NewSingleFlight returns a SingleFlight.

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.

func (*SpinLock) Lock

func (sl *SpinLock) Lock()

Lock spins until the lock is acquired.

func (*SpinLock) TryLock

func (sl *SpinLock) TryLock() bool

TryLock attempts to acquire the lock without blocking.

func (*SpinLock) Unlock

func (sl *SpinLock) Unlock()

Unlock releases the lock.

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.

Jump to

Keyboard shortcuts

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