Documentation
¶
Overview ¶
Package jack is a toolkit of composable concurrency and resilience primitives for Go services: worker pools, backpressure, rate limiting, circuit breaking, hedging, graceful shutdown, leases and consistent hashing.
Design principles ¶
- Context first. Blocking operations take a context.Context and return its error instead of leaking goroutines or blocking forever.
- Bounded by default. Every queue, pool and limiter has a capacity and fails fast (or sheds load) instead of growing without limit.
- Cheap fast paths. Semaphore.TryAcquire, RateLimiter.Allow, PLocalCounter and HashRing.Get are lock-free (atomics only); Pool.Submit takes a shared read lock solely to make channel close safe. Slow paths queue per priority so high-priority work is served first.
- Panics are values. Callbacks run through recovery and surface panics as *CaughtPanic, so a panicking handler cannot take a worker (or a process) down silently.
- Observable. Every component exposes atomic metrics; see Metrics on each type and WritePrometheus for exposition.
Building blocks ¶
Execution: Pool, Queue, Runner, Group, Go, Async/Future, Routines, Looper, Scheduler.
Backpressure and admission: Semaphore, RateLimiter (token bucket with Reservation), Throttle, AdaptiveLimiter, Bulkhead, Queue.
Resilience: Breaker, Retry, Hedger, Leaser (leases over Semaphore), Latch, Gate, Flight, OnceGroup, Pipeline (composed stages), Backoff.
Data and batching: Coalescer, Accumulator, Debouncer, Reaper (TTL), Lifetime (timed callbacks), Observable, PLocal, LRU (bounded cache), HashRing (consistent hashing).
Lifecycle: Shutdown (ordered cleanup), Doctor/Patient (health checks), Safely (context-aware mutex), KeyedLocker (per-key mutex), Helpers (Wait, Execute, Repeat, Parallel).
Composition ¶
Primitives are designed to be layered. A typical request path narrows from cheap rejection to expensive work:
Queue → Breaker → Bulkhead → AdaptiveLimiter → Throttle
→ RateLimiter → Hedger → Retry → upstream
The outermost stage fails fastest, so overloaded systems shed work before spending resources on it.
Errors ¶
Sentinel errors are exported per component (ErrPoolClosed, ErrQueueFull, ErrBreakerOpen, ErrRateLimiterClosed, ErrLeaseExpired, …) and are safe to compare with errors.Is. Retry wraps the final error with ErrRetryExhausted.
Observability ¶
Each component exposes a typed *Metrics struct built from atomics, so it can be read from any goroutine without locking. Implement MetricsCollector (or use MetricsFunc) and pass collectors to WritePrometheus to expose them to a Prometheus-compatible scraper without adding a dependency. Cumulative totals are exported as counters and state values as gauges; two collectors exposing the same series produce an error rather than a body a scraper would reject (use WithName to tell instances apart).
The counters are incremented on the operation path, so they are kept cheap: atomics only, per stripe where a component is striped (KeyedLocker sums them when Metrics is called). The exposition path sorts and renders each series once per scrape instead of inside the sort comparator.
Index ¶
- Constants
- Variables
- func Background(label string, maxRestarts int, fn func(context.Context) error) string
- func Execute(ctx context.Context, fn func() error) error
- func Go(f func() error) <-chan error
- func Logger() *ll.Logger
- func Parallel(ctx context.Context, n int, fn func(context.Context, int) error) error
- func Repeat(ctx context.Context, interval time.Duration, fn func(context.Context) error) error
- func Safe(fn func() error) error
- func SafeCtx(ctx context.Context, fn func() error) error
- func SafeCtxNoStack(ctx context.Context, fn func() error) error
- func SafeNoStack(fn func() error) error
- func Select[T any](ctx context.Context, futures ...*Future[T]) (int, T, error)
- func SleepBackoff(ctx context.Context, d time.Duration) error
- func Spawn(label string, fn func(context.Context) error)
- func VitalsWithRun(ctx context.Context, id string, operation Func, opts ...VitalsOption) error
- func VitalsWithRunCtx(ctx context.Context, id string, operation FuncCtx, opts ...VitalsOption) error
- func Wait(ctx context.Context, fn func()) error
- func WaitAll[T any](futures ...*Future[T]) ([]T, error)
- func WaitAny[T any](futures ...*Future[T]) (int, T, error)
- func WaitTimeout(timeout time.Duration, fn func()) error
- func WritePrometheus(w io.Writer, collectors ...MetricsCollector) error
- func WritePrometheusWithPrefix(w io.Writer, prefix string, collectors ...MetricsCollector) error
- type Accumulator
- type AccumulatorMetrics
- type AccumulatorOption
- type AdaptiveLimiter
- func (a *AdaptiveLimiter) Call(ctx context.Context, p Priority, fn func(context.Context) error) error
- func (a *AdaptiveLimiter) Close()
- func (a *AdaptiveLimiter) CollectMetrics(sink MetricSink)
- func (a *AdaptiveLimiter) InFlight() int
- func (a *AdaptiveLimiter) Limit() int
- func (a *AdaptiveLimiter) Metrics() *AdaptiveLimiterMetrics
- type AdaptiveLimiterMetrics
- type AdaptiveOption
- type Backoff
- type BackoffOption
- type Breaker
- type BreakerMetrics
- type BreakerOption
- func BreakerWithHalfOpenLimit(n int64) BreakerOption
- func BreakerWithOnStateChange(fn func(name string, from, to BreakerState)) BreakerOption
- func BreakerWithOpenTimeout(d time.Duration) BreakerOption
- func BreakerWithSuccessThreshold(n uint64) BreakerOption
- func BreakerWithThreshold(n uint64) BreakerOption
- type BreakerState
- type Bulkhead
- func (b *Bulkhead) AddPartition(name string, capacity int)
- func (b *Bulkhead) Available(partition string) int
- func (b *Bulkhead) Call(ctx context.Context, partition string, p Priority, ...) error
- func (b *Bulkhead) Close()
- func (b *Bulkhead) CollectMetrics(sink MetricSink)
- func (b *Bulkhead) Metrics(partition string) *SemaphoreMetrics
- func (b *Bulkhead) Partitions() []string
- func (b *Bulkhead) TryCall(ctx context.Context, partition string, p Priority, ...) error
- type BulkheadMetrics
- type BulkheadOption
- type Callback
- type CallbackCtx
- type Caller
- type CaughtPanic
- type Coalescer
- type CoalescerMetrics
- type CoalescerOption
- type Cycle
- type Debouncer
- type DebouncerOption
- type Doctor
- func (d *Doctor) Add(p *Patient) error
- func (d *Doctor) CollectMetrics(sink MetricSink)
- func (d *Doctor) GetState(id string) (PatientState, bool)
- func (d *Doctor) Metrics() *DoctorMetrics
- func (d *Doctor) Remove(id string) bool
- func (d *Doctor) SetDegraded(id string, degraded bool)
- func (d *Doctor) Stop(id string) bool
- func (d *Doctor) StopAll(timeout time.Duration)
- type DoctorMetrics
- type DoctorOption
- func DoctorWithGlobalTimeout(t time.Duration) DoctorOption
- func DoctorWithLogger(l *ll.Logger) DoctorOption
- func DoctorWithMaxConcurrent(n int) DoctorOption
- func DoctorWithObservable(obs Observable[PatientEvent]) DoctorOption
- func DoctorWithPool(pool *Pool) DoctorOption
- func DoctorWithVerbose(v bool) DoctorOption
- type Event
- type EvictReason
- type Flight
- func (f *Flight) CollectMetrics(sink MetricSink)
- func (f *Flight) Do(key string, fn func() (interface{}, error)) (res FlightResult, err error)
- func (f *Flight) DoCtx(ctx context.Context, key string, fn func() (interface{}, error)) (res FlightResult, err error)
- func (f *Flight) Forget(key string)
- func (f *Flight) InFlight(key string) bool
- func (f *Flight) Metrics() *FlightMetrics
- type FlightMetrics
- type FlightOption
- type FlightResult
- type Func
- type FuncCtx
- type Future
- func (f *Future[T]) Await() (T, error)
- func (f *Future[T]) AwaitWithContext(ctx context.Context) (T, error)
- func (f *Future[T]) AwaitWithTimeout(timeout time.Duration) (T, error)
- func (f *Future[T]) Cancel() bool
- func (f *Future[T]) IsCanceled() bool
- func (f *Future[T]) IsDone() bool
- func (f *Future[T]) Recover(ctx context.Context, fn func(error) (T, error)) *Future[T]
- func (f *Future[T]) Then(ctx context.Context, fn func(T) (any, error)) *Future[any]
- type Gate
- type Group
- type HashRing
- func (r *HashRing) Add(nodes ...string) int
- func (r *HashRing) CollectMetrics(sink MetricSink)
- func (r *HashRing) Get(key string) (string, bool)
- func (r *HashRing) GetN(key string, n int) []string
- func (r *HashRing) Len() int
- func (r *HashRing) Nodes() []string
- func (r *HashRing) Remove(nodes ...string) int
- func (r *HashRing) Replicas() int
- func (r *HashRing) VirtualNodes() int
- type HedgeGroup
- type HedgeMetrics
- type HedgeOption
- type Hedger
- type HedgerOf
- type Hook
- type HookCtx
- type Identifiable
- type KeyedLocker
- func (l *KeyedLocker) CollectMetrics(sink MetricSink)
- func (l *KeyedLocker) Do(key string, fn func())
- func (l *KeyedLocker) DoCtx(ctx context.Context, key string, fn func() error) error
- func (l *KeyedLocker) DoErr(key string, fn func() error) error
- func (l *KeyedLocker) Lock(key string)
- func (l *KeyedLocker) LockCtx(ctx context.Context, key string) error
- func (l *KeyedLocker) Metrics() *KeyedLockerMetrics
- func (l *KeyedLocker) Stripes() int
- func (l *KeyedLocker) TryLock(key string) bool
- func (l *KeyedLocker) Unlock(key string)
- type KeyedLockerMetrics
- type KeyedLockerOption
- type LRU
- func (c *LRU[K, V]) Cap() int
- func (c *LRU[K, V]) Clear()
- func (c *LRU[K, V]) CollectMetrics(sink MetricSink)
- func (c *LRU[K, V]) Delete(key K) bool
- func (c *LRU[K, V]) Get(key K) (V, bool)
- func (c *LRU[K, V]) GetOrLoad(ctx context.Context, key K, load func(context.Context) (V, error)) (V, bool, error)
- func (c *LRU[K, V]) Has(key K) bool
- func (c *LRU[K, V]) Keys() []K
- func (c *LRU[K, V]) Len() int
- func (c *LRU[K, V]) Metrics() *LRUMetrics
- func (c *LRU[K, V]) Peek(key K) (V, bool)
- func (c *LRU[K, V]) Put(key K, val V) bool
- func (c *LRU[K, V]) Range(fn func(key K, val V) bool)
- type LRUMetrics
- type LRUOption
- type Latch
- type Lease
- type LeaseMetrics
- type Leaser
- type LeaserOption
- type Lifetime
- func (lm *Lifetime) CancelTimed(id string) bool
- func (lm *Lifetime) CollectMetrics(sink MetricSink)
- func (lm *Lifetime) ExecuteCtxWithLifetime(ctx context.Context, id string, lifetime *Vitals, operation FuncCtx) error
- func (lm *Lifetime) ExecuteWithLifetime(ctx context.Context, id string, lifetime *Vitals, operation Func) error
- func (lm *Lifetime) GetRemainingDuration(id string) (time.Duration, bool)
- func (lm *Lifetime) HasPending(id string) bool
- func (lm *Lifetime) Metrics() *LifetimeMetrics
- func (lm *Lifetime) PendingCount() int
- func (lm *Lifetime) ResetTimed(id string) bool
- func (lm *Lifetime) Schedule(ctx context.Context, id string, operation FuncCtx, wait time.Duration)
- func (lm *Lifetime) ScheduleLifetimeTimed(ctx context.Context, id string, lifetime *Vitals)
- func (lm *Lifetime) ScheduleTimed(ctx context.Context, id string, callback CallbackCtx, wait time.Duration)
- func (lm *Lifetime) Stop(ids ...string)
- func (lm *Lifetime) StopAll()
- type LifetimeMetrics
- type LifetimeOption
- type LoopConfig
- type Looper
- type LooperMetrics
- type LooperOption
- func WithLooperBackoff(enabled bool) LooperOption
- func WithLooperContext(ctx context.Context) LooperOption
- func WithLooperImmediate(immediate bool) LooperOption
- func WithLooperInterval(interval time.Duration) LooperOption
- func WithLooperJitter(jitter float64) LooperOption
- func WithLooperLogger(logger *ll.Logger) LooperOption
- func WithLooperMaxInterval(max time.Duration) LooperOption
- func WithLooperMinInterval(min time.Duration) LooperOption
- func WithLooperName(name string) LooperOption
- type MetricSample
- type MetricSink
- type MetricType
- type MetricsCollector
- type MetricsFunc
- type Observable
- type Observer
- type OnceGroup
- type OnceGroupMetrics
- type PLocal
- type PLocalCounter
- type Patient
- type PatientConfig
- type PatientEvent
- type PatientMetrics
- type PatientState
- type Pipeline
- type Pool
- func (p *Pool) CollectMetrics(sink MetricSink)
- func (p *Pool) Do(fn func())
- func (p *Pool) DoCtx(ctx context.Context, fn func(ctx context.Context))
- func (p *Pool) IsClosed() bool
- func (p *Pool) Logger(extLogger *ll.Logger) *Pool
- func (p *Pool) Metrics() *PoolMetrics
- func (p *Pool) QueueSize() int
- func (p *Pool) Shutdown(timeout time.Duration) error
- func (p *Pool) Submit(ts ...Task) error
- func (p *Pool) SubmitCtx(ctx context.Context, ts ...TaskCtx) error
- func (p *Pool) Workers() int
- type PoolMetrics
- type Pooling
- type Priority
- type Queue
- func (q *Queue) Close()
- func (q *Queue) CollectMetrics(sink MetricSink)
- func (q *Queue) Depth() int
- func (q *Queue) DepthByPriority() [priorityCount]int
- func (q *Queue) Enqueue(p Priority, item any) error
- func (q *Queue) EnqueueCtx(ctx context.Context, p Priority, item any) error
- func (q *Queue) Metrics() *QueueMetrics
- type QueueMetrics
- type QueueOption
- type RateLimiter
- func (r *RateLimiter) Acquire(ctx context.Context, p Priority) error
- func (r *RateLimiter) Allow(p Priority) bool
- func (r *RateLimiter) AllowN(p Priority, n int64) bool
- func (r *RateLimiter) Burst() int64
- func (r *RateLimiter) Close()
- func (r *RateLimiter) CollectMetrics(sink MetricSink)
- func (r *RateLimiter) Metrics() *RateLimiterMetrics
- func (r *RateLimiter) Release()
- func (r *RateLimiter) Replenish(n int64)
- func (r *RateLimiter) Reserve(n int64, opts ...ReserveOption) *Reservation
- func (r *RateLimiter) ReserveMetrics() *ReservationMetrics
- func (r *RateLimiter) Tokens() int64
- type RateLimiterMetrics
- type RateLimiterOption
- type Reaper
- func (r *Reaper) Clear() int
- func (r *Reaper) CollectMetrics(sink MetricSink)
- func (r *Reaper) Count() int
- func (r *Reaper) Deadline() (time.Time, bool)
- func (r *Reaper) Metrics() *ReaperMetrics
- func (r *Reaper) Register(h ReaperHandler)
- func (r *Reaper) Remove(id string) bool
- func (r *Reaper) Start()
- func (r *Reaper) Stop()
- func (r *Reaper) Touch(id string)
- func (r *Reaper) TouchAt(id string, deadline time.Time)
- type ReaperHandler
- type ReaperMetrics
- type ReaperOption
- type ReaperTask
- type Reservation
- type ReservationMetrics
- type ReserveOption
- type Retry
- type RetryMetrics
- type RetryOption
- func RetryWithBackoff(b *Backoff) RetryOption
- func RetryWithBaseDelay(d time.Duration) RetryOption
- func RetryWithJitter(enabled bool) RetryOption
- func RetryWithMaxAttempts(n int) RetryOption
- func RetryWithMaxDelay(d time.Duration) RetryOption
- func RetryWithMultiplier(m float64) RetryOption
- func RetryWithOnRetry(fn func(attempt int, err error)) RetryOption
- func RetryWithRetryIf(fn func(error) bool) RetryOption
- type RingOption
- type Routine
- type RoutineInfo
- type RoutineOption
- type RoutineState
- type Routines
- func (r *Routines) Active() int
- func (r *Routines) Background(label string, maxRestarts int, fn func(context.Context) error) string
- func (r *Routines) Cancel(id string) bool
- func (r *Routines) CollectMetrics(sink MetricSink)
- func (r *Routines) Forget(id string) bool
- func (r *Routines) Info(id string) (RoutineInfo, bool)
- func (r *Routines) List() []RoutineInfo
- func (r *Routines) Metrics() *RoutinesMetrics
- func (r *Routines) Spawn(label string, fn func(context.Context) error) string
- func (r *Routines) SpawnCtx(ctx context.Context, label string, fn func(context.Context) error) string
- func (r *Routines) Stop()
- func (r *Routines) StopTimeout(timeout time.Duration) error
- func (r *Routines) Wait()
- type RoutinesMetrics
- type Runner
- type RunnerOption
- type Safely
- type Schedule
- type Scheduler
- func (s *Scheduler) Do(ts ...Task) error
- func (s *Scheduler) DoCtx(taskExecCtx context.Context, ts ...TaskCtx) error
- func (s *Scheduler) Entries() []cron.Entry
- func (s *Scheduler) Name() string
- func (s *Scheduler) NextRun() (time.Time, bool)
- func (s *Scheduler) Running() bool
- func (s *Scheduler) Stop() error
- func (s *Scheduler) Terminate(cancelPool bool) error
- type Scheduling
- type Semaphore
- func (s *Semaphore) Acquire(ctx context.Context, p Priority) error
- func (s *Semaphore) Available() int
- func (s *Semaphore) Capacity() int
- func (s *Semaphore) Close()
- func (s *Semaphore) CollectMetrics(sink MetricSink)
- func (s *Semaphore) Metrics() *SemaphoreMetrics
- func (s *Semaphore) Release()
- func (s *Semaphore) TryAcquire(p Priority) bool
- func (s *Semaphore) TryAcquireN(p Priority, n int) bool
- type SemaphoreMetrics
- type SemaphoreOption
- type Shutdown
- func (sm *Shutdown) Done() <-chan struct{}
- func (sm *Shutdown) GetStats() *ShutdownStats
- func (sm *Shutdown) IsShuttingDown() bool
- func (sm *Shutdown) Register(fn any) error
- func (sm *Shutdown) RegisterCall(name string, fn Func) error
- func (sm *Shutdown) RegisterCloser(name string, closer io.Closer) error
- func (sm *Shutdown) RegisterFunc(name string, fn func()) error
- func (sm *Shutdown) RegisterWithContext(name string, fn FuncCtx) error
- func (sm *Shutdown) RegisterWithPriority(name string, priority int, fns ...FuncCtx) error
- func (sm *Shutdown) TriggerShutdown() *ShutdownStats
- func (sm *Shutdown) Wait() *ShutdownStats
- func (sm *Shutdown) WaitChan() <-chan *ShutdownStats
- type ShutdownError
- type ShutdownOption
- type ShutdownStats
- type Stage
- func AdaptiveStage(a *AdaptiveLimiter, p Priority) Stage
- func BreakerStage(b *Breaker) Stage
- func BulkheadStage(b *Bulkhead, partition string, p Priority) Stage
- func RateLimiterStage(rl *RateLimiter, p Priority) Stage
- func RetryStage(r *Retry) Stage
- func SemaphoreStage(s *Semaphore, p Priority) Stage
- func ThrottleStage(t *Throttle, p Priority) Stage
- func TimeoutStage(d time.Duration) Stage
- type Task
- type TaskCtx
- type Throttle
- type ThrottleMetrics
- type ThrottleOption
- type Vitals
- type VitalsOption
- type WaitGroup
Constants ¶
const ( // NoLimitCalls is the default for maxCalls, meaning no limit on the number of calls before execution. // Set to math.MaxInt to indicate unbounded call accumulation. NoLimitCalls = math.MaxInt // NoLimitWait is the default for maxWait, meaning no time limit before forced execution. // Set to time.Duration(math.MaxInt64) to disable the maximum wait enforcement. NoLimitWait = time.Duration(math.MaxInt64) )
Variables ¶
var ( ErrFutureCanceled = fmt.Errorf("future was canceled") ErrFutureTimeout = fmt.Errorf("future timed out") )
var ( ErrSemaphoreClosed = errors.New("semaphore closed") ErrRateLimiterClosed = errors.New("rate limiter closed") ErrThrottleClosed = errors.New("adaptive throttle closed") ErrQueueClosed = errors.New("priority queue closed") )
var ( ErrBreakerOpen = errors.New("circuit breaker open") ErrBreakerClosed = errors.New("circuit breaker instance closed") )
var ( ErrBulkheadFull = errors.New("bulkhead full") ErrBulkheadClosed = errors.New("bulkhead closed") ErrBulkheadNotFound = errors.New("bulkhead partition not found") )
var ( ErrLeaseExpired = errors.New("lease expired") ErrLeaseReleased = errors.New("lease already released") ErrLeaseInvalid = errors.New("lease semaphore closed") )
var ( // ErrThrottled is returned by ThrottleStage when the throttle sheds a call. ErrThrottled = errors.New("jack: throttled") // ErrPipelineNoTerminal is returned when a Pipeline is executed without an // operation. ErrPipelineNoTerminal = errors.New("jack: pipeline has no terminal function") )
var ( ErrReservationExpired = errors.New("reservation expired") ErrReservationCancelled = errors.New("reservation cancelled") )
var ( ErrRetryExhausted = errors.New("retry attempts exhausted") // ErrRetryNilFunc is returned by Do when no function is supplied. ErrRetryNilFunc = errors.New("jack: retry function is nil") )
var ( // ErrPoolClosed indicates the worker pool has been closed. ErrPoolClosed = errors.New("pool has been closed") // ErrRunnerClosed indicates the runner has been closed. ErrRunnerClosed = errors.New("runner has been closed") // ErrSchedulerClosed indicates the scheduler has been closed. ErrSchedulerClosed = errors.New("scheduler has been closed") // ErrTaskTimeout indicates a task exceeded its execution timeout. ErrTaskTimeout = errors.New("task execution timed out") // ErrTaskPanic indicates a task panicked during execution. ErrTaskPanic = errors.New("task panicked during execution") // ErrQueueFull indicates the task queue is full and cannot accept new tasks. ErrQueueFull = errors.New("task queue is full") // ErrShutdownTimedOut indicates the pool shutdown exceeded its timeout. ErrShutdownTimedOut = errors.New("shutdown timed out") )
Errors returned by the worker pool and task execution.
var ( ErrSchedulerJobAlreadyRunning = errors.New("scheduler: job is already running") ErrSchedulerNotRunning = errors.New("scheduler: job is not running or already stopped") ErrSchedulerPoolNil = errors.New("scheduler: pool cannot be nil") ErrSchedulerNameMissing = errors.New("scheduler: name cannot be empty") )
var ErrAccumulatorClosed = errors.New("accumulator closed")
ErrAccumulatorClosed is returned when operating on a closed accumulator.
var ErrCoalescerClosed = errors.New("coalescer closed")
ErrCoalescerClosed is returned when operating on a closed coalescer.
var ErrHedgeAllFailed = errors.New("all hedged requests failed")
var ErrLRUNilLoader = errors.New("jack: lru loader is nil")
ErrLRUNilLoader is returned by GetOrLoad when no loader is supplied.
Functions ¶
func Background ¶
func Go ¶
Go runs a function in a standalone goroutine with error and panic handling. It returns a buffered channel that receives the function’s error or a CaughtPanic. The channel is closed after the error or completion. Thread-safe via goroutine and channel operations. Example:
errCh := Go(func() error { return nil }) // Runs a function and returns error channel
func Logger ¶
Logger returns the default logger for the jack package. Thread-safe as it returns a pre-initialized logger. Example:
log := jack.Logger() // Retrieves the default logger
func Safe ¶
Safe executes fn with panic recovery. If fn panics, a *CaughtPanic error is returned. Otherwise, the error returned by fn is returned. If fn is nil, Safe will return a *CaughtPanic. Example:
err := Safe(func() error {
// May panic or return an error
return nil
})
if cp, ok := err.(*CaughtPanic); ok {
fmt.Printf("Caught panic: %v\nStack: %s\n", cp.Value, cp.Stack)
}
func SafeCtx ¶
SafeCtx executes fn with context support and panic recovery. The function fn will be interrupted if the context is canceled (e.g., via context.WithTimeout). If fn is nil, SafeCtx will return a *CaughtPanic. If ctx is nil, SafeCtx will panic when ctx.Err() is called. Example:
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
err := SafeCtx(ctx, func() error {
// No need to check ctx
time.Sleep(100 * time.Millisecond)
return nil
})
// err will likely be context.DeadlineExceeded due to timeout
func SafeCtxNoStack ¶
SafeCtxNoStack executes fn with context support, panic recovery, but no stack trace. The function fn will be interrupted if the context is canceled (e.g., via context.WithTimeout). If fn is nil, SafeCtxNoStack will return a *CaughtPanic (without stack). If ctx is nil, SafeCtxNoStack will panic when ctx.Err() is called. Example:
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
err := SafeCtxNoStack(ctx, func() error {
// No need to check ctx
time.Sleep(100 * time.Millisecond)
return nil
})
// err will likely be context.DeadlineExceeded due to timeout
func SafeNoStack ¶
SafeNoStack executes fn with panic recovery but no stack trace. If fn panics, a *CaughtPanic error (without stack) is returned. Otherwise, fn's error. If fn is nil, SafeNoStack will return a *CaughtPanic. Example:
err := SafeNoStack(func() error {
panic("error")
return nil
})
if cp, ok := err.(*CaughtPanic); ok {
fmt.Printf("Caught panic: %v, Stack: %v\n", cp.Value, cp.Stack == nil)
}
func Select ¶
Select blocks until ctx is cancelled or the first Future completes. Returns the index and value of the winning Future.
func SleepBackoff ¶
SleepBackoff waits for d, returning early with ctx.Err() if the context is done. A non-positive duration returns nil immediately.
func VitalsWithRun ¶
VitalsWithRun creates a Vitals instance and executes an operation with it in one call. It accepts functional options to configure hooks before immediately running the operation. This convenience function combines NewVitals and Execute for simplified inline usage.
func VitalsWithRunCtx ¶
func VitalsWithRunCtx(ctx context.Context, id string, operation FuncCtx, opts ...VitalsOption) error
VitalsWithRunCtx creates a Vitals instance and executes a context-aware operation in one call. It accepts functional options to configure hooks before immediately running the operation with context. This convenience function combines NewVitals and ExecuteCtx for simplified inline usage.
func WaitAll ¶
WaitAll blocks until all futures complete and returns their results in order. Returns an error immediately if any Future fails.
func WaitAny ¶
WaitAny blocks until the first Future completes and returns its index and value. Remaining futures continue running; only the first result is returned.
func WaitTimeout ¶
WaitTimeout runs fn in a goroutine and blocks until it completes or timeout elapses.
func WritePrometheus ¶
func WritePrometheus(w io.Writer, collectors ...MetricsCollector) error
WritePrometheus writes the collected samples in Prometheus text exposition format (version 0.0.4). No third-party dependency is required, so the output can be scraped by Prometheus, VictoriaMetrics, the OTel collector, or anything else that understands the format.
if err := jack.WritePrometheus(w,
jack.WithName("api", pool),
jack.WithName("api", queue),
); err != nil {
return err
}
Output is deterministic: samples are sorted by name and then by label set, and each metric name is described once with # HELP / # TYPE.
func WritePrometheusWithPrefix ¶
func WritePrometheusWithPrefix(w io.Writer, prefix string, collectors ...MetricsCollector) error
WritePrometheusWithPrefix is WritePrometheus with every metric name prefixed (prefix "jack" turns "pool_tasks_completed" into "jack_pool_tasks_completed"). An empty prefix is a no-op.
Two collectors that emit the same series (same name and labels) result in an error rather than output Prometheus would reject at scrape time; disambiguate them with WithName.
Types ¶
type Accumulator ¶
type Accumulator struct {
// contains filtered or unexported fields
}
Accumulator batches items and flushes them based on size, time, or explicit caller demand — whichever comes first.
Modes:
- Size only: AccumulatorWithMaxSize(n) — flush when buffer full
- Time only: AccumulatorWithMaxWait(d) — flush after d since first add
- Both: size AND time — flush on whichever fires first
- Explicit: neither option — only Flush() triggers flush
maxSize and maxWait are trigger thresholds, not hard limits: a flush drains whatever is buffered, so concurrent writers can briefly push a batch past maxSize before the flush runs. A flush handler error is returned to the caller (Add or Flush) and also counted in Errors, logged, and published as an "accumulator_flush_failed" event; the failed batch is not retried.
Thread-safe. Call NewAccumulator to create one.
func NewAccumulator ¶
func NewAccumulator(flush func([]any) error, opts ...AccumulatorOption) *Accumulator
NewAccumulator creates a batch accumulator.
func (*Accumulator) Add ¶
func (a *Accumulator) Add(item any) error
Add appends an item. Triggers auto-flush if maxSize reached or maxWait elapsed since first add. Returns ErrAccumulatorClosed if closed, and the flush handler's error when this call itself triggered a size flush.
func (*Accumulator) Close ¶
func (a *Accumulator) Close() error
Close stops accepting items and flushes remaining batch.
func (*Accumulator) CollectMetrics ¶
func (a *Accumulator) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for Accumulator.
func (*Accumulator) Flush ¶
func (a *Accumulator) Flush() error
Flush immediately flushes the current batch. Safe concurrently. Returns nil if buffer is empty.
func (*Accumulator) Metrics ¶
func (a *Accumulator) Metrics() *AccumulatorMetrics
Metrics returns current metrics.
type AccumulatorMetrics ¶
type AccumulatorMetrics struct {
Flushes atomic.Uint64 // total flush calls
ItemsAdded atomic.Uint64 // items successfully added
ItemsFlushed atomic.Uint64 // items sent to flush handler
Errors atomic.Uint64 // flush handler errors
TimerFlushes atomic.Uint64 // flushes triggered by timer
SizeFlushes atomic.Uint64 // flushes triggered by maxSize
}
AccumulatorMetrics tracks operational statistics.
type AccumulatorOption ¶
type AccumulatorOption func(*accumulatorOpt)
AccumulatorOption configures an Accumulator.
func AccumulatorWithMaxSize ¶
func AccumulatorWithMaxSize(n int) AccumulatorOption
AccumulatorWithMaxSize enables automatic flush when buffer reaches n items.
func AccumulatorWithMaxWait ¶
func AccumulatorWithMaxWait(d time.Duration) AccumulatorOption
AccumulatorWithMaxWait enables automatic flush after d since first add.
func AccumulatorWithObservable ¶
func AccumulatorWithObservable(obs Observable[Event]) AccumulatorOption
AccumulatorWithObservable attaches an event observer.
type AdaptiveLimiter ¶
type AdaptiveLimiter struct {
// contains filtered or unexported fields
}
AdaptiveLimiter adjusts its concurrency limit dynamically based on observed round-trip latency using a gradient-style AIMD controller.
When RTT ≤ target → additive increase (limit++). When RTT > target → multiplicative decrease (limit × ratio).
Composable with the rest of the library: the AdaptiveLimiter owns a Semaphore internally and exposes the same Call / Close interface as Breaker and Bulkhead.
func NewAdaptiveLimiter ¶
func NewAdaptiveLimiter(opts ...AdaptiveOption) *AdaptiveLimiter
NewAdaptiveLimiter creates an AdaptiveLimiter with sensible defaults.
func (*AdaptiveLimiter) Call ¶
func (a *AdaptiveLimiter) Call(ctx context.Context, p Priority, fn func(context.Context) error) error
Call executes fn if a slot is available within the adaptive limit. It measures RTT and adjusts the limit after each call returns.
func (*AdaptiveLimiter) Close ¶
func (a *AdaptiveLimiter) Close()
Close shuts down the underlying semaphore, unblocking all pending callers.
func (*AdaptiveLimiter) CollectMetrics ¶
func (a *AdaptiveLimiter) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for AdaptiveLimiter.
func (*AdaptiveLimiter) InFlight ¶
func (a *AdaptiveLimiter) InFlight() int
InFlight returns the number of concurrently executing calls.
func (*AdaptiveLimiter) Limit ¶
func (a *AdaptiveLimiter) Limit() int
Limit returns the current concurrency limit.
func (*AdaptiveLimiter) Metrics ¶
func (a *AdaptiveLimiter) Metrics() *AdaptiveLimiterMetrics
Metrics returns the limiter's operational metrics.
type AdaptiveLimiterMetrics ¶
type AdaptiveLimiterMetrics struct {
Acquired atomic.Uint64 // successful acquisitions
Rejected atomic.Uint64 // rejections when at limit
LimitIncr atomic.Uint64 // times the limit was increased
LimitDecr atomic.Uint64 // times the limit was decreased
CurrentLimit atomic.Int64 // live concurrency limit
CurrentInFlight atomic.Int64 // goroutines currently executing
AvgRTTNs atomic.Int64 // EWMA RTT in nanoseconds
}
AdaptiveLimiterMetrics tracks adaptive limiter operational statistics.
type AdaptiveOption ¶
type AdaptiveOption func(*AdaptiveLimiter)
AdaptiveOption configures an AdaptiveLimiter.
func AdaptiveWithInitialLimit ¶
func AdaptiveWithInitialLimit(n int) AdaptiveOption
AdaptiveWithInitialLimit sets the starting concurrency limit (default 10).
func AdaptiveWithMaxLimit ¶
func AdaptiveWithMaxLimit(n int) AdaptiveOption
AdaptiveWithMaxLimit sets the ceiling above which the limit will not grow (default 200).
func AdaptiveWithMinLimit ¶
func AdaptiveWithMinLimit(n int) AdaptiveOption
AdaptiveWithMinLimit sets the floor below which the limit will not drop (default 1).
func AdaptiveWithSmoothingFactor ¶
func AdaptiveWithSmoothingFactor(f float64) AdaptiveOption
AdaptiveWithSmoothingFactor controls EWMA smoothing for RTT samples (default 0.1). Closer to 0 = slower adaptation; closer to 1 = reacts to every sample.
func AdaptiveWithTargetP50 ¶
func AdaptiveWithTargetP50(d time.Duration) AdaptiveOption
AdaptiveWithTargetP50 sets the target median RTT the controller aims to maintain. When measured RTT exceeds this, the limit is reduced (default 100ms).
type Backoff ¶
type Backoff struct {
// contains filtered or unexported fields
}
Backoff computes exponential backoff delays with optional jitter.
It is stateless and safe for concurrent use: callers pass the attempt index (0-based), which makes it usable inside request handlers without shared state.
backoff := jack.NewBackoff(
jack.BackoffWithBase(50*time.Millisecond),
jack.BackoffWithMax(2*time.Second),
jack.BackoffWithFullJitter(true),
)
for attempt := 0; ; attempt++ {
if err := call(); err == nil {
return nil
} else if waitErr := jack.SleepBackoff(ctx, backoff.Delay(attempt)); waitErr != nil {
return waitErr
}
}
func NewBackoff ¶
func NewBackoff(opts ...BackoffOption) *Backoff
NewBackoff creates a Backoff. Defaults: 100ms base, 30s cap, 2.0 multiplier, no jitter.
func (*Backoff) Delay ¶
Delay returns the wait before attempt (0-based):
base × multiplier^attempt, capped at max, then jittered
The growth is exponential: with the defaults, attempts 0..5 yield 100ms, 200ms, 400ms, 800ms, 1.6s and 3.2s. The cap is hard: the returned value is always in [0, max], even with jitter, so callers can rely on it to bound latency. Negative attempts are treated as 0. The result is never negative and Delay allocates nothing, so it is safe in tight retry loops.
type BackoffOption ¶
type BackoffOption func(*backoffConfig)
BackoffOption configures a Backoff.
func BackoffWithBase ¶
func BackoffWithBase(d time.Duration) BackoffOption
BackoffWithBase sets the delay used for attempt 0 (default 100ms). Zero disables waiting; negative values are ignored.
func BackoffWithFullJitter ¶
func BackoffWithFullJitter(enabled bool) BackoffOption
BackoffWithFullJitter selects the "full jitter" strategy: each delay is drawn uniformly from [0, cappedDelay] instead of being spread around it. This is the variant recommended for retried network calls (it decorrelates clients most aggressively), and it is what Retry uses by default. It takes precedence over BackoffWithJitter.
func BackoffWithJitter ¶
func BackoffWithJitter(ratio float64) BackoffOption
BackoffWithJitter spreads each delay by ±ratio (0 disables jitter, 0.2 means ±20%). Jitter prevents retry storms from synchronising across processes, but keeps a floor of (1-ratio)×delay: a client that just failed still waits. Values outside [0, 1) are ignored. See BackoffWithFullJitter for the variant that can spread all the way down to zero.
func BackoffWithMax ¶
func BackoffWithMax(d time.Duration) BackoffOption
BackoffWithMax sets the hard cap for the returned delay (default 30s). Jitter is applied inside the cap, so the result never exceeds it. Non-positive values are ignored.
func BackoffWithMultiplier ¶
func BackoffWithMultiplier(m float64) BackoffOption
BackoffWithMultiplier sets the growth factor per attempt (default 2.0). A value of 1 produces a constant delay; values below 1 are ignored.
type Breaker ¶
type Breaker struct {
// contains filtered or unexported fields
}
Breaker implements the circuit-breaker pattern.
States:
Closed → normal; failures accumulate until threshold is reached.
Open → tripped; all calls rejected for openTimeout, then → HalfOpen.
HalfOpen → probe; up to halfOpenLimit calls pass through. Enough successes
→ Closed; any failure → Open.
Call is the only entry point. It wraps the user function and manages all state transitions atomically without holding a mutex during execution.
func NewBreaker ¶
func NewBreaker(name string, opts ...BreakerOption) *Breaker
NewBreaker creates a Breaker with the given name and options.
func (*Breaker) Call ¶
Call executes fn if the circuit allows it, updating state based on the result. Returns ErrBreakerOpen if the circuit is open. The context is passed through to fn; if fn returns context.DeadlineExceeded or context.Canceled the call counts as a timeout (also a failure).
func (*Breaker) CollectMetrics ¶
func (b *Breaker) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for Breaker.
func (*Breaker) Metrics ¶
func (b *Breaker) Metrics() *BreakerMetrics
Metrics returns the breaker's operational metrics.
func (*Breaker) Reset ¶
func (b *Breaker) Reset()
Reset forces the circuit back to Closed and clears all counters. Use for testing or manual operator intervention.
func (*Breaker) State ¶
func (b *Breaker) State() BreakerState
State returns the current circuit state.
type BreakerMetrics ¶
type BreakerMetrics struct {
Requests atomic.Uint64 // total calls attempted
Successes atomic.Uint64 // calls that returned nil
Failures atomic.Uint64 // calls that returned a non-nil error
Rejections atomic.Uint64 // calls rejected while open
Timeouts atomic.Uint64 // calls that exceeded their deadline
StateChanges atomic.Uint64 // total transitions between states
ConsecSuccess atomic.Int64 // consecutive successes in half-open
ConsecFailure atomic.Int64 // consecutive failures in current window
}
BreakerMetrics tracks circuit breaker operational statistics.
type BreakerOption ¶
type BreakerOption func(*Breaker)
BreakerOption configures a Breaker.
func BreakerWithHalfOpenLimit ¶
func BreakerWithHalfOpenLimit(n int64) BreakerOption
BreakerWithHalfOpenLimit sets the maximum number of concurrent probes allowed in half-open state (default 1).
func BreakerWithOnStateChange ¶
func BreakerWithOnStateChange(fn func(name string, from, to BreakerState)) BreakerOption
BreakerWithOnStateChange registers a callback invoked on every state transition.
func BreakerWithOpenTimeout ¶
func BreakerWithOpenTimeout(d time.Duration) BreakerOption
BreakerWithOpenTimeout sets how long the circuit stays open before moving to half-open to probe recovery (default 10s).
func BreakerWithSuccessThreshold ¶
func BreakerWithSuccessThreshold(n uint64) BreakerOption
BreakerWithSuccessThreshold sets how many consecutive successes in half-open state are required before the circuit closes again (default 2).
func BreakerWithThreshold ¶
func BreakerWithThreshold(n uint64) BreakerOption
BreakerWithThreshold sets the number of consecutive failures required to open the circuit (default 5).
type BreakerState ¶
type BreakerState int32
BreakerState represents the state of a circuit breaker.
const ( // BreakerClosed is the normal operating state — calls pass through. BreakerClosed BreakerState = iota // BreakerOpen means the circuit is tripped — calls are rejected immediately. BreakerOpen // BreakerHalfOpen means a limited probe is allowed to test recovery. BreakerHalfOpen )
func (BreakerState) String ¶
func (s BreakerState) String() string
String returns a human-readable label for the circuit state.
type Bulkhead ¶
type Bulkhead struct {
// contains filtered or unexported fields
}
Bulkhead isolates failure domains by giving each named partition its own bounded concurrency budget backed by an independent Semaphore.
When partition A is saturated, partition B is completely unaffected. This is the standard pattern for protecting a shared resource (e.g. a DB connection pool) from being monopolised by one upstream caller.
Usage:
bh := jack.NewBulkhead(jack.BulkheadWithPartition("payments", 20),
jack.BulkheadWithPartition("reports", 5))
err := bh.Call(ctx, "payments", jack.PriorityHigh, func(ctx context.Context) error {
return db.Query(ctx, ...)
})
func NewBulkhead ¶
func NewBulkhead(opts ...BulkheadOption) *Bulkhead
NewBulkhead creates a Bulkhead with the given options.
func (*Bulkhead) AddPartition ¶
AddPartition registers a new named partition at runtime. If a partition with that name already exists it is left unchanged.
func (*Bulkhead) Available ¶
Available returns the number of free concurrency slots for the named partition.
func (*Bulkhead) Call ¶
func (b *Bulkhead) Call(ctx context.Context, partition string, p Priority, fn func(context.Context) error) error
Call executes fn within the named partition's concurrency limit. If the partition doesn't exist and defaultCap > 0, it is auto-created. Returns ErrBulkheadFull if all slots are taken and the context expires, ErrBulkheadNotFound if auto-creation is disabled and the partition is unknown.
func (*Bulkhead) Close ¶
func (b *Bulkhead) Close()
Close shuts down all partitions. Subsequent calls return ErrBulkheadClosed.
func (*Bulkhead) CollectMetrics ¶
func (b *Bulkhead) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for Bulkhead, emitting one series per partition with a "partition" label.
func (*Bulkhead) Metrics ¶
func (b *Bulkhead) Metrics(partition string) *SemaphoreMetrics
Metrics returns the semaphore metrics for the named partition, or nil if not found.
func (*Bulkhead) Partitions ¶
Partitions returns a snapshot of all registered partition names.
type BulkheadMetrics ¶
type BulkheadMetrics struct {
Acquired SemaphoreMetrics // reuses semaphore metrics per partition
}
BulkheadMetrics tracks statistics for a single named partition.
type BulkheadOption ¶
type BulkheadOption func(*Bulkhead)
BulkheadOption configures a Bulkhead.
func BulkheadWithDefaultCapacity ¶
func BulkheadWithDefaultCapacity(capacity int) BulkheadOption
BulkheadWithDefaultCapacity sets the capacity used when auto-creating partitions on first use (default 10). Set to 0 to disable auto-creation.
func BulkheadWithPartition ¶
func BulkheadWithPartition(name string, capacity int) BulkheadOption
BulkheadWithPartition pre-registers a named partition with the given capacity.
func BulkheadWithSemaphoreOptions ¶
func BulkheadWithSemaphoreOptions(opts ...SemaphoreOption) BulkheadOption
BulkheadWithSemaphoreOptions passes additional SemaphoreOptions to every partition semaphore (e.g. CoDel settings).
type Callback ¶
type Callback func(id string)
Callback defines a callback function without error return for post-execution logic. It accepts a unique identifier string for the operation that completed. Use this type for notifications, cleanup, or metrics that do not affect flow control.
type CallbackCtx ¶
CallbackCtx defines a context-aware callback function without error return. It accepts a context for cancellation and an identifier for the completed operation. Use this type when callbacks need to coordinate with context deadlines or values.
type Caller ¶
type Caller struct {
// contains filtered or unexported fields
}
Caller defines a group of calls that execute together
type CaughtPanic ¶
type CaughtPanic struct {
Value interface{} // The value passed to panic()
Stack []byte // The stack trace (may be empty if not collected)
}
CaughtPanic represents a panic that was caught during execution. It is returned by every component that runs user callbacks, so panicking code surfaces as a value instead of unwinding the caller's goroutine.
func (*CaughtPanic) Error ¶
func (c *CaughtPanic) Error() string
Error implements the error interface.
func (*CaughtPanic) String ¶
func (c *CaughtPanic) String() string
String provides a formatted string representation of the panic.
func (*CaughtPanic) Unwrap ¶
func (c *CaughtPanic) Unwrap() error
Unwrap provides compatibility with errors.Is/As.
type Coalescer ¶
type Coalescer struct {
// contains filtered or unexported fields
}
Coalescer merges discrete items into batches and flushes them together. It is useful for write coalescing, metrics aggregation, or event batching.
func NewCoalescer ¶
func NewCoalescer(flush func([]interface{}) error, maxSize int, maxWait time.Duration, opts ...CoalescerOption) *Coalescer
NewCoalescer creates a batching coalescer.
func (*Coalescer) Add ¶
Add appends an item to the batch. If the batch reaches maxSize, it is flushed immediately. Otherwise a timer is started for maxWait.
func (*Coalescer) CollectMetrics ¶
func (c *Coalescer) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for Coalescer.
func (*Coalescer) Metrics ¶
func (c *Coalescer) Metrics() *CoalescerMetrics
Metrics returns a snapshot of current metrics.
type CoalescerMetrics ¶
type CoalescerMetrics struct {
Batches atomic.Uint64
Items atomic.Uint64
Flushes atomic.Uint64
Errors atomic.Uint64
}
CoalescerMetrics holds atomic counters for observability.
type CoalescerOption ¶
type CoalescerOption func(*coalesceOpt)
CoalescerOption is a functional option for configuring Coalescer.
func CoalescerWithObservable ¶
func CoalescerWithObservable(obs Observable[Event]) CoalescerOption
CoalescerWithObservable sets an observable for coalescer events.
type Cycle ¶
type Cycle func(*Scheduling)
Cycle is a functional option type for configuring a Scheduler. It uses the functional options pattern to allow flexible and extensible configuration of scheduler settings.
func SchedulingWithObservable ¶
func SchedulingWithObservable(obs Observable[Schedule]) Cycle
SchedulingWithObservable returns a Cycle to set an observable for events.
type Debouncer ¶
type Debouncer struct {
// contains filtered or unexported fields
}
Debouncer groups calls to a function, executing only after a period of inactivity or when certain thresholds (max calls or max wait) are met. It uses a mutex for thread-safety and timers for delay and maxWait enforcement.
func NewDebouncer ¶
func NewDebouncer(options ...DebouncerOption) *Debouncer
NewDebouncer creates a new Debouncer instance configured with the given functional options. The WithDebounceDelay option is required; other options are optional with sensible defaults. Timers are initialized but stopped until first use; limits default to no restrictions.
func (*Debouncer) Cancel ¶
func (d *Debouncer) Cancel()
Cancel prevents a pending debounced function from executing by clearing internal state. It stops all active timers and resets call counters without invoking the pending function. Safe to call multiple times or when no function is pending; has no effect if already closed.
func (*Debouncer) Do ¶
func (d *Debouncer) Do(fn func())
Do schedules the given function to execute after the configured delay period of inactivity. Each call resets the delay timer and updates the pending function to the latest provided. If maxCalls or maxWait thresholds are met, execution happens immediately without waiting.
func (*Debouncer) Flush ¶
func (d *Debouncer) Flush()
Flush executes any pending debounced function immediately without waiting for timers. It stops all active timers, executes the function in a new goroutine, and resets state. If no function is currently pending, this method does nothing and returns safely.
func (*Debouncer) IsPending ¶
IsPending returns true if a debounced function is currently waiting to be executed. This method checks the internal call counter while holding the mutex for thread safety. Useful for monitoring debouncer state without triggering any execution or side effects.
func (*Debouncer) Stop ¶
func (d *Debouncer) Stop()
Stop permanently shuts down the debouncer and prevents any future function executions. It sets the closed flag, clears pending functions, and stops all active timers immediately. After calling Stop, any subsequent Do calls will be ignored and no operations will proceed.
type DebouncerOption ¶
type DebouncerOption func(*Debouncer)
DebouncerOption is a functional option for configuring the Debouncer. These options allow customization of delay, maximum calls, and maximum wait time. Use WithDebounceDelay (required), WithDebounceMaxCalls, and WithDebounceMaxWait to set behaviors.
func WithDebounceDelay ¶
func WithDebounceDelay(d time.Duration) DebouncerOption
WithDebounceDelay sets the delay period after the last call before execution. This is a required option; without it, the debouncer will not function correctly. The delay determines the inactivity period needed to trigger the debounced function.
func WithDebounceMaxCalls ¶
func WithDebounceMaxCalls(count int) DebouncerOption
WithDebounceMaxCalls sets the maximum number of calls before immediate execution. By default, there is no limit on call accumulation using NoLimitCalls constant. Once this threshold is reached, the function executes regardless of remaining delay.
func WithDebounceMaxWait ¶
func WithDebounceMaxWait(limit time.Duration) DebouncerOption
WithDebounceMaxWait sets the maximum wait time before forced function execution. This check happens on each Do call, so total wait could be up to maxWait plus delay. A separate timer ensures execution after maxWait even if no new calls occur.
func WithDebouncerPool ¶
func WithDebouncerPool(pool *Pool) DebouncerOption
WithDebouncerPool sets a custom pool for executing debounced functions asynchronously. If not provided via this option, the default package-level pool will be used instead. This allows control over goroutine scheduling and resource management for executions.
type Doctor ¶
type Doctor struct {
// contains filtered or unexported fields
}
Doctor monitors patients with global and per-patient metrics.
func NewDoctor ¶
func NewDoctor(opts ...DoctorOption) *Doctor
NewDoctor creates and initializes a new Doctor instance with the provided configuration options. It sets up the patient priority queue, starts the scheduler goroutine, and creates a default pool if needed. The returned Doctor is immediately active and ready to accept patient registrations via Add.
func (*Doctor) Add ¶
Add registers a new patient with the Doctor, validating its configuration and scheduling its first check. If a patient with the same ID already exists, it is replaced and the old instance is marked removed. Returns an error if the patient lacks a required Check function or if registration fails.
func (*Doctor) CollectMetrics ¶
func (d *Doctor) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for Doctor.
func (*Doctor) GetState ¶
func (d *Doctor) GetState(id string) (PatientState, bool)
GetState retrieves the current health state of a patient by ID along with an existence indicator. If the patient is not registered, it returns PatientUnknown and false for the existence flag. This method provides thread-safe read access to patient state without modifying any internal data.
func (*Doctor) Metrics ¶
func (d *Doctor) Metrics() *DoctorMetrics
Metrics returns a reference to the Doctor's global metrics struct for monitoring operational statistics. All metric fields use atomic operations, allowing safe concurrent reads without additional locking. Use this to expose health check statistics to dashboards, alerts, or external monitoring systems.
func (*Doctor) Remove ¶
Remove unregisters a patient by ID, marking it as removed and preventing future scheduled checks. The method returns true if the patient was found and successfully removed, false otherwise. Removed patients are cleaned from the scheduler queue on the next scheduling cycle.
func (*Doctor) SetDegraded ¶
SetDegraded manually overrides a patient's degradation state, forcing degraded or healthy status. If the patient ID is not found, the method logs a warning and returns without making changes. Use this for operational control, such as forcing maintenance mode or acknowledging known issues.
func (*Doctor) Stop ¶
Stop halts a specific patient by ID, marking it removed and preventing any further health checks. It returns true if the patient was found and stopped, false if the patient did not exist. This method is safe to call multiple times and ensures clean removal from the scheduler.
func (*Doctor) StopAll ¶
StopAll gracefully shuts down the Doctor, stopping all patients and the scheduler goroutine. It uses an atomic flag to ensure idempotency and waits for in-flight operations with the given timeout. After calling StopAll, the Doctor cannot be restarted and all patient registrations are cleared.
type DoctorMetrics ¶
type DoctorMetrics struct {
PatientsTotal atomic.Int64
ChecksTotal atomic.Uint64
ChecksHealthy atomic.Uint64
ChecksDegraded atomic.Uint64
ChecksFailed atomic.Uint64
StateChanges atomic.Uint64
AcceleratedChecks atomic.Uint64
ManualDegraded atomic.Uint64
Recoveries atomic.Uint64
Timeouts atomic.Uint64
PanicsRecovered atomic.Uint64
PoolSubmits atomic.Uint64
PoolSubmitFails atomic.Uint64
}
DoctorMetrics tracks global operational metrics across all patients.
type DoctorOption ¶
type DoctorOption func(*Doctor)
func DoctorWithGlobalTimeout ¶
func DoctorWithGlobalTimeout(t time.Duration) DoctorOption
DoctorWithGlobalTimeout configures the default timeout for patient checks lacking individual settings. If the provided duration is zero or negative, the option is ignored and the default applies. This ensures checks do not hang indefinitely and helps maintain responsive health monitoring.
func DoctorWithLogger ¶
func DoctorWithLogger(l *ll.Logger) DoctorOption
DoctorWithLogger assigns a namespaced logger instance for structured Doctor operation logging. If the provided logger is nil, the option has no effect and the default logger remains active. Use this to integrate Doctor logs with your application's logging infrastructure and levels.
func DoctorWithMaxConcurrent ¶
func DoctorWithMaxConcurrent(n int) DoctorOption
DoctorWithMaxConcurrent sets the maximum number of concurrent health checks the Doctor can run. If the provided value is zero or negative, the option is ignored and the default applies. This limit controls resource usage and prevents system overload during intensive monitoring.
func DoctorWithObservable ¶
func DoctorWithObservable(obs Observable[PatientEvent]) DoctorOption
DoctorWithObservable attaches an event observer to receive patient state change notifications. The observable receives PatientEvent structs whenever a patient's health status updates. Use this for external monitoring, logging, or triggering downstream actions based on health.
func DoctorWithPool ¶
func DoctorWithPool(pool *Pool) DoctorOption
DoctorWithPool configures the Doctor to use a custom worker pool for check execution. If the provided pool is nil, the option has no effect and default pool creation applies. Use this to control concurrency limits and resource sharing across multiple Doctors.
func DoctorWithVerbose ¶
func DoctorWithVerbose(v bool) DoctorOption
DoctorWithVerbose enables or disables detailed debug logging for Doctor operations and checks. When enabled, additional context like durations and errors are logged for troubleshooting. Use this during development or debugging, and disable in production for reduced log volume.
type Event ¶
type Event struct {
Type string // Type of event: "queued", "run", "done"
TaskID string // Optional task identifier
WorkerID string // Identifier of the worker processing the task
Time time.Time // Time of the event
Duration time.Duration // Duration of task execution (for "done" events)
Err error // Error, if any (for "done" events)
}
Event captures details of task execution for observability. Thread-safe for use in notification across goroutines.
type EvictReason ¶
type EvictReason int
EvictReason explains why an entry left an LRU cache.
const ( // EvictLRU means the entry was the least recently used one when the cache // was full. EvictLRU EvictReason = iota // EvictTTL means the entry outlived its TTL and was removed lazily on access. EvictTTL // EvictDeleted means the caller removed the entry with Delete. EvictDeleted // EvictCleared means the cache was emptied with Clear. EvictCleared )
func (EvictReason) String ¶
func (r EvictReason) String() string
String returns a human-readable label for the eviction reason.
type Flight ¶
type Flight struct {
// contains filtered or unexported fields
}
Flight deduplicates concurrent executions by key. The first caller becomes the leader and runs fn directly; waiters block until the leader finishes and receive the same result.
func NewFlight ¶
func NewFlight(opts ...FlightOption) *Flight
NewFlight creates a coalescing flight group.
func (*Flight) CollectMetrics ¶
func (f *Flight) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for Flight.
func (*Flight) Do ¶
func (f *Flight) Do(key string, fn func() (interface{}, error)) (res FlightResult, err error)
Do executes fn once per key. The leader receives Shared=false; concurrent callers receive Shared=true after the leader finishes. If fn panics, the panic is recovered and returned as a *CaughtPanic to every waiter and the leader itself.
func (*Flight) DoCtx ¶
func (f *Flight) DoCtx(ctx context.Context, key string, fn func() (interface{}, error)) (res FlightResult, err error)
DoCtx executes fn once per key. The leader receives Shared=false; concurrent callers receive Shared=true after the leader finishes. If fn panics, the panic is recovered and returned as a *CaughtPanic to every waiter and the leader itself.
func (*Flight) Forget ¶
Forget drops an in-flight key so the next caller starts fresh. Existing waiters continue blocking on the current leader.
func (*Flight) Metrics ¶
func (f *Flight) Metrics() *FlightMetrics
Metrics returns a snapshot of current metrics.
type FlightMetrics ¶
type FlightMetrics struct {
Leaders atomic.Uint64
Waiters atomic.Uint64
Served atomic.Uint64
Canceled atomic.Uint64
InFlight atomic.Int64
}
FlightMetrics holds atomic counters for observability.
type FlightOption ¶
type FlightOption func(*flightOpt)
FlightOption is a functional option for configuring Flight.
func FlightWithObservable ¶
func FlightWithObservable(obs Observable[Event]) FlightOption
FlightWithObservable sets an observable for flight events.
type FlightResult ¶
type FlightResult struct {
Val interface{}
}
FlightResult carries the outcome of a coalesced operation.
type Func ¶
type Func func() error
Func converts a function returning an error into a Task. Example:
pool.Submit(jack.Func(func() error { return nil })) // Submits a simple task
type FuncCtx ¶
FuncCtx converts a context-aware function into a TaskCtx. Example:
pool.SubmitCtx(ctx, jack.FuncCtx(func(ctx context.Context) error { return nil })) // Submits a context-aware task
type Future ¶
type Future[T any] struct { // contains filtered or unexported fields }
func AsyncWithContext ¶
AsyncWithContext runs fn in a new goroutine under the given context.
func NewFuture ¶
NewFuture creates a Future that executes fn asynchronously in a new goroutine. The context governs cancellation; fn receives a child context that is cancelled by Future.Cancel. A nil ctx is treated as context.Background(). If fn is nil, the Future is returned in an already-failed state.
func (*Future[T]) Await ¶
Await blocks until the Future completes and returns its result and any error.
func (*Future[T]) AwaitWithContext ¶
AwaitWithContext blocks until the Future completes or ctx is cancelled.
func (*Future[T]) AwaitWithTimeout ¶
AwaitWithTimeout blocks until the Future completes or timeout elapses. Returns ErrFutureTimeout if the deadline is exceeded.
func (*Future[T]) Cancel ¶
Cancel stops the Future: it cancels the future's context so fn can observe cancellation, and marks the result as discarded so Await returns ErrFutureCanceled. Returns true if this call caused the cancellation. A panicking or blocking fn is still awaited; goroutines cannot be killed.
func (*Future[T]) IsCanceled ¶
IsCanceled reports whether the Future was cancelled before completing.
func (*Future[T]) IsDone ¶
IsDone reports whether the Future has finished, either successfully or with an error.
type Gate ¶
type Gate struct {
// contains filtered or unexported fields
}
Gate is a reusable barrier. Open lets all waiters pass; Close blocks future waiters. Pulse wakes current waiters but remains closed.
func (*Gate) Pulse ¶
func (g *Gate) Pulse()
Pulse wakes all current waiters; the gate remains closed.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group coordinates concurrent execution of functions with error and panic handling. It supports context cancellation and worker limits, collecting errors in a channel. Thread-safe via wait group, mutex, and channel operations.
func NewGroup ¶
func NewGroup() *Group
NewGroup creates a new Group for running functions concurrently. It initializes an error channel with a buffer of 1. Thread-safe via initialization. Example:
group := NewGroup() // Creates a new group
func (*Group) Errors ¶
Errors returns a channel that receives the first error from Go or GoCtx goroutines. The channel has a buffer of 1 and is closed by Wait. Thread-safe via channel operations. Example:
for err := range group.Errors() { ... } // Reads errors
func (*Group) Go ¶
Go runs a function in a new goroutine with error and panic handling. The first non-nil error or panic is sent to the Errors channel. If a context is set, it is cancelled on the first error. Thread-safe via wait group and doWork. Example:
group.Go(func() error { return nil }) // Runs a function
func (*Group) GoCtx ¶
GoCtx runs a context-aware function in a new goroutine. It requires a context set via WithContext and passes it to the function. The first error, panic, or context cancellation is sent to the Errors channel, and the context is cancelled.
The concurrency limit (WithLimit) is applied by doWork exactly once; GoCtx must not acquire it itself, or a limited Group would self-deadlock. Thread-safe via wait group and doWork. Example:
group.WithContext(ctx).GoCtx(func(ctx context.Context) error { return nil }) // Runs a context-aware function
func (*Group) Wait ¶
func (g *Group) Wait()
Wait blocks until all Go and GoCtx goroutines complete. It closes the Errors channel and cancels the Group’s context, if set. Errors produced by goroutines started after Wait returns are discarded rather than panicking, but a Group is not meant to be reused after Wait. Thread-safe via wait group and once. Example:
group.Wait() // Waits for all goroutines
func (*Group) WithContext ¶
WithContext associates a context with the Group, enabling cancellation. It cancels any previous context and creates a new cancellable context. Thread-safe via context operations. Example:
group.WithContext(ctx) // Sets group context
type HashRing ¶
type HashRing struct {
// contains filtered or unexported fields
}
HashRing is a consistent hash ring with virtual nodes.
Keys map to physical nodes with minimal disruption: adding or removing a node remaps only the keys that node owns (plus a small share of virtual-node boundaries), instead of reshuffling the whole keyspace the way a modulo hash does. That makes it suitable for sharding caches, queues and sticky routing where rebalancing everything at once is unacceptable.
Reads are lock-free: membership changes build a new immutable snapshot and publish it atomically, so Get/GetN keep serving the previous snapshot until the swap. Writes (Add/Remove) are serialized by a mutex.
It is safe for concurrent use by any number of goroutines.
func NewHashRing ¶
func NewHashRing(opts ...RingOption) *HashRing
NewHashRing creates an empty ring. Nodes are added with Add.
ring := jack.NewHashRing(jack.RingWithReplicas(160))
ring.Add("cache-a", "cache-b", "cache-c")
node, ok := ring.Get("user:42")
func (*HashRing) Add ¶
Add inserts nodes into the ring and returns how many were newly added. Empty names and duplicates are ignored. Cost is O(nodes × replicas × log) because the ring is rebuilt; add nodes in batches rather than one by one.
func (*HashRing) CollectMetrics ¶
func (r *HashRing) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for HashRing.
func (*HashRing) Get ¶
Get returns the node that owns key. The second result is false when the ring has no nodes.
func (*HashRing) GetN ¶
GetN returns up to n distinct nodes in clockwise ring order starting at key. It is the building block for replica reads/writes: the first element is the same node Get returns, the rest are the preferred fallbacks.
func (*HashRing) Remove ¶
Remove deletes nodes from the ring and returns how many were present. Unknown names and empty strings are ignored. Removing every node leaves an empty ring whose Get reports false.
func (*HashRing) Replicas ¶
Replicas returns the number of virtual nodes generated per physical node.
func (*HashRing) VirtualNodes ¶
VirtualNodes returns the number of distinct points on the ring. It is lower than Len()×Replicas only when two virtual nodes collide on the same hash.
type HedgeGroup ¶
type HedgeGroup struct {
// contains filtered or unexported fields
}
HedgeGroup runs multiple hedge strategies concurrently and returns the first successful result. Useful when hedging across different endpoints or regions.
func NewHedgeGroup ¶
func NewHedgeGroup(hedgers ...*Hedger) *HedgeGroup
NewHedgeGroup creates a group of independent hedgers.
type HedgeMetrics ¶
type HedgeMetrics struct {
Requests atomic.Uint64 // total primary calls
Hedged atomic.Uint64 // times a second request was fired
PrimaryWon atomic.Uint64 // primary response arrived first
HedgeWon atomic.Uint64 // hedge response arrived first
Errors atomic.Uint64 // all attempts failed
AvgDelayNs atomic.Int64 // EWMA of hedge fire delay in nanoseconds
}
HedgeMetrics tracks hedged request operational statistics.
type HedgeOption ¶
type HedgeOption func(*Hedger)
HedgeOption configures a Hedger.
func HedgeWithDelay ¶
func HedgeWithDelay(d time.Duration) HedgeOption
HedgeWithDelay sets a fixed delay before the hedge fires (default: use latency percentile). When set, the latency window is not consulted.
func HedgeWithMaxConcurrent ¶
func HedgeWithMaxConcurrent(n int) HedgeOption
HedgeWithMaxConcurrent sets the maximum number of in-flight hedged pairs (default 100). When the limit is reached, no hedge is fired and only the primary runs.
func HedgeWithMinSamples ¶
func HedgeWithMinSamples(n int) HedgeOption
HedgeWithMinSamples sets how many RTT samples must be collected before the latency-based hedge delay activates (default 10). Before that, a zero delay (fire immediately) is used.
func HedgeWithPercentile ¶
func HedgeWithPercentile(p int) HedgeOption
HedgeWithPercentile sets which RTT percentile to use as the hedge delay (default 50). Valid range: 1–99.
type Hedger ¶
type Hedger struct {
// contains filtered or unexported fields
}
Hedger implements the hedged requests pattern.
A primary request is sent immediately. If it has not responded within the hedge delay (derived from recent latency or a fixed duration), an identical second request is fired. Whichever responds first is returned; the other is cancelled via context cancellation.
This reduces tail latency at the cost of occasionally sending duplicate requests. It is safe only for idempotent operations (reads, retried writes with idempotency keys).
The hedge delay adapts automatically: it is the Nth percentile of recent call RTTs measured in a lock-free circular sample buffer. No third-party sketch library is used.
func NewHedger ¶
func NewHedger(opts ...HedgeOption) *Hedger
NewHedger creates a Hedger with sensible defaults.
func (*Hedger) Do ¶
Do executes fn with hedging. fn receives a context that is cancelled when the other attempt wins. The caller's ctx governs the outer deadline.
fn must be idempotent — it may be called twice concurrently.
func (*Hedger) Metrics ¶
func (h *Hedger) Metrics() *HedgeMetrics
Metrics returns the hedger's operational metrics.
type HedgerOf ¶
type HedgerOf[T any] struct { // contains filtered or unexported fields }
HedgerOf wraps Hedger for typed results, avoiding any cast at the call site.
func NewHedgerOf ¶
func NewHedgerOf[T any](opts ...HedgeOption) *HedgerOf[T]
NewHedgerOf creates a typed hedger.
func (*HedgerOf[T]) Metrics ¶
func (h *HedgerOf[T]) Metrics() *HedgeMetrics
Metrics returns the underlying hedger's metrics.
type Hook ¶
Hook defines a lifecycle hook function that returns an error. It accepts a unique identifier string for the operation being tracked. Use this type for pre-execution validation or setup logic that may fail.
type HookCtx ¶
HookCtx defines a context-aware lifecycle hook function that returns an error. It accepts a context for cancellation and an identifier for the tracked operation. Use this type when hooks need to respect timeouts or external cancellation signals.
type Identifiable ¶
type Identifiable interface {
// ID returns a unique identifier for the task.
ID() string
}
Identifiable is an optional interface for tasks to provide a custom ID for logging, metrics, and tracing.
type KeyedLocker ¶
type KeyedLocker struct {
// contains filtered or unexported fields
}
KeyedLocker provides mutual exclusion per key using a fixed set of stripes.
It solves the "mutex per key" problem (per-user serialisation, cache-stampede control, per-account ordering) without an ever-growing map of mutexes: keys are hashed onto N stripes, so memory is O(stripes) regardless of how many distinct keys are seen. Two keys that hash to the same stripe are serialised unnecessarily — size the stripe count for the concurrency you expect.
Unlike sync.Mutex, acquisition can wait on a context, which makes it usable on request paths:
var locks = jack.NewKeyedLocker()
if err := locks.DoCtx(ctx, "account:"+id, func() error {
return chargeAccount(id)
}); err != nil {
return err
}
The zero value is ready to use and behaves like NewKeyedLocker().
func NewKeyedLocker ¶
func NewKeyedLocker(opts ...KeyedLockerOption) *KeyedLocker
NewKeyedLocker creates a keyed locker with defaultKeyedStripes stripes.
func (*KeyedLocker) CollectMetrics ¶
func (l *KeyedLocker) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for KeyedLocker.
func (*KeyedLocker) Do ¶
func (l *KeyedLocker) Do(key string, fn func())
Do runs fn while holding the stripe for key. Panics propagate after the lock is released, matching Safely.Do.
func (*KeyedLocker) DoCtx ¶
DoCtx acquires the stripe for key (respecting ctx) and runs fn. When ctx is already done, fn is not called and ctx.Err() is returned.
func (*KeyedLocker) DoErr ¶
func (l *KeyedLocker) DoErr(key string, fn func() error) error
DoErr runs fn while holding the stripe for key and returns its error.
func (*KeyedLocker) Lock ¶
func (l *KeyedLocker) Lock(key string)
Lock acquires the stripe for key, blocking until it is free.
func (*KeyedLocker) LockCtx ¶
func (l *KeyedLocker) LockCtx(ctx context.Context, key string) error
LockCtx acquires the stripe for key, giving up when ctx is done. It returns ctx.Err() if the lock could not be acquired; a nil ctx is treated as context.Background(). An already-cancelled context fails immediately, even if the stripe happens to be free.
func (*KeyedLocker) Metrics ¶
func (l *KeyedLocker) Metrics() *KeyedLockerMetrics
Metrics returns a snapshot of the locker's counters.
func (*KeyedLocker) Stripes ¶
func (l *KeyedLocker) Stripes() int
Stripes returns the number of lock stripes (always a power of two).
func (*KeyedLocker) TryLock ¶
func (l *KeyedLocker) TryLock(key string) bool
TryLock acquires the stripe for key without blocking. It reports whether the lock was taken; on success the caller must call Unlock.
func (*KeyedLocker) Unlock ¶
func (l *KeyedLocker) Unlock(key string)
Unlock releases the stripe for key. Unlocking a key whose stripe is already free is a no-op and is counted in Metrics().UnbalancedUnlocks; it never blocks, so a double Unlock cannot wedge the caller (unlike a sync.Mutex, which panics).
type KeyedLockerMetrics ¶
type KeyedLockerMetrics struct {
Locks atomic.Uint64 // successful acquisitions
Unlocks atomic.Uint64 // releases
Contended atomic.Uint64 // acquisitions that had to wait
Rejected atomic.Uint64 // TryLock failures or cancelled waits
UnbalancedUnlocks atomic.Uint64 // unlocks of an already-free stripe
}
KeyedLockerMetrics is a snapshot of a KeyedLocker's counters.
Unlike the other *Metrics types in this package, the counters live per lock stripe (so keys that hash to different stripes never share a cache line) and are summed when Metrics is called. The returned values describe the moment of the call; they do not update afterwards.
type KeyedLockerOption ¶
type KeyedLockerOption func(*keyedLockerConfig)
KeyedLockerOption configures a KeyedLocker.
func KeyedLockerWithStripes ¶
func KeyedLockerWithStripes(n int) KeyedLockerOption
KeyedLockerWithStripes sets the number of lock stripes (rounded up to the next power of two). More stripes reduce false sharing between unrelated keys at the cost of memory: each stripe is a channel plus its own cache-line of counters, so budget roughly 100 bytes per stripe. Non-positive values are ignored.
type LRU ¶
type LRU[K comparable, V any] struct { // contains filtered or unexported fields }
LRU is a bounded, thread-safe least-recently-used cache with optional TTL and eviction callbacks.
Capacity is a hard bound: when a new key would exceed it, the least recently used entry is evicted. Unlike a TTL-only store (see Reaper), memory is bounded even if keys are never touched again, which makes it suitable for hot-path caches in long-running services.
Reads mutate the recency list, so they serialise on a single mutex: this is a small, predictable cache, not a sharded one. If profiling shows lock contention in your workload, partition the keyspace across several LRUs (or use KeyedLocker to serialise per key) instead of expecting one instance to scale linearly with cores.
var cache = jack.NewLRU[string, *User](10_000,
jack.LRUWithTTL[string, *User](5*time.Minute),
jack.LRUWithOnEvict[string, *User](func(k string, v *User, why jack.EvictReason) {
log.Debug("evict", k, why)
}),
)
v, ok := cache.Get("user:42")
func NewLRU ¶
func NewLRU[K comparable, V any](capacity int, opts ...LRUOption[K, V]) *LRU[K, V]
NewLRU creates a cache holding at most capacity entries. A non-positive capacity is treated as 1.
func (*LRU[K, V]) CollectMetrics ¶
func (c *LRU[K, V]) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for LRU.
func (*LRU[K, V]) GetOrLoad ¶
func (c *LRU[K, V]) GetOrLoad(ctx context.Context, key K, load func(context.Context) (V, error)) (V, bool, error)
GetOrLoad returns the cached value for key, or calls load exactly once for concurrent callers and caches the successful result.
The second result reports whether the value came from the cache. Waiters share the loader's outcome: if it fails, every waiter receives the same error and nothing is cached. The loader runs without the cache lock held, so it may perform slow I/O; each waiter's ctx only bounds its own wait.
v, hit, err := cache.GetOrLoad(ctx, "user:42", func(ctx context.Context) (*User, error) {
return repo.Load(ctx, 42)
})
func (*LRU[K, V]) Keys ¶
func (c *LRU[K, V]) Keys() []K
Keys returns the cached keys ordered from most to least recently used.
func (*LRU[K, V]) Len ¶
Len returns the number of entries currently stored, including any entries whose TTL has elapsed but which have not been read since.
func (*LRU[K, V]) Metrics ¶
func (c *LRU[K, V]) Metrics() *LRUMetrics
Metrics returns the cache's activity counters.
func (*LRU[K, V]) Peek ¶
Peek returns the value for key without changing its recency or residency. Expired entries are reported as missing (and removed).
type LRUMetrics ¶
type LRUMetrics struct {
Hits atomic.Uint64
Misses atomic.Uint64
Inserts atomic.Uint64
Evictions atomic.Uint64 // capacity-driven removals
Expirations atomic.Uint64 // TTL-driven removals
Deletes atomic.Uint64 // explicit removals
Loads atomic.Uint64 // GetOrLoad calls that executed the loader
LoadErrors atomic.Uint64 // loader failures
LoadCoalesced atomic.Uint64 // waiters that joined an in-flight load
}
LRUMetrics tracks cache activity.
type LRUOption ¶
type LRUOption[K comparable, V any] func(*lruConfig[K, V])
LRUOption configures an LRU cache.
func LRUWithOnEvict ¶
func LRUWithOnEvict[K comparable, V any](fn func(key K, val V, reason EvictReason)) LRUOption[K, V]
LRUWithOnEvict registers a callback invoked whenever an entry leaves the cache (capacity, TTL, Delete or Clear). The callback runs after the cache lock is released, so it may safely call back into the cache.
func LRUWithTTL ¶
func LRUWithTTL[K comparable, V any](ttl time.Duration) LRUOption[K, V]
LRUWithTTL expires entries after ttl. Expiry is lazy: an expired entry is removed the next time it is read, or when the cache is otherwise touched. Len() and Keys() may therefore report entries that are already stale; use Peek/Get when freshness matters. Non-positive values disable TTL.
type Latch ¶
type Latch struct {
// contains filtered or unexported fields
}
Latch is a one-shot signal. It starts closed; once Open() is called, it remains open forever. Safe for concurrent use.
type Lease ¶
type Lease struct {
// contains filtered or unexported fields
}
Lease represents a time-bounded slot acquired from a Semaphore. If the holder fails to call Release before the deadline, the Reaper automatically reclaims the slot so it is never permanently lost.
Obtain a Lease via Leaser.Acquire, not by constructing directly.
type LeaseMetrics ¶
type LeaseMetrics struct {
Acquired atomic.Uint64 // leases successfully granted
Released atomic.Uint64 // leases explicitly released
Expired atomic.Uint64 // leases reclaimed by the reaper
Active atomic.Int64 // leases currently held
}
LeaseMetrics tracks lease operational statistics.
type Leaser ¶
type Leaser struct {
// contains filtered or unexported fields
}
Leaser wraps a Semaphore and a Reaper to provide leases: acquired semaphore slots with automatic reclamation on expiry.
This solves the problem where a crashed or slow holder never calls Release, permanently consuming a concurrency slot. The Reaper fires after TTL and calls Release on the holder's behalf.
func NewLeaser ¶
func NewLeaser(sem *Semaphore, opts ...LeaserOption) *Leaser
NewLeaser creates a Leaser backed by the given Semaphore. The caller owns the Semaphore and Reaper lifecycles; call Close on both when done.
func (*Leaser) Acquire ¶
func (lm *Leaser) Acquire(ctx context.Context, id string, p Priority, ttl time.Duration) (*Lease, error)
Acquire waits for a semaphore slot and returns a Lease valid for ttl. If ttl is zero the manager's default TTL is used. The Lease must be Released when the work is done; if not, the Reaper reclaims it automatically after ttl.
func (*Leaser) Close ¶
func (lm *Leaser) Close()
Close stops the internal Reaper. The backing Semaphore is not closed.
func (*Leaser) CollectMetrics ¶
func (lm *Leaser) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for Leaser.
func (*Leaser) Metrics ¶
func (lm *Leaser) Metrics() *LeaseMetrics
Metrics returns the manager's operational metrics.
type LeaserOption ¶
type LeaserOption func(*Leaser)
LeaserOption configures a Leaser.
func LeaserWithTTL ¶
func LeaserWithTTL(d time.Duration) LeaserOption
LeaserWithTTL sets the default lease lifetime (default 30s).
type Lifetime ¶
type Lifetime struct {
// contains filtered or unexported fields
}
func NewLifetime ¶
func NewLifetime(opts ...LifetimeOption) *Lifetime
NewLifetime creates and initializes a new Lifetime manager with the provided configuration options. It spawns one pruning goroutine per shard to handle timer expiration with minimal lock contention. The returned Lifetime is immediately active and ready to schedule timed callbacks via ScheduleTimed.
func (*Lifetime) CancelTimed ¶
CancelTimed removes a scheduled timer by ID, preventing its callback from executing. It returns true if the timer was found and successfully cancelled, false if none existed. This method is safe to call multiple times and has no effect if the timer already expired.
func (*Lifetime) CollectMetrics ¶
func (lm *Lifetime) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for Lifetime.
func (*Lifetime) ExecuteCtxWithLifetime ¶
func (lm *Lifetime) ExecuteCtxWithLifetime(ctx context.Context, id string, lifetime *Vitals, operation FuncCtx) error
ExecuteCtxWithLifetime runs a context-aware operation with Vitals hooks and schedules timed callback on success. If the operation succeeds and a timed callback is configured, it is scheduled for later execution. This method combines immediate context-aware execution with deferred lifecycle callback management.
func (*Lifetime) ExecuteWithLifetime ¶
func (lm *Lifetime) ExecuteWithLifetime(ctx context.Context, id string, lifetime *Vitals, operation Func) error
ExecuteWithLifetime runs an operation with Vitals hooks and schedules a timed callback on success. If the operation succeeds and a timed callback is configured, it is scheduled for later execution. This method combines immediate operation execution with deferred lifecycle callback management.
func (*Lifetime) GetRemainingDuration ¶
GetRemainingDuration returns the time remaining until a scheduled timer expires for the given ID. It returns the duration and true if the timer exists and has not yet expired, otherwise zero and false. This method is useful for displaying countdowns or making scheduling decisions based on timer state.
func (*Lifetime) HasPending ¶
HasPending checks whether a timer is currently scheduled for the given ID. It returns true if the ID exists in any shard's queue, false otherwise. This method provides thread-safe read access without modifying internal state.
func (*Lifetime) Metrics ¶
func (lm *Lifetime) Metrics() *LifetimeMetrics
Metrics returns a reference to the Lifetime manager's metrics struct for monitoring operational statistics. All metric fields use atomic operations, allowing safe concurrent reads without additional locking. Use this to expose timer scheduling statistics to dashboards, alerts, or external monitoring systems.
func (*Lifetime) PendingCount ¶
PendingCount returns the total number of timers currently scheduled across all shards. It iterates through each shard while holding locks to ensure an accurate snapshot count. Use this for monitoring queue depth or implementing backpressure in high-load scenarios.
func (*Lifetime) ResetTimed ¶
ResetTimed extends the expiration time of an existing timer by its original duration from now. It returns true if the timer was found and successfully reset, false if no timer existed for the ID. This method is useful for implementing keep-alive or activity-based timeout extension patterns.
func (*Lifetime) Schedule ¶
Schedule schedules a FuncCtx operation to execute after the specified wait duration for a given ID. This is a convenience wrapper around ScheduleTimed that accepts jack.FuncCtx type callbacks. If a timer already exists for the ID, it is replaced with the new callback and expiration time.
func (*Lifetime) ScheduleLifetimeTimed ¶
ScheduleLifetimeTimed schedules a timed callback using configuration from a Vitals instance. If the Vitals or its Timed callback is nil, it resets any existing timer for the ID instead. This convenience method integrates Lifetime timer management with Vitals lifecycle configuration.
func (*Lifetime) ScheduleTimed ¶
func (lm *Lifetime) ScheduleTimed(ctx context.Context, id string, callback CallbackCtx, wait time.Duration)
ScheduleTimed registers a callback to execute after the specified wait duration for a given ID. If a timer already exists for the ID, it is replaced with the new callback and expiration time. The method uses sharding for concurrency and signals the prune loop to re-evaluate scheduling.
func (*Lifetime) Stop ¶
Stop cancels timers for the specified IDs, or all timers if no IDs are provided. It groups IDs by shard for efficient batch removal and signals prune loops to re-evaluate. This method provides a convenient way to clean up multiple timers without individual CancelTimed calls.
func (*Lifetime) StopAll ¶
func (lm *Lifetime) StopAll()
StopAll gracefully shuts down the Lifetime manager, cancelling all pending timers and goroutines. It signals the context to stop prune loops, waits for them to exit, then clears all shard state. After calling StopAll, the Lifetime instance cannot be reused and all scheduled callbacks are discarded.
type LifetimeMetrics ¶
type LifetimeMetrics struct {
ScheduleCalls atomic.Uint64
ResetCalls atomic.Uint64
CancelCalls atomic.Uint64
ExecuteCalls atomic.Uint64
ExpiredTotal atomic.Uint64
ExpiredHandled atomic.Uint64
ExpiredMissed atomic.Uint64
ExpiredErrors atomic.Uint64
ActiveTimers atomic.Int64
MaxActiveTimers atomic.Int64
TotalTimersSeen atomic.Uint64
AvgExpirationMs atomic.Int64
MinExpirationMs atomic.Int64
MaxExpirationMs atomic.Int64
LoopIterations atomic.Uint64
SignalsSent atomic.Uint64
StopsReceived atomic.Uint64
}
LifetimeMetrics tracks operational metrics for the lifetime manager.
type LifetimeOption ¶
type LifetimeOption func(*Lifetime)
func LifetimeWithLogger ¶
func LifetimeWithLogger(l *ll.Logger) LifetimeOption
LifetimeWithLogger assigns a namespaced logger instance for structured Lifetime operation logging. If the provided logger is nil, the option has no effect and the default logger remains active. Use this to integrate Lifetime logs with your application's logging infrastructure and levels.
func LifetimeWithMinTick ¶
func LifetimeWithMinTick(tick time.Duration) LifetimeOption
LifetimeWithMinTick sets the minimum polling interval for the expiration check loop. If the provided duration is zero or negative, the option is ignored and default applies. Use this to balance responsiveness against CPU usage when processing expired timers.
func LifetimeWithShards ¶
func LifetimeWithShards(count uint32) LifetimeOption
LifetimeWithShards configures the number of shards for concurrent timer management. The count must be a power of two for efficient bitwise modulo hashing distribution. Use higher shard counts to reduce lock contention under heavy timer scheduling load.
func LifetimeWithTimerLimit ¶
func LifetimeWithTimerLimit(limit time.Duration) LifetimeOption
LifetimeWithTimerLimit configures the maximum execution time allowed for timer callbacks. If the provided duration is zero or negative, the option is ignored and default applies. This prevents long-running callbacks from blocking the expiration processing loop.
type LoopConfig ¶
type Looper ¶
type Looper struct {
// contains filtered or unexported fields
}
func NewLooper ¶
func NewLooper(task Func, opts ...LooperOption) *Looper
NewLooper creates a new Looper with full metrics.
func (*Looper) CurrentInterval ¶
CurrentInterval returns the interval currently in use, including any backoff applied.
func (*Looper) FailureCount ¶
FailureCount returns the number of consecutive errors since the last success.
func (*Looper) Metrics ¶
func (l *Looper) Metrics() *LooperMetrics
Metrics returns the looper's metrics.
func (*Looper) ResetInterval ¶
func (l *Looper) ResetInterval()
ResetInterval restores the interval to the value set at construction time.
func (*Looper) SetInterval ¶
SetInterval updates the iteration interval dynamically; takes effect on the next tick.
type LooperMetrics ¶
type LooperMetrics struct {
Executions atomic.Uint64
Failures atomic.Uint64
Successes atomic.Uint64
BackoffEvents atomic.Uint64
IntervalChanges atomic.Uint64
PanicsRecovered atomic.Uint64
ContextCancels atomic.Uint64
Timeouts atomic.Uint64
LastRun atomic.Value
LastError atomic.Value
LastStart atomic.Int64
LastEnd atomic.Int64
TotalDurationNs atomic.Int64
MinDurationNs atomic.Int64
MaxDurationNs atomic.Int64
CurrentIntervalNs atomic.Int64
CurrentBackoffNs atomic.Int64
}
LooperMetrics tracks comprehensive operational metrics.
type LooperOption ¶
type LooperOption func(*LoopConfig)
func WithLooperBackoff ¶
func WithLooperBackoff(enabled bool) LooperOption
WithLooperBackoff enables exponential backoff on consecutive errors.
func WithLooperContext ¶
func WithLooperContext(ctx context.Context) LooperOption
WithLooperContext attaches an external context; cancelling it stops the looper.
func WithLooperImmediate ¶
func WithLooperImmediate(immediate bool) LooperOption
WithLooperImmediate makes the looper execute fn once before the first timer fires.
func WithLooperInterval ¶
func WithLooperInterval(interval time.Duration) LooperOption
WithLooperInterval sets the base interval between loop iterations.
func WithLooperJitter ¶
func WithLooperJitter(jitter float64) LooperOption
WithLooperJitter adds proportional random jitter to each interval to spread load.
func WithLooperLogger ¶
func WithLooperLogger(logger *ll.Logger) LooperOption
WithLooperLogger sets a custom logger for the looper.
func WithLooperMaxInterval ¶
func WithLooperMaxInterval(max time.Duration) LooperOption
WithLooperMaxInterval caps the backoff interval ceiling.
func WithLooperMinInterval ¶
func WithLooperMinInterval(min time.Duration) LooperOption
WithLooperMinInterval sets the floor below which the interval will not drop.
func WithLooperName ¶
func WithLooperName(name string) LooperOption
WithLooperName sets the display name used in logs and metrics.
type MetricSample ¶
type MetricSample struct {
// Name is the metric name (e.g. "tasks_completed"). WritePrometheus
// sanitises it to [a-zA-Z0-9_:].
Name string
// Help is the optional help text emitted as "# HELP".
Help string
// Labels are optional key/value pairs emitted as {k="v"} in sorted order.
Labels map[string]string
// Type selects the "# TYPE" line. The zero value is MetricGauge; the
// built-in collectors mark their cumulative totals as MetricCounter.
Type MetricType
// Value is the current value. Samples carry signed 64-bit values, so a
// uint64 counter is exposed as int64 and would only become negative after
// more than 2^63 events.
Value int64
}
MetricSample is a single numeric observation produced by a component.
type MetricSink ¶
type MetricSink func(MetricSample)
MetricSink receives samples during a collection pass.
type MetricType ¶
type MetricType int
MetricType is the Prometheus type of a sample. It controls the "# TYPE" line so scrapers can apply the right rate/reset semantics: counters are treated as monotonically increasing and reset on restart, gauges are not.
const ( // MetricGauge is the zero value: a value that can go up and down. MetricGauge MetricType = iota // MetricCounter is a value that only increases (until the process restarts). MetricCounter )
func (MetricType) String ¶
func (t MetricType) String() string
String returns the Prometheus type name.
type MetricsCollector ¶
type MetricsCollector interface {
CollectMetrics(sink MetricSink)
}
MetricsCollector exposes component metrics to an external monitoring system. Implementations must be safe for concurrent use and must not block.
func MergeCollectors ¶
func MergeCollectors(collectors ...MetricsCollector) MetricsCollector
MergeCollectors combines collectors into one; use it to build a per-service registry.
func WithName ¶
func WithName(name string, c MetricsCollector) MetricsCollector
WithName attaches a name label to a collector so several instances of the same component type can be exposed side by side:
jack.WritePrometheus(w,
jack.WithName("api", apiPool),
jack.WithName("batch", batchPool),
)
The name is applied as the "name" label rather than as a prefix, so metric names stay stable: pool_tasks_completed{name="api"}.
type MetricsFunc ¶
type MetricsFunc func(MetricSink)
MetricsFunc adapts a plain function to MetricsCollector.
func (MetricsFunc) CollectMetrics ¶
func (f MetricsFunc) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector.
type Observable ¶
type Observable[T any] interface { // Add registers one or more observers to receive notifications. Add(observers ...Observer[T]) // Remove unregisters one or more observers. Remove(observers ...Observer[T]) // Notify sends one or more events to all registered observers. Notify(events ...T) // Shutdown stops the observable and waits for all notifications to complete. Shutdown() }
Observable defines the interface for objects that can be observed by multiple Observer instances. It supports adding and removing observers, notifying them of one or more events, and shutting down cleanly. The type parameter T matches the event type for observers. Example:
obs := NewObservable[string]()
logObserver := &LogObserver{}
obs.Add(logObserver)
obs.Notify("Task completed")
obs.Shutdown()
func NewObservable ¶
func NewObservable[T any](numNotifyWorkers ...int) Observable[T]
NewObservable creates and returns a new Observable instance. It optionally accepts the number of worker goroutines for asynchronous notifications (defaults to 5 if not provided or invalid). Workers process notifications in parallel to avoid blocking the notifier, with panic recovery for observer errors. Parameters: numNotifyWorkers: Optional number of worker goroutines (positive integer). If not provided or invalid, defaults to 5.
Returns: Observable[T]: A new Observable instance ready to accept observers and events.
Example:
// Create an observable with 2 workers
obs := NewObservable[string](2)
logObserver := &LogObserver{}
obs.Add(logObserver)
obs.Notify("Event 1") // Sends "Event 1" to logObserver asynchronously
type Observer ¶
type Observer[T any] interface { OnNotify(value T) // Called when an event of type T is received }
Observer defines the interface for objects that wish to receive notifications from an Observable. The type parameter T specifies the type of events or values that will be notified. Implementors must provide an OnNotify method to handle incoming events. Example:
type LogObserver struct {}
func (l *LogObserver) OnNotify(event string) {
fmt.Printf("Received event: %s\n", event)
}
type OnceGroup ¶
type OnceGroup[K comparable, V any] struct { // contains filtered or unexported fields }
OnceGroup coalesces in-flight duplicate requests for the same key. The first call for a key executes fn; subsequent calls wait and receive the same result. Panics are propagated to all waiters.
func (*OnceGroup[K, V]) Do ¶
Do executes fn for key if no in-flight call exists. Otherwise, it waits for the existing call to complete and shares its result. The returned bool indicates whether the result was shared. Context cancellation affects only the waiter; the underlying execution continues.
func (*OnceGroup[K, V]) Metrics ¶
func (g *OnceGroup[K, V]) Metrics() OnceGroupMetrics
Metrics returns coalescing statistics.
type OnceGroupMetrics ¶
OnceGroupMetrics holds observable counters.
type PLocal ¶
type PLocal[T any] struct { // contains filtered or unexported fields }
PLocal provides goroutine-sharded storage to reduce lock contention. Each goroutine accesses an independent shard with stable affinity.
Shard affinity is derived by parsing the current goroutine ID from runtime.Stack, which costs a few microseconds per call. Use it for low-frequency, large-payload state (buffers, batches, caches), not as a drop-in for hot counters — PLocalCounter is the cheap option for those.
func (*PLocal[T]) Fold ¶
func (p *PLocal[T]) Fold(zeroValue T, fn func(acc, val T) T) T
Fold aggregates values across all shards using the provided function. The accumulator is initialized to zeroValue and fn is called for each shard. Useful for summing counters or collecting metrics from all goroutines.
type PLocalCounter ¶
type PLocalCounter struct {
// contains filtered or unexported fields
}
PLocalCounter is a high-throughput counter sharded across multiple independent slots to eliminate cache-line contention.
func (*PLocalCounter) Value ¶
func (c *PLocalCounter) Value() int64
Value returns the current sum across all shards.
type Patient ¶
type Patient struct {
// contains filtered or unexported fields
}
Patient holds runtime state for one monitored entity with its own metrics.
func NewPatient ¶
func NewPatient(cfg PatientConfig) *Patient
NewPatient creates a Patient with its own metrics instance.
func (*Patient) Metrics ¶
func (p *Patient) Metrics() *PatientMetrics
Metrics returns the patient's individual metrics.
type PatientConfig ¶
type PatientConfig struct {
ID string
Interval time.Duration
Jitter float64
Timeout time.Duration
Accelerated time.Duration
MaxFailures uint64
Check FuncCtx
OnStart FuncCtx
OnComplete FuncCtx
OnError FuncCtx
OnTimeout FuncCtx
OnRecover FuncCtx
OnRemove func()
OnStateChange func(PatientEvent)
}
PatientConfig defines monitoring parameters for a single patient.
type PatientEvent ¶
type PatientEvent struct {
ID string
State PatientState
LastCheck time.Time
Duration time.Duration
Error error
Meta map[string]any
}
PatientEvent is emitted for observability when a patient's state changes.
type PatientMetrics ¶
type PatientMetrics struct {
ChecksTotal atomic.Uint64
ChecksHealthy atomic.Uint64
ChecksDegraded atomic.Uint64
ChecksFailed atomic.Uint64
StateChanges atomic.Uint64
Recoveries atomic.Uint64
Timeouts atomic.Uint64
PanicsRecovered atomic.Uint64
ConsecFailures atomic.Int64
ConsecSuccesses atomic.Int64
LastCheckMs atomic.Int64
AvgCheckMs atomic.Int64
LastFailureMs atomic.Int64
LastRecoveryMs atomic.Int64
}
PatientMetrics tracks per-patient operational metrics.
type PatientState ¶
type PatientState int
PatientState represents the health state of a monitored patient.
const ( PatientUnknown PatientState = iota PatientHealthy PatientDegraded PatientFailed )
type Pipeline ¶
type Pipeline struct {
// contains filtered or unexported fields
}
Pipeline composes stages into a single reusable call path, so admission control, resilience and retry policy can be declared once and shared:
var pipe = jack.NewPipeline(
jack.BreakerStage(breaker),
jack.BulkheadStage(bulkhead, "payments", jack.PriorityHigh),
jack.ThrottleStage(throttle, jack.PriorityHigh),
jack.RateLimiterStage(limiter, jack.PriorityHigh),
jack.RetryStage(retry),
)
err := pipe.Do(ctx, func(ctx context.Context) error {
return client.Charge(ctx, req)
})
Stages run in the order given, outermost first. A Pipeline is immutable and safe for concurrent use; Append returns a new pipeline.
func NewPipeline ¶
NewPipeline builds a pipeline from stages. Nil stages are ignored.
func (*Pipeline) Append ¶
Append returns a new Pipeline with more stages appended; the receiver is unchanged, so pipelines can be shared safely.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool manages a fixed number of worker goroutines to execute tasks concurrently. It supports task submission with or without context, shutdown with timeout, and observability.
func NewPool ¶
NewPool creates a new pool with the specified number of workers and optional configurations. Workers start immediately. At least one worker is always created.
func (*Pool) CollectMetrics ¶
func (p *Pool) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for Pool.
func (*Pool) Do ¶
func (p *Pool) Do(fn func())
Do submits a void function as a task, discarding any submission error.
func (*Pool) DoCtx ¶
DoCtx submits a context-aware void function as a task, discarding any submission error.
func (*Pool) Metrics ¶
func (p *Pool) Metrics() *PoolMetrics
Metrics returns the pool's operational metrics.
func (*Pool) Shutdown ¶
Shutdown gracefully stops the pool and waits for all workers to finish. Returns ErrShutdownTimedOut if workers do not exit within the timeout.
func (*Pool) Submit ¶
Submit enqueues one or more tasks for execution without context. Returns ErrPoolClosed if the pool is shut down, ErrQueueFull if the queue is at capacity.
type PoolMetrics ¶
type PoolMetrics struct {
TasksSubmitted atomic.Uint64 // total tasks accepted into the queue
TasksCompleted atomic.Uint64 // tasks that finished without error
TasksFailed atomic.Uint64 // tasks that returned an error or panicked
TasksRejected atomic.Uint64 // tasks dropped due to full queue or closed pool
PanicsRecovered atomic.Uint64 // panics caught inside task execution
QueueDepth atomic.Int64 // current number of tasks waiting in the channel
MaxQueueDepth atomic.Int64 // high-water mark of QueueDepth
TotalDurationNs atomic.Int64 // cumulative nanoseconds spent executing tasks
ActiveWorkers atomic.Int64 // workers currently executing a task
}
PoolMetrics tracks operational statistics for a Pool. All fields use atomic operations and are safe for concurrent reads without locking.
type Pooling ¶
type Pooling func(*poolingOpt)
Pooling is a functional option type for configuring the pool during creation.
func PoolingWithIDGenerator ¶
PoolingWithIDGenerator sets a custom task ID generator function.
func PoolingWithNoID ¶
func PoolingWithNoID() Pooling
PoolingWithNoID disables task ID generation entirely. This eliminates all allocations in the Submit hot path when observability (logging, event emission) is not needed. The worker will log an empty task ID. Use when pool submission is on a latency-critical path and task tracing is not required.
func PoolingWithObservable ¶
func PoolingWithObservable(obs Observable[Event]) Pooling
PoolingWithObservable sets an observable for event notifications in the pool. The observable receives "queued", "run", and "done" events for every task.
func PoolingWithQueueSize ¶
PoolingWithQueueSize sets the task queue buffer size. Defaults to 2× the worker count when not provided or negative.
type Priority ¶
type Priority int
Priority defines request importance for backpressure decisions. Lower numeric values indicate higher priority (Critical=0 is served first).
type Queue ¶
type Queue struct {
// contains filtered or unexported fields
}
Queue is a bounded, multi-priority, multi-consumer work queue. Higher-priority items are always dequeued before lower-priority ones. Under saturation it drops the lowest-priority items first (tail-drop per tier). This makes it suitable as the entry point of a load-balancer or transaction processor pipeline: pair it with a Semaphore or RateLimiter downstream.
func NewQueue ¶
NewQueue creates a priority queue that dispatches items to handler concurrently. handler is called once per item with the queue's context; if it returns a non-nil error the item is counted as failed but the queue keeps running.
func (*Queue) Close ¶
func (q *Queue) Close()
Close stops accepting new items and waits for all workers to drain and exit.
func (*Queue) CollectMetrics ¶
func (q *Queue) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for Queue.
func (*Queue) Depth ¶
Depth returns the total number of items currently in the queue across all priorities.
func (*Queue) DepthByPriority ¶
DepthByPriority returns the number of items waiting at each priority level.
func (*Queue) Enqueue ¶
Enqueue adds an item at the given priority. It returns ErrQueueFull if the per-priority bin is at capacity, or ErrQueueClosed if the queue is shut down.
func (*Queue) EnqueueCtx ¶
EnqueueCtx adds an item at the given priority, blocking until space is available, the context is cancelled, or the queue is closed.
func (*Queue) Metrics ¶
func (q *Queue) Metrics() *QueueMetrics
Metrics returns the queue's operational metrics.
type QueueMetrics ¶
type QueueMetrics struct {
Enqueued atomic.Uint64
Dequeued atomic.Uint64
Dropped atomic.Uint64 // items discarded after being dequeued (item timeout)
Timeouts atomic.Uint64
Depth atomic.Int64
MaxDepth atomic.Int64
Saturated atomic.Uint64 // times the queue was full at enqueue time
}
QueueMetrics tracks operational statistics for the priority queue.
type QueueOption ¶
type QueueOption func(*Queue)
QueueOption configures a Queue.
func QueueWithCapacity ¶
func QueueWithCapacity(capacity int) QueueOption
QueueWithCapacity sets the maximum number of items the queue can hold per priority level. Total capacity is capacity × priorityCount. Default is 256 per level.
func QueueWithTimeout ¶
func QueueWithTimeout(d time.Duration) QueueOption
QueueWithTimeout sets the maximum time an item may wait in the queue before being dropped. Zero means no timeout (items wait until a worker is free).
func QueueWithWorkers ¶
func QueueWithWorkers(n int) QueueOption
QueueWithWorkers sets the number of concurrent consumer goroutines (default 1).
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter bounds throughput with a prioritized token bucket. Allow / AllowN are lock-free. Blocking Acquire uses CoDel-style priority queues. Replenish manually returns tokens; it is distinct from time-based refill and is intended for reservation patterns where the caller holds a token between operations.
func NewRateLimiter ¶
func NewRateLimiter(ratePerSec float64, burst int, opts ...RateLimiterOption) *RateLimiter
NewRateLimiter creates a token bucket rate limiter. ratePerSec is the sustained fill rate; burst is the maximum token capacity. Default maxWait (CoDel dropping threshold) is 500ms.
func (*RateLimiter) Acquire ¶
func (r *RateLimiter) Acquire(ctx context.Context, p Priority) error
Acquire waits for a single token, respecting priority and context cancellation. Higher-priority callers are dequeued before lower-priority ones already waiting.
func (*RateLimiter) Allow ¶
func (r *RateLimiter) Allow(p Priority) bool
Allow consumes one token if available. Non-blocking, lock-free.
func (*RateLimiter) AllowN ¶
func (r *RateLimiter) AllowN(p Priority, n int64) bool
AllowN attempts to consume n tokens atomically. Non-blocking, lock-free.
func (*RateLimiter) Burst ¶
func (r *RateLimiter) Burst() int64
Burst returns the maximum token capacity the rate limiter was created with.
func (*RateLimiter) Close ¶
func (r *RateLimiter) Close()
Close shuts down the rate limiter, unblocking all pending waiters with ErrRateLimiterClosed.
func (*RateLimiter) CollectMetrics ¶
func (r *RateLimiter) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for RateLimiter.
func (*RateLimiter) Metrics ¶
func (r *RateLimiter) Metrics() *RateLimiterMetrics
Metrics returns the rate limiter's operational metrics.
func (*RateLimiter) Release ¶
func (r *RateLimiter) Release()
Release replenishes one token and wakes the highest-priority waiter if any are queued. Retained for compatibility with patterns that pair Acquire with Release. Prefer Replenish(1) for new code where the intent is explicit.
func (*RateLimiter) Replenish ¶
func (r *RateLimiter) Replenish(n int64)
Replenish adds n tokens back to the bucket, up to the burst ceiling. Use this when a caller held a reservation and is returning it — for example, when a request is retried locally and the upstream slot does not need to be consumed. This is NOT the same as time-based refill.
func (*RateLimiter) Reserve ¶
func (r *RateLimiter) Reserve(n int64, opts ...ReserveOption) *Reservation
Reserve attempts to reserve n tokens from the rate limiter without blocking. It returns a Reservation whose Delay() tells the caller how long to wait. The caller can Cancel() the reservation if the delay is unacceptable.
Unlike Acquire, Reserve never blocks. It is safe for concurrent use.
func (*RateLimiter) ReserveMetrics ¶
func (r *RateLimiter) ReserveMetrics() *ReservationMetrics
ReserveMetrics returns the metrics for the reservation subsystem. Returns nil if Reserve has never been called.
func (*RateLimiter) Tokens ¶
func (r *RateLimiter) Tokens() int64
Tokens returns the number of tokens currently available.
type RateLimiterMetrics ¶
type RateLimiterMetrics struct {
AllowedFast atomic.Uint64
AllowedSlow atomic.Uint64
Rejected atomic.Uint64
TokensConsumed atomic.Uint64
TokensRefilled atomic.Uint64
QueueDepth atomic.Int64
MaxQueueDepth atomic.Int64
}
RateLimiterMetrics tracks operational statistics for the rate limiter.
type RateLimiterOption ¶
type RateLimiterOption func(*RateLimiter)
RateLimiterOption configures a RateLimiter.
func RateLimiterWithMaxWait ¶
func RateLimiterWithMaxWait(d time.Duration) RateLimiterOption
RateLimiterWithMaxWait sets the hard ceiling on waiter age during CoDel dropping mode. In normal mode waiters are not evicted by age; their own context handles timeout.
type Reaper ¶
type Reaper struct {
// contains filtered or unexported fields
}
func NewReaper ¶
func NewReaper(ttl time.Duration, opts ...ReaperOption) *Reaper
NewReaper creates a TTL-based expiry manager with min-heap scheduling. The handler is called once per expired entry in a background goroutine.
func (*Reaper) CollectMetrics ¶
func (r *Reaper) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for Reaper.
func (*Reaper) Deadline ¶
Deadline returns the earliest expiry time across all shards, or false if empty.
func (*Reaper) Metrics ¶
func (r *Reaper) Metrics() *ReaperMetrics
Metrics returns the reaper's operational statistics.
func (*Reaper) Register ¶
func (r *Reaper) Register(h ReaperHandler)
Register sets the handler for expired tasks (backward compatible).
func (*Reaper) Start ¶
func (r *Reaper) Start()
Start is a no-op for backward compatibility. Reaper starts automatically now.
func (*Reaper) Stop ¶
func (r *Reaper) Stop()
Stop shuts down all reaper goroutines and releases internal resources.
type ReaperHandler ¶
type ReaperMetrics ¶
type ReaperMetrics struct {
TouchCalls atomic.Uint64
TouchAtCalls atomic.Uint64
RemoveCalls atomic.Uint64
ClearCalls atomic.Uint64
ExpiredTotal atomic.Uint64
ExpiredHandled atomic.Uint64
ExpiredMissed atomic.Uint64
ExpiredErrors atomic.Uint64
ActiveTasks atomic.Int64
MaxActiveTasks atomic.Int64
TotalTasksSeen atomic.Uint64
AvgExpirationMs atomic.Int64
MinExpirationMs atomic.Int64
MaxExpirationMs atomic.Int64
LoopIterations atomic.Uint64
SignalsSent atomic.Uint64
StopsReceived atomic.Uint64
}
type ReaperOption ¶
type ReaperOption func(*Reaper)
func ReaperWithHandler ¶
func ReaperWithHandler(h ReaperHandler) ReaperOption
ReaperWithHandler sets the callback invoked when an entry's TTL expires.
func ReaperWithLogger ¶
func ReaperWithLogger(l *ll.Logger) ReaperOption
ReaperWithLogger sets a custom logger for the reaper.
func ReaperWithMinTick ¶
func ReaperWithMinTick(tick time.Duration) ReaperOption
ReaperWithMinTick sets the minimum poll interval for the expiry loop.
func ReaperWithShards ¶
func ReaperWithShards(count uint32) ReaperOption
ReaperWithShards controls the number of internal heap shards for reduced contention.
func ReaperWithTimerLimit ¶
func ReaperWithTimerLimit(limit time.Duration) ReaperOption
ReaperWithTimerLimit caps the duration the reaper will sleep before checking again.
type ReaperTask ¶
type Reservation ¶
type Reservation struct {
// contains filtered or unexported fields
}
Reservation is a future token grant from a RateLimiter. The caller inspects Delay() and decides whether to wait or drop. Once committed, call Wait(ctx) to block for exactly the reservation's delay. A Reservation is single-use and not safe for concurrent access.
func (*Reservation) Cancel ¶
func (r *Reservation) Cancel()
Cancel releases the reservation without consuming tokens. Safe to call multiple times.
func (*Reservation) Delay ¶
func (r *Reservation) Delay() time.Duration
Delay returns how long the caller must wait before the reserved tokens are available. Zero means tokens are available immediately. A negative value means the reservation was cancelled.
func (*Reservation) OK ¶
func (r *Reservation) OK() bool
OK reports whether the reservation is valid and not yet used or cancelled.
type ReservationMetrics ¶
type ReservationMetrics struct {
Reserved atomic.Uint64 // successful Reserve calls
Used atomic.Uint64 // reservations that called Wait successfully
Dropped atomic.Uint64 // reservations cancelled before use
Expired atomic.Uint64 // reservations that timed out during Wait
}
ReservationMetrics tracks reservation operational statistics.
type ReserveOption ¶
type ReserveOption func(*reserveConfig)
ReserveOption configures reservation behaviour.
func ReserveWithMaxDelay ¶
func ReserveWithMaxDelay(d time.Duration) ReserveOption
ReserveWithMaxDelay causes Reserve to return a cancelled Reservation if the computed delay exceeds d. Use to implement admission control at the call site without blocking any goroutine.
type Retry ¶
type Retry struct {
// contains filtered or unexported fields
}
Retry defines how retries are performed. Create one once and reuse it across many Do / DoCtx calls — it is safe for concurrent use.
func NewRetry ¶
func NewRetry(opts ...RetryOption) *Retry
NewRetry constructs a Retry with sensible defaults.
func (*Retry) Do ¶
Do executes fn according to the policy, retrying on retryable errors. The provided context governs the total deadline; each individual attempt also respects context cancellation. A nil context behaves like context.Background(). Returns the last error wrapped in ErrRetryExhausted if all attempts fail.
func (*Retry) Metrics ¶
func (p *Retry) Metrics() *RetryMetrics
Metrics returns the policy's operational metrics.
type RetryMetrics ¶
type RetryMetrics struct {
Attempts atomic.Uint64 // total individual calls (including first)
Successes atomic.Uint64 // calls that ultimately succeeded
Failures atomic.Uint64 // calls that exhausted all retries
Timeouts atomic.Uint64 // attempts abandoned due to context deadline
}
RetryMetrics tracks retry operational statistics.
type RetryOption ¶
type RetryOption func(*Retry)
RetryOption configures a Retry.
func RetryWithBackoff ¶
func RetryWithBackoff(b *Backoff) RetryOption
RetryWithBackoff replaces the delay calculation with a shared Backoff, so a service can use one policy for both manual retry loops and Retry.Do. The policy's jitter strategy is used as-is (its cap still applies).
func RetryWithBaseDelay ¶
func RetryWithBaseDelay(d time.Duration) RetryOption
RetryWithBaseDelay sets the initial delay before the first retry (default 100ms).
func RetryWithJitter ¶
func RetryWithJitter(enabled bool) RetryOption
RetryWithJitter enables full jitter on the backoff delay (default true). Full jitter randomises each delay in [0, computed_delay] to spread load.
func RetryWithMaxAttempts ¶
func RetryWithMaxAttempts(n int) RetryOption
RetryWithMaxAttempts sets the total number of attempts (initial + retries). Default is 3.
func RetryWithMaxDelay ¶
func RetryWithMaxDelay(d time.Duration) RetryOption
RetryWithMaxDelay caps the backoff delay (default 30s).
func RetryWithMultiplier ¶
func RetryWithMultiplier(m float64) RetryOption
RetryWithMultiplier sets the exponential backoff multiplier (default 2.0).
func RetryWithOnRetry ¶
func RetryWithOnRetry(fn func(attempt int, err error)) RetryOption
RetryWithOnRetry registers a callback invoked before each retry with the attempt number (1-based) and the error that triggered the retry.
func RetryWithRetryIf ¶
func RetryWithRetryIf(fn func(error) bool) RetryOption
RetryWithRetryIf replaces the default retry predicate. By default all non-nil errors are retried. Supply a custom function to skip retrying on permanent errors (e.g. 404, validation failures).
type RingOption ¶
type RingOption func(*ringConfig)
RingOption configures a HashRing.
func RingWithHash ¶
func RingWithHash(fn func(string) uint64) RingOption
RingWithHash replaces the default key hash (xxhash.Sum64String). The function must be deterministic: the same input must always yield the same value, or key placement will be unstable. Nil is ignored.
func RingWithReplicas ¶
func RingWithReplicas(n int) RingOption
RingWithReplicas sets the number of virtual nodes per physical node. Non-positive values are ignored (the default of 128 is kept). More replicas → smoother distribution, larger snapshots.
type Routine ¶
type Routine struct {
Interval time.Duration // Interval between task executions
MaxRuns int // Maximum number of runs (0 for unlimited)
Cron string // Cron expression (e.g., "0 0 * * *" for daily at midnight, "@every 1m")
}
Routine configures recurring task execution with an interval and maximum runs. Thread-safe for use in scheduling configurations.
type RoutineInfo ¶
type RoutineInfo struct {
ID string
Label string
State RoutineState
StartedAt time.Time
EndedAt time.Time
Err error
Stack []byte // non-nil only when State == RoutinePanicked
}
RoutineInfo holds the observable state of a single tracked goroutine.
type RoutineOption ¶
type RoutineOption func(*Routines)
RoutineOption configures a Routines tracker.
func RoutineWithIDGenerator ¶
func RoutineWithIDGenerator(fn func(label string) string) RoutineOption
RoutineWithIDGenerator sets the base-name generator used to build routine IDs. The tracker appends a monotonic sequence number ("<base>#<n>") so IDs stay unique even when two routines share a label; the generator should therefore return a stable prefix rather than a complete unique ID.
func RoutineWithMaxEntries ¶
func RoutineWithMaxEntries(n int) RoutineOption
RoutineWithMaxEntries caps the number of retained RoutineInfo entries. When exceeded, completed entries are evicted to bound memory growth.
func RoutineWithOnDone ¶
func RoutineWithOnDone(fn func(info RoutineInfo)) RoutineOption
RoutineWithOnDone sets a callback invoked when any goroutine finishes (regardless of success, error, or panic).
func RoutineWithOnPanic ¶
func RoutineWithOnPanic(fn func(info RoutineInfo)) RoutineOption
RoutineWithOnPanic sets a callback invoked whenever a goroutine panics. The callback runs synchronously before the goroutine exits.
type RoutineState ¶
type RoutineState int32
RoutineState describes the lifecycle state of a tracked goroutine.
const ( RoutineRunning RoutineState = iota RoutineDone RoutinePanicked RoutineCancelled )
func (RoutineState) String ¶
func (s RoutineState) String() string
String returns a human-readable label for the goroutine state.
type Routines ¶
type Routines struct {
// contains filtered or unexported fields
}
Routines is a goroutine tracker and lifecycle manager. It is the answer to the "rogue goroutine" problem: every goroutine spawned through Routines is registered, tracked by state, and guaranteed to be joined by Stop or Wait.
Singleton usage:
var rt = jack.NewRoutines()
rt.Spawn("fetch", func(ctx context.Context) error { ... })
rt.SpawnCtx("heartbeat", func(ctx context.Context) error { ... })
rt.Stop() // cancels context, waits for all goroutines
All methods are safe for concurrent use.
func NewRoutines ¶
func NewRoutines(opts ...RoutineOption) *Routines
NewRoutines creates a Routines tracker. The returned tracker has its own cancellable context; call Stop() to terminate all goroutines it owns.
func (*Routines) Background ¶
Background spawns a long-running goroutine that is restarted automatically if it returns a non-nil error, up to maxRestarts times (0 = unlimited). Each restart is counted in Metrics.Spawned.
func (*Routines) Cancel ¶
Cancel cancels a single goroutine by ID. Returns false if the ID is unknown or the goroutine has already exited.
func (*Routines) CollectMetrics ¶
func (r *Routines) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for Routines.
func (*Routines) Forget ¶
Forget removes a completed goroutine from tracking, reclaiming the memory held by its RoutineInfo. Returns false if the ID is unknown or the goroutine is still running.
func (*Routines) Info ¶
func (r *Routines) Info(id string) (RoutineInfo, bool)
Info returns a snapshot of the named goroutine's current state. Returns false if no goroutine with that ID exists.
func (*Routines) List ¶
func (r *Routines) List() []RoutineInfo
List returns a snapshot of all tracked goroutines, including completed ones.
func (*Routines) Metrics ¶
func (r *Routines) Metrics() *RoutinesMetrics
Metrics returns aggregate goroutine statistics.
func (*Routines) Spawn ¶
Spawn spawns a goroutine under the tracker's context. The goroutine is cancelled when Stop() is called or the tracker's context is cancelled. label is used for identification; it need not be unique — a unique ID is derived automatically. Returns the assigned ID so the caller can query status via Info(id).
func (*Routines) SpawnCtx ¶
func (r *Routines) SpawnCtx(ctx context.Context, label string, fn func(context.Context) error) string
SpawnCtx spawns a goroutine under a caller-supplied context. The goroutine is cancelled when either the supplied ctx OR the tracker's context is done — whichever fires first.
func (*Routines) Stop ¶
func (r *Routines) Stop()
Stop cancels the tracker's context and waits for all goroutines to exit. After Stop returns, no goroutines tracked by this Routines are running.
func (*Routines) StopTimeout ¶
StopTimeout cancels the tracker's context and waits up to the given duration for all goroutines to exit. Returns an error if the timeout is exceeded.
type RoutinesMetrics ¶
type RoutinesMetrics struct {
Spawned atomic.Uint64 // total goroutines ever started
Active atomic.Int64 // currently running
Completed atomic.Uint64 // finished without error or panic
Failed atomic.Uint64 // returned a non-nil error
Panicked atomic.Uint64 // recovered from a panic
Cancelled atomic.Uint64 // stopped via context cancellation
}
RoutinesMetrics tracks aggregate goroutine statistics.
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
Runner executes tasks asynchronously using a buffered task queue. It supports observability and logging, processing tasks in a single goroutine. Thread-safe via mutex, wait group, and channel operations.
func NewRunner ¶
func NewRunner(opts ...RunnerOption) *Runner
NewRunner creates a new Runner with the specified options. It initializes a task queue and starts a processing goroutine. Thread-safe via initialization and logger namespace. Example:
runner := NewRunner(WithRunnerQueueSize(10), WithRunnerObservable(obs)) // Creates runner with queue size 10
func (*Runner) Do ¶
Do submits a Task for execution in the runner’s queue. It returns an error if the task is nil or the runner is closed. Thread-safe via submit and channel operations. Example:
runner.Do(myTask) // Submits a task
func (*Runner) DoCtx ¶
DoCtx submits a TaskCtx for execution with the given context. It returns an error if the context or task is nil, the context is done, or the runner is closed. Thread-safe via submit and channel operations. Example:
runner.DoCtx(ctx, myTaskCtx) // Submits a context-aware task
func (*Runner) QueueSize ¶
QueueSize returns the current number of tasks in the queue. Thread-safe via channel length access. Example:
size := runner.QueueSize() // Gets current queue size
type RunnerOption ¶
type RunnerOption func(*runnerOptions)
RunnerOption configures a Runner during creation.
func WithRunnerIDGenerator ¶
func WithRunnerIDGenerator(fn func(interface{}) string) RunnerOption
WithRunnerIDGenerator sets the task ID generator function. Example:
runner := NewRunner(WithRunnerIDGenerator(customIDFunc)) // Sets custom ID generator
func WithRunnerObservable ¶
func WithRunnerObservable(obs Observable[Event]) RunnerOption
WithRunnerObservable sets the observable for task events. Example:
runner := NewRunner(WithRunnerObservable(obs)) // Configures observable
func WithRunnerQueueSize ¶
func WithRunnerQueueSize(size int) RunnerOption
WithRunnerQueueSize sets the task queue size. Non-positive values are ignored. Example:
runner := NewRunner(WithRunnerQueueSize(20)) // Sets queue size to 20
type Safely ¶
Safely wraps sync.Mutex with safe execution methods.
func (*Safely) Do ¶
func (m *Safely) Do(fn func())
Do executes fn while holding the mutex lock. The function is guaranteed to run with exclusive access, ensuring thread safety. If fn is nil, Do will panic. Example:
var mu Safely
mu.Do(func() {
// Critical section
fmt.Println("Safe execution")
})
func (*Safely) DoCtx ¶
DoCtx executes fn while holding the mutex lock, with context support. The lock is acquired, then the context is checked. If the context is done, its error is returned and fn is not executed. Otherwise, fn is executed in a goroutine, allowing DoCtx to return early if the context is canceled or times out (e.g., via context.WithTimeout).
The mutex remains held until fn itself returns, even when DoCtx returns early because the context fired. Releasing it earlier would let a second goroutine enter the critical section while fn is still running.
If fn is nil, DoCtx will return a *CaughtPanic. If ctx is nil, DoCtx will panic when ctx.Err() is called. This method does not recover from panics in fn; use SafeCtx for panic recovery. Example:
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
err := mu.DoCtx(ctx, func() error {
// Critical section, no need to check ctx
time.Sleep(100 * time.Millisecond)
return nil
})
// err will likely be context.DeadlineExceeded due to timeout
func (*Safely) Safe ¶
Safe executes fn while holding the mutex lock, with panic recovery. If fn panics, a *CaughtPanic error is returned. Otherwise, the error returned by fn is returned. If fn is nil, Safe will return a *CaughtPanic. Example:
var mu Safely
err := mu.Safe(func() error {
// Critical section
return nil
})
if err != nil {
fmt.Println("Error:", err)
}
func (*Safely) SafeCtx ¶
SafeCtx executes fn with panic recovery and context awareness while holding the mutex lock. The lock is acquired, then fn is executed via the standalone SafeCtx logic. DoCtx-style early return applies: if the context is canceled or times out, SafeCtx returns that error immediately, but the mutex stays held until fn returns so the critical section is never run concurrently. Any panics are recovered. If fn is nil, SafeCtx will return a *CaughtPanic. If ctx is nil, SafeCtx will panic when ctx.Err() is called. Example:
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
err := mu.SafeCtx(ctx, func() error {
// Critical section, no need to check ctx
time.Sleep(100 * time.Millisecond)
return nil
})
// err will likely be context.DeadlineExceeded due to timeout
type Schedule ¶
type Schedule struct {
Type string // Type of event (e.g., "task_submitted", "task_submission_failed", "stopped")
Name string // Name of the scheduler emitting the event
TaskID string // Unique identifier for the task
TaskType string // Type of the task (e.g., struct name or type description)
Message string // Descriptive message providing context for the event
Error error // Any error associated with the event, if applicable
Routine Routine // Configuration of the scheduling routine (e.g., interval, max runs)
Time time.Time // Timestamp when the event occurred
NextRun time.Time // Scheduled time for the next task execution, if applicable
}
Schedule represents an event emitted by the scheduler for observability.
type Scheduler ¶
type Scheduler struct {
// contains filtered or unexported fields
}
Scheduler manages periodic or limited task submissions to a Pool. It supports both interval-based and cron expression-based scheduling.
func NewScheduler ¶
NewScheduler creates a new Scheduler instance.
func (*Scheduler) Do ¶
Do starts scheduling for the provided non-context-aware tasks. It initializes the scheduler to execute the given tasks according to the configured routine (e.g., interval or max runs). The method ensures thread-safety by locking the scheduler state and checks if the scheduler is already running to prevent duplicate executions. Each task is executed in its own goroutine, and the method emits a "started" event for observability.
func (*Scheduler) DoCtx ¶
DoCtx starts scheduling for context-aware tasks. Similar to Do, but designed for tasks that accept a context for cancellation or deadlines. It ensures thread-safety, checks for existing runs, and associates a task execution context with the tasks. Each task runs in its own goroutine, and a "started" event is emitted for observability.
func (*Scheduler) Entries ¶
Entries returns the current cron entries if using cron-based scheduling.
type Scheduling ¶
type Scheduling struct {
RetryCount int // Number of retry attempts for task submission on failure
RetryBackoff time.Duration // Duration to wait between retry attempts for failed submissions
// contains filtered or unexported fields
}
Scheduling holds configuration for retry behavior and observability.
type Semaphore ¶
type Semaphore struct {
// contains filtered or unexported fields
}
Semaphore bounds concurrent access with priority and CoDel queueing. The fast path (TryAcquire) is lock-free. Blocking acquire uses per-priority queues that switch from FIFO to LIFO under sustained overload.
func NewSemaphore ¶
func NewSemaphore(capacity int, opts ...SemaphoreOption) *Semaphore
NewSemaphore creates a prioritized semaphore with the given capacity. Defaults: target sojourn 5ms, max sojourn 500ms, interval 100ms.
func (*Semaphore) Acquire ¶
Acquire waits for a slot, respecting priority and context cancellation. Higher-priority callers are served before lower-priority ones already queued.
func (*Semaphore) Available ¶
Available returns the number of slots currently available for acquisition.
func (*Semaphore) Capacity ¶
Capacity returns the total slot capacity the semaphore was created with.
func (*Semaphore) Close ¶
func (s *Semaphore) Close()
Close permanently shuts down the semaphore, unblocking all pending waiters with ErrSemaphoreClosed.
func (*Semaphore) CollectMetrics ¶
func (s *Semaphore) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for Semaphore.
func (*Semaphore) Metrics ¶
func (s *Semaphore) Metrics() *SemaphoreMetrics
Metrics returns the semaphore's operational metrics.
func (*Semaphore) Release ¶
func (s *Semaphore) Release()
Release returns a slot and attempts to hand it to the highest-priority waiter. If the dequeued waiter was context-cancelled, Release retries until it finds a live one.
func (*Semaphore) TryAcquire ¶
TryAcquire attempts to take a slot without blocking. Returns false immediately if no slot is available or the semaphore is closed.
type SemaphoreMetrics ¶
type SemaphoreMetrics struct {
AcquiredFast atomic.Uint64
AcquiredSlow atomic.Uint64
Released atomic.Uint64
Rejected atomic.Uint64
Timeouts atomic.Uint64
QueueDepth atomic.Int64
MaxQueueDepth atomic.Int64
}
SemaphoreMetrics tracks operational statistics for the semaphore. All fields are safe for concurrent reads without additional locking.
type SemaphoreOption ¶
type SemaphoreOption func(*Semaphore)
SemaphoreOption configures a Semaphore.
func SemaphoreWithInterval ¶
func SemaphoreWithInterval(d time.Duration) SemaphoreOption
SemaphoreWithInterval sets the CoDel interval for sustained overload detection. Dropping mode engages when sojourn stays above target for this long.
func SemaphoreWithMaxSojourn ¶
func SemaphoreWithMaxSojourn(d time.Duration) SemaphoreOption
SemaphoreWithMaxSojourn sets the hard ceiling on waiter age. Exceeding this immediately engages dropping mode regardless of interval.
func SemaphoreWithTargetSojourn ¶
func SemaphoreWithTargetSojourn(d time.Duration) SemaphoreOption
SemaphoreWithTargetSojourn sets the CoDel target sojourn time. When the oldest waiter exceeds this threshold, the queue enters dropping mode.
type Shutdown ¶
type Shutdown struct {
// contains filtered or unexported fields
}
Shutdown manages the graceful shutdown process.
func NewShutdown ¶
func NewShutdown(opts ...ShutdownOption) *Shutdown
NewShutdown creates a configured Shutdown manager. Defaults: 30s timeout, sequential execution, SIGINT/SIGTERM/SIGQUIT.
func (*Shutdown) Done ¶
func (sm *Shutdown) Done() <-chan struct{}
Done returns a channel closed when shutdown completes.
func (*Shutdown) GetStats ¶
func (sm *Shutdown) GetStats() *ShutdownStats
GetStats returns a deep copy of current shutdown statistics. Safe for concurrent reads; protects against mutation during access. Used by public APIs to return final results. Used by public APIs to return final results.
func (*Shutdown) IsShuttingDown ¶
IsShuttingDown reports whether shutdown has been initiated. Thread-safe via atomic boolean; useful for guarding late registrations. Returns true once the first shutdown trigger occurs.
func (*Shutdown) Register ¶
Register adds a cleanup task. Supported types: func(), func() error, func(context.Context) error, io.Closer.
func (*Shutdown) RegisterCloser ¶
RegisterCloser registers an io.Closer. Convenience wrapper that converts Close() error into shutdown error. Name reflects the concrete type when left empty.
func (*Shutdown) RegisterFunc ¶
func (*Shutdown) RegisterWithContext ¶
RegisterWithContext registers a fully context-aware callback. Allows explicit naming and direct use of jack.FuncCtx functions. Preferred for advanced cleanup needing cancellation/timeout awareness.
func (*Shutdown) RegisterWithPriority ¶
func (*Shutdown) TriggerShutdown ¶
func (sm *Shutdown) TriggerShutdown() *ShutdownStats
TriggerShutdown manually initiates the shutdown process and returns final statistics.
func (*Shutdown) Wait ¶
func (sm *Shutdown) Wait() *ShutdownStats
Wait blocks until a signal is received or TriggerShutdown is called, then runs all registered cleanup tasks and returns statistics.
func (*Shutdown) WaitChan ¶
func (sm *Shutdown) WaitChan() <-chan *ShutdownStats
WaitChan returns a channel that receives stats once shutdown is complete. Non-blocking alternative to Wait(), ideal for async integration. Channel is closed after sending the single stats value.
type ShutdownError ¶
ShutdownError provides structured error details for a failed cleanup task.
func (*ShutdownError) Error ¶
func (e *ShutdownError) Error() string
Error implements the error interface, combining the task name and underlying cause.
func (*ShutdownError) Unwrap ¶
func (e *ShutdownError) Unwrap() error
Unwrap returns the underlying error for errors.Is/As chain traversal.
type ShutdownOption ¶
type ShutdownOption func(*Shutdown)
ShutdownOption configures the Shutdown.
func ShutdownConcurrent ¶
func ShutdownConcurrent() ShutdownOption
ShutdownConcurrent enables concurrent execution of cleanup functions. By default, execution is sequential (LIFO).
func ShutdownWithForceQuit ¶
func ShutdownWithForceQuit(d time.Duration) ShutdownOption
ShutdownWithForceQuit enables a force-quit trigger after a specific timeout, cancelling the cleanup context if shutdown takes too long.
func ShutdownWithLogger ¶
func ShutdownWithLogger(l *ll.Logger) ShutdownOption
ShutdownWithLogger sets a custom logger for the manager.
func ShutdownWithSignals ¶
func ShutdownWithSignals(signals ...os.Signal) ShutdownOption
ShutdownWithSignals specifies which OS signals to capture. If not set, defaults to SIGINT, SIGTERM, and SIGQUIT.
func ShutdownWithTimeout ¶
func ShutdownWithTimeout(d time.Duration) ShutdownOption
ShutdownWithTimeout sets the maximum time to wait for shutdown completion.
type ShutdownStats ¶
type Stage ¶
Stage is one step of a Pipeline.
A stage receives the current context and a next function that invokes the rest of the pipeline (ending in the terminal operation). It may call next zero times to short-circuit the request, once to continue, or several times to repeat the remainder — RetryStage relies on that to re-attempt the downstream calls. Stages may pass a derived context to next (for example a timeout) and may inspect the error returned by the remainder.
func AdaptiveStage ¶
func AdaptiveStage(a *AdaptiveLimiter, p Priority) Stage
AdaptiveStage runs the remainder under an AdaptiveLimiter, which grows and shrinks its concurrency limit based on observed latencies.
func BreakerStage ¶
BreakerStage rejects calls while the circuit is open and records the outcome.
func BulkheadStage ¶
BulkheadStage caps concurrency for the given partition before continuing.
func RateLimiterStage ¶
func RateLimiterStage(rl *RateLimiter, p Priority) Stage
RateLimiterStage waits for a token, then continues. Tokens are consumed: they are not returned when the pipeline completes, which is the point of a rate limiter.
func RetryStage ¶
RetryStage retries the remainder of the pipeline according to the retry policy.
func SemaphoreStage ¶
SemaphoreStage bounds concurrent executions by acquiring and releasing a slot.
func ThrottleStage ¶
ThrottleStage sheds calls locally with ErrThrottled when the throttle is applying backpressure for the given priority.
func TimeoutStage ¶
TimeoutStage bounds the remainder of the pipeline with a deadline. A non-positive duration is a no-op.
type Task ¶
type Task interface {
// Do executes the task and returns any error.
Do() error
}
Task represents a simple task that can be executed without a context.
type TaskCtx ¶
type TaskCtx interface {
// Do executes the task with the given context and returns any error.
Do(ctx context.Context) error
}
TaskCtx represents a context-aware task that can be executed with a context.
type Throttle ¶
type Throttle struct {
// contains filtered or unexported fields
}
Throttle is a client-side self-tuning throttle. It observes upstream acceptance and rejection rates and probabilistically rejects local requests before sending when the upstream is overloaded. Lower-priority traffic is throttled more aggressively than critical traffic.
func NewThrottle ¶
func NewThrottle(priorities int, opts ...ThrottleOption) *Throttle
NewThrottle creates a throttle with the given number of priorities. The standard jack priority count (4) is recommended.
func (*Throttle) Accepted ¶
Accepted records a successful upstream response for the given priority tier, decreasing throttle pressure for that tier.
func (*Throttle) Allow ¶
Allow returns true if the request should proceed. Uses a lock-free LCG random check against the per-priority rejection probability.
func (*Throttle) Close ¶
func (a *Throttle) Close()
Close shuts down the throttle; all subsequent Allow calls return false.
func (*Throttle) CollectMetrics ¶
func (t *Throttle) CollectMetrics(sink MetricSink)
CollectMetrics implements MetricsCollector for Throttle, emitting the current rejection probability per priority tier.
func (*Throttle) Metrics ¶
func (a *Throttle) Metrics() *ThrottleMetrics
Metrics returns the throttle's operational metrics.
func (*Throttle) Probability ¶
Probability returns the current rejection probability for the given priority as a value in [0.0, 1.0]. This is the live per-priority value, not a single aggregate.
type ThrottleMetrics ¶
type ThrottleMetrics struct {
RequestsTotal atomic.Uint64
Accepted atomic.Uint64
RejectedLocal atomic.Uint64
RejectedRemote atomic.Uint64
// ThrottleProbs holds the current rejection probability for each priority tier,
// scaled by throttleProbScale (0 = no throttling, 10 000 = always throttled).
// Index matches Priority: 0=Critical, 1=High, 2=Medium, 3=Low.
ThrottleProbs [priorityCount]atomic.Int64
}
ThrottleMetrics tracks throttle behaviour across all priority tiers.
type ThrottleOption ¶
type ThrottleOption func(*Throttle)
ThrottleOption configures a Throttle.
func ThrottleWithRatio ¶
func ThrottleWithRatio(ratio float64) ThrottleOption
ThrottleWithRatio sets the overcommit ratio (default 2.0). A ratio of 2.0 targets a 50% upstream acceptance rate before local shedding begins.
func ThrottleWithWindow ¶
func ThrottleWithWindow(d time.Duration) ThrottleOption
ThrottleWithWindow sets the observation window for accept/reject counters (default 1 minute). Counters reset after windowResetSamples samples to adapt to changing upstream load.
func ThrottleWithWindowResetSamples ¶
func ThrottleWithWindowResetSamples(n uint64) ThrottleOption
ThrottleWithWindowResetSamples sets how many samples to collect before resetting the observation window (default 1000). Smaller values adapt faster; larger values are smoother.
type Vitals ¶
type Vitals struct {
Start HookCtx
End CallbackCtx
Timed CallbackCtx
TimedWait time.Duration
}
func NewVitals ¶
func NewVitals(opts ...VitalsOption) *Vitals
NewVitals creates a new Vitals instance configured with the provided functional options. Each option modifies the Vitals configuration before returning the initialized instance. If no options are provided, returns a Vitals with all fields set to their zero values.
func (*Vitals) Execute ¶
Execute runs an operation with optional start and end hooks while propagating errors. It invokes the Start hook first, then the operation, and finally the End callback on success. If Start or the operation returns an error, execution stops and the error is returned immediately.
func (*Vitals) ExecuteCtx ¶
ExecuteCtx runs a context-aware operation with optional start and end hooks. It invokes the Start hook first, then the operation with context, and finally the End callback. Errors from Start or the operation are propagated immediately, skipping subsequent steps.
type VitalsOption ¶
type VitalsOption func(*Vitals)
func VitalsWithEnd ¶
func VitalsWithEnd(callback CallbackCtx) VitalsOption
VitalsWithEnd configures a callback function to run after successful operation completion. The callback receives the context and operation ID but does not return an error. Use this option for post-operation cleanup, notifications, or result processing.
func VitalsWithStart ¶
func VitalsWithStart(hook HookCtx) VitalsOption
VitalsWithStart configures a hook function to run at the beginning of a vital operation. The hook receives the context and operation ID, and can return an error to abort execution. Use this option to inject pre-operation logic like logging, metrics, or validation.
func VitalsWithTimed ¶
func VitalsWithTimed(callback CallbackCtx, wait time.Duration) VitalsOption
VitalsWithTimed configures a timed callback with a wait duration for delayed execution. The callback receives context and ID after the specified wait period elapses. Use this option for deferred operations like timeout handling or scheduled cleanup.
type WaitGroup ¶
type WaitGroup struct {
// contains filtered or unexported fields
}
WaitGroup is a reusable goroutine coordinator. Unlike sync.WaitGroup, it can be reused immediately after Wait returns.
func (*WaitGroup) Go ¶
func (wg *WaitGroup) Go(fn func())
Go starts fn in a new goroutine and tracks its completion.
func (*WaitGroup) IsDone ¶
IsDone reports whether all tracked goroutines have completed. Returns false for a zero-value WaitGroup that has never been used.
func (*WaitGroup) Wait ¶
func (wg *WaitGroup) Wait()
Wait blocks until all tracked goroutines complete.
Source Files
¶
- accumulator.go
- adaptive.go
- async.go
- backoff.go
- backpressure.go
- breaker.go
- bulkhead.go
- coalesce.go
- debounce.go
- doc.go
- doctor.go
- flight.go
- fn.go
- func.go
- gate.go
- hedge.go
- helpers.go
- keyedlock.go
- latch.go
- lease.go
- lifetime.go
- looper.go
- lru.go
- metrics.go
- metrics_collectors.go
- observer.go
- oncegroup.go
- patient.go
- pipeline.go
- plocal.go
- pool.go
- queue.go
- ratelimiter.go
- reaper.go
- reservation.go
- retry.go
- ring.go
- routines.go
- run.go
- runner.go
- safely.go
- scheduler.go
- semaphore.go
- shutdown.go
- task.go
- throttle.go
- types.go
- vitals.go
- waitgroup.go
- worker.go