engine

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package engine defines the core Engine interface and associated types used by all I/O engine implementations (io_uring, epoll, adaptive, std).

This package is the dependency root for engine-related types. User code typically interacts with engines through the top-level celeris package rather than importing this package directly.

Index

Constants

This section is empty.

Variables

View Source
var ErrQueueFull = errors.New("celeris/engine: worker write queue full")

ErrQueueFull is returned by [WorkerLoop.Write] when the worker's outbound queue is full and cannot accept more data without blocking. Callers should apply backpressure to their data source rather than retry in a tight loop.

View Source
var ErrSwitchingNotFrozen = errors.New("celeris/engine: adaptive engine switching is not frozen; call FreezeSwitching before registering driver connections")

ErrSwitchingNotFrozen is returned by adaptive engines when a driver attempts to register a connection without first calling [SwitchFreezer.FreezeSwitching]. Driver FDs cannot migrate across epoll/io_uring worker boundaries, so the adaptive engine must be held on a single sub-engine for the driver's lifetime.

View Source
var ErrUnknownFD = errors.New("celeris/engine: file descriptor not registered")

ErrUnknownFD is returned when a WorkerLoop operation references a file descriptor that is not registered on that worker.

Functions

This section is empty.

Types

type AcceptController added in v0.3.0

type AcceptController interface {
	// PauseAccept stops accepting new connections. Existing connections continue.
	PauseAccept() error
	// ResumeAccept resumes accepting new connections after a pause.
	ResumeAccept() error
}

AcceptController is implemented by engines that support dynamic accept control, used by the adaptive engine to pause/resume individual sub-engines during switches.

type CapabilityProfile

type CapabilityProfile struct {
	// OS is the operating system name (e.g. "linux").
	OS string
	// KernelVersion is the full kernel version string (e.g. "5.15.0-91-generic").
	KernelVersion string
	// KernelMajor is the major kernel version number.
	KernelMajor int
	// KernelMinor is the minor kernel version number.
	KernelMinor int
	// IOUringTier is the detected io_uring capability tier (None through Optional).
	IOUringTier Tier
	// EpollAvailable is true if epoll is supported on this system.
	EpollAvailable bool
	// MultishotAccept is true if io_uring multishot accept is available (kernel 5.19+).
	MultishotAccept bool
	// MultishotRecv is true if io_uring multishot recv is available (kernel 5.19+).
	MultishotRecv bool
	// ProvidedBuffers is true if io_uring provided buffer rings are available (kernel 5.19+).
	ProvidedBuffers bool
	// SQPoll is true if io_uring SQ polling mode is available (kernel 6.0+).
	SQPoll bool
	// CoopTaskrun is true if IORING_SETUP_COOP_TASKRUN is available (kernel 5.19+).
	CoopTaskrun bool
	// SingleIssuer is true if IORING_SETUP_SINGLE_ISSUER is available (kernel 6.0+).
	SingleIssuer bool
	// LinkedSQEs is true if io_uring linked SQE chains are supported.
	LinkedSQEs bool
	// DeferTaskrun is true if IORING_SETUP_DEFER_TASKRUN is available (kernel 6.1+).
	DeferTaskrun bool
	// FixedFiles is true if io_uring fixed file descriptors are available.
	FixedFiles bool
	// SendZC is true if io_uring zero-copy send is available.
	SendZC bool
	// NumCPU is the number of logical CPUs.
	NumCPU int
	// NUMANodes is the number of NUMA nodes detected.
	NUMANodes int
}

CapabilityProfile describes the I/O capabilities detected on the current system. The probe package populates this at startup to guide engine selection.

func NewDefaultProfile

func NewDefaultProfile() CapabilityProfile

NewDefaultProfile returns a CapabilityProfile with safe defaults.

type Engine

type Engine interface {
	// Listen starts the engine and blocks until ctx is canceled or a fatal
	// error occurs. The engine begins accepting connections on the configured address.
	Listen(ctx context.Context) error
	// Shutdown gracefully drains in-flight connections. The provided context
	// controls the deadline; if it expires, remaining connections are closed.
	Shutdown(ctx context.Context) error
	// Metrics returns a point-in-time snapshot of engine performance counters.
	Metrics() EngineMetrics
	// Type returns the engine type identifier (IOUring, Epoll, Adaptive, or Std).
	Type() EngineType
	// Addr returns the bound listener address, or nil if not yet listening.
	Addr() net.Addr
}

Engine is the interface that all I/O engine implementations must satisfy. Implementations include io_uring, epoll, adaptive, and the standard library net/http server. Engine methods are safe for concurrent use after Listen is called.

type EngineMetrics

type EngineMetrics struct {
	// RequestCount is the cumulative number of requests handled by this engine.
	RequestCount uint64
	// ActiveConnections is the current number of open connections.
	ActiveConnections int64
	// ErrorCount is the cumulative number of connection-level or protocol errors.
	ErrorCount uint64
	// Throughput is the recent requests-per-second rate.
	Throughput float64
	// LatencyP50 is the 50th-percentile (median) request latency.
	LatencyP50 time.Duration
	// LatencyP99 is the 99th-percentile request latency.
	LatencyP99 time.Duration
	// LatencyP999 is the 99.9th-percentile request latency.
	LatencyP999 time.Duration
}

EngineMetrics is a point-in-time snapshot of engine-level performance counters. Each engine maintains internal atomic counters and populates a fresh snapshot on each [Engine.Metrics] call.

type EngineType

type EngineType uint8 //nolint:revive // user-approved name

EngineType identifies which I/O engine implementation is in use.

const (
	IOUring  EngineType // io_uring-based engine
	Epoll               // epoll-based engine
	Adaptive            // runtime-adaptive engine selection
	Std                 // net/http stdlib engine
)

I/O engine implementation types. The zero value is engineDefault which resolves to Adaptive on Linux and Std elsewhere (see resource.Config.WithDefaults).

func (EngineType) IsDefault added in v1.1.0

func (t EngineType) IsDefault() bool

IsDefault reports whether the engine type is the unset zero value.

func (EngineType) String

func (t EngineType) String() string

String returns the human-readable engine type name (e.g. "io_uring", "epoll").

type EventLoopProvider added in v1.4.0

type EventLoopProvider interface {
	// NumWorkers returns the number of worker event loops available for
	// driver FD registration. This is typically equal to the engine's
	// configured worker count (one per CPU core).
	NumWorkers() int
	// WorkerLoop returns the WorkerLoop for worker n, where 0 <= n < NumWorkers().
	// Calling WorkerLoop with an out-of-range index panics.
	WorkerLoop(n int) WorkerLoop
}

EventLoopProvider is implemented by engines that expose their per-worker event loops for database/cache driver integration. Drivers call WorkerLoop to obtain a specific worker and register file descriptors on it, sharing the HTTP server's epoll instance or io_uring rings.

A nil return from [celeris.Server.EventLoopProvider] (or a type assertion failure here) indicates the engine does not support driver integration (e.g., the standard net/http fallback). Drivers should fall back to the standalone mini event loop in driver/internal/eventloop in that case.

Implementations must be safe for concurrent use after the engine has begun listening.

type Protocol

type Protocol uint8

Protocol represents the HTTP protocol version.

const (
	HTTP1 Protocol // HTTP/1.1
	H2C            // HTTP/2 cleartext
	Auto           // auto-negotiate
)

HTTP protocol version constants. The zero value is protocolDefault which resolves to Auto (see resource.Config.WithDefaults).

func (Protocol) IsDefault added in v1.1.0

func (p Protocol) IsDefault() bool

IsDefault reports whether the protocol is the unset zero value.

func (Protocol) String

func (p Protocol) String() string

String returns the protocol name (e.g. "http1", "h2c", "auto").

type SwitchFreezer added in v0.3.0

type SwitchFreezer interface {
	// FreezeSwitching prevents the adaptive engine from switching.
	FreezeSwitching()
	// UnfreezeSwitching allows engine switching to resume.
	UnfreezeSwitching()
}

SwitchFreezer is implemented by the adaptive engine to allow external code (e.g., benchmarks) to temporarily prevent engine switches.

type Tier

type Tier uint8

Tier represents an io_uring capability tier. Ordering forms a strict hierarchy suitable for >= comparisons.

const (
	None     Tier = iota // no io_uring support
	Base                 // kernel 5.10+
	Mid                  // kernel 5.13+ (COOP_TASKRUN)
	High                 // kernel 5.19+ (multishot accept/recv, provided buffers)
	Optional             // kernel 6.0+ (SINGLE_ISSUER, SQPOLL)
)

io_uring capability tiers in ascending order of feature availability.

func (Tier) Available

func (t Tier) Available() bool

Available reports whether this tier represents a detected capability.

func (Tier) String

func (t Tier) String() string

String returns the tier name (e.g. "none", "base", "high").

type WorkerLoop added in v1.4.0

type WorkerLoop interface {
	// RegisterConn adds fd to the worker's interest set and installs the
	// data and close callbacks. The fd is expected to be already connected
	// and in non-blocking mode. Returns an error if fd is already registered
	// on this or another worker.
	RegisterConn(fd int, onRecv func([]byte), onClose func(error)) error
	// UnregisterConn removes fd from the worker's interest set and triggers
	// the onClose callback (with a nil error) after any in-flight operations
	// complete. The caller is responsible for closing the fd itself.
	UnregisterConn(fd int) error
	// Write enqueues data for transmission on fd. The call does not block on
	// the kernel send buffer: data is buffered and flushed asynchronously by
	// the worker. Returns [ErrQueueFull] if the worker's outbound queue is
	// saturated. The data slice may be retained by the worker until the
	// write completes; callers must not modify it after calling Write.
	Write(fd int, data []byte) error
	// CPUID returns the CPU this worker is pinned to, or -1 if the worker is
	// not pinned. Drivers may use this to co-locate allocations (e.g.,
	// NUMA-aware buffers) with the worker.
	CPUID() int
}

WorkerLoop is the per-worker surface that drivers use to register file descriptors, send data, and receive callbacks when data arrives or the connection closes.

Thread safety

All methods are safe to call from any goroutine. [Write] is serialized per-FD internally (at most one send is in flight per descriptor), following the same invariant the HTTP path relies on.

Callback semantics

The onRecv and onClose callbacks passed to [RegisterConn] are invoked from the worker's own goroutine. The []byte slice passed to onRecv is only valid for the duration of the callback — callers must copy before retaining it. onClose is called exactly once, either in response to [UnregisterConn] or when the kernel reports a closed/errored connection. The error argument to onClose is nil on orderly shutdown and non-nil on I/O errors.

Drivers must not call [RegisterConn] / [UnregisterConn] / [Write] from within an onRecv or onClose callback targeting the same FD; use a separate goroutine if such re-entrance is needed.

Directories

Path Synopsis
Package epoll implements the epoll-based I/O engine for Linux.
Package epoll implements the epoll-based I/O engine for Linux.
Package iouring implements an engine backed by Linux io_uring.
Package iouring implements an engine backed by Linux io_uring.
Package std provides an engine implementation backed by net/http.
Package std provides an engine implementation backed by net/http.

Jump to

Keyboard shortcuts

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