engine

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package engine enforces bounded concurrency with backpressure, request deduplication (singleflight), and integration with adaptive limiting and the circuit breaker. It uses only the standard library: semaphores are buffered channels, singleflight is a mutex-guarded call table. No unbounded goroutines (invariant I13); panics in submitted functions are recovered and returned as errors so they never crash the process.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrOverloaded is returned when the in-flight queue is full (backpressure).
	ErrOverloaded = errors.New("engine: overloaded")
	// ErrInvalidConfig is returned by New for non-positive pool sizes.
	ErrInvalidConfig = errors.New("engine: invalid config")
	// ErrClosed is returned by Submit after Close.
	ErrClosed = errors.New("engine: closed")
)

Sentinel errors.

Functions

This section is empty.

Types

type Engine

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

Engine bounds concurrency and deduplicates concurrent identical requests.

func New

func New(opts ...Option) (*Engine, error)

New returns an Engine with the given options and sane defaults.

func (*Engine) Close

func (e *Engine) Close()

Close rejects new work and waits for all in-flight submissions.

func (*Engine) Drain

func (e *Engine) Drain(ctx context.Context) error

Drain rejects new submissions and waits for admitted callers and detached singleflight execution goroutines to return.

func (*Engine) Submit

func (e *Engine) Submit(ctx context.Context, key string, fn func(context.Context) (any, error)) (any, error)

Submit schedules fn under backpressure and returns fn's result value and error. It returns ErrOverloaded if the in-flight queue is full, ErrCircuitOpen if the breaker is open, ErrRateLimited if concurrency exceeds the adaptive limit. Concurrent calls with the same non-empty key share a single execution (singleflight) and all receive the leader's result. An empty key opts out of de-duplication (writes/unique ops). Panics in fn are recovered as errors.

func (*Engine) SubmitCPU

func (e *Engine) SubmitCPU(ctx context.Context, key string, fn func(context.Context) (any, error)) (any, error)

SubmitCPU schedules compute-heavy work on the CPU pool.

func (*Engine) SubmitClass

func (e *Engine) SubmitClass(
	ctx context.Context,
	class WorkClass,
	key string,
	fn func(context.Context) (any, error),
) (any, error)

SubmitClass schedules fn on the selected resource pool. Submit remains the backwards-compatible IO entry point.

type Option

type Option func(*config)

Option configures an Engine.

func WithBreaker

func WithBreaker(b *ratelimit.Breaker) Option

WithBreaker attaches a circuit breaker.

func WithCPUPool

func WithCPUPool(n int) Option

WithCPUPool sets the CPU concurrency (<= GOMAXPROCS).

func WithFailureClassifier

func WithFailureClassifier(classify func(error) bool) Option

WithFailureClassifier decides whether a non-nil execution error represents provider/system health and should affect AIMD and the circuit breaker.

func WithIOPool

func WithIOPool(n int) Option

WithIOPool sets the IO concurrency (<= DB connection pool size).

func WithLimiter

func WithLimiter(l *ratelimit.Adaptive) Option

WithLimiter attaches an adaptive limiter for AIMD feedback.

func WithMaxInflight

func WithMaxInflight(n int) Option

WithMaxInflight sets the bounded in-flight queue (backpressure).

func WithRPSLimiter

func WithRPSLimiter(l *ratelimit.TokenBucket) Option

WithRPSLimiter attaches a token-bucket request-rate limiter.

type WorkClass

type WorkClass uint8

WorkClass selects the resource pool used by a submission.

const (
	// WorkIO is database or network work and uses the IO pool.
	WorkIO WorkClass = iota
	// WorkCPU is compute-heavy work and uses the CPU pool.
	WorkCPU
)

Jump to

Keyboard shortcuts

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