resource

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package resource defines server configuration, resource limits, and default presets. The top-level celeris.Config is the primary user-facing type; this package provides the internal representation used by engine implementations.

Index

Constants

View Source
const (
	// MinWorkers is the minimum allowed number of I/O workers.
	MinWorkers = 2
	// MaxSQERing is the maximum io_uring submission queue ring size.
	MaxSQERing = 65536
	// MinBufferSize is the minimum per-connection I/O buffer size in bytes.
	MinBufferSize = 4096
	// MaxBufferSize is the maximum per-connection I/O buffer size in bytes.
	MaxBufferSize = 262144
)

Resource limit constants for validation and clamping.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Protocol is the HTTP protocol version (HTTP1, H2C, or Auto).
	Protocol engine.Protocol
	// Engine is the I/O engine type (IOUring, Epoll, Adaptive, or Std).
	Engine engine.EngineType
	// Addr is the TCP address to listen on (e.g. ":8080").
	Addr string
	// Resources holds worker, buffer, and connection limit overrides.
	Resources Resources
	// MaxHeaderBytes is the max header block size in bytes (min 4096 if set).
	MaxHeaderBytes int
	// MaxConcurrentStreams limits simultaneous H2 streams per connection.
	MaxConcurrentStreams uint32
	// MaxFrameSize is the max H2 frame payload size (range 16384-16777215).
	MaxFrameSize uint32
	// InitialWindowSize is the H2 initial stream flow-control window size.
	InitialWindowSize uint32
	// ReadTimeout is the max duration for reading the entire request.
	ReadTimeout time.Duration
	// WriteTimeout is the max duration for writing the response.
	WriteTimeout time.Duration
	// IdleTimeout is the max duration a keep-alive connection may be idle.
	IdleTimeout time.Duration
	// DisableKeepAlive disables HTTP keep-alive.
	DisableKeepAlive bool
	// Listener is an optional pre-existing listener for socket inheritance.
	Listener net.Listener
	// MaxRequestBodySize is the maximum allowed request body size in bytes.
	// 0 uses the default (100 MB). -1 disables the limit (unlimited).
	MaxRequestBodySize int64
	// AsyncHandlers dispatches HTTP handlers to spawned goroutines instead
	// of inline execution on LockOSThread'd workers. See celeris.Config.
	AsyncHandlers bool
	// OnExpectContinue is called when an H1 request contains "Expect: 100-continue".
	// If the callback returns false, the server responds with 417 Expectation Failed
	// and skips reading the request body. If nil, the server always sends 100 Continue.
	OnExpectContinue func(method, path string, headers [][2]string) bool
	// OnConnect is called when a new connection is accepted.
	OnConnect func(addr string)
	// OnDisconnect is called when a connection is closed.
	OnDisconnect func(addr string)
	// Logger is the structured logger for engine diagnostics (default slog.Default()).
	Logger *slog.Logger
	// EnableH2Upgrade enables RFC 7540 §3.2 HTTP/1.1→H2C upgrades. Resolved
	// from celeris.Config.EnableH2Upgrade (pointer, may be nil) and Protocol.
	// Always a concrete value after WithDefaults.
	EnableH2Upgrade bool
	// SkipBuiltinScaler suppresses the per-engine dynamic worker scaler
	// loop. Set by the adaptive engine when it constructs its sub-engines —
	// adaptive runs ONE higher-level scaler that delegates to the active
	// sub-engine, so the iouring + epoll built-in scalers must stay quiet
	// to avoid two scalers fighting over the same worker pool.
	SkipBuiltinScaler bool
	// WorkerScaling configures the dynamic worker scaler. Nil disables the
	// scaler (default — workers stays at Resources.Workers and never adapts).
	// When set, the scaler activates and deactivates workers based on
	// observed load to keep CQE/event batching density in the sweet spot.
	// See WorkerScalingConfig for tuning details.
	WorkerScaling *WorkerScalingConfig
}

Config holds the internal server configuration used by engine implementations. Users typically interact with the top-level celeris.Config instead.

func (Config) Validate

func (c Config) Validate() []error

Validate checks all config fields and returns any validation errors.

func (Config) WithDefaults

func (c Config) WithDefaults() Config

WithDefaults returns a copy of Config with zero-value fields set to sensible defaults.

type ResolvedResources

type ResolvedResources struct {
	// Workers is the resolved number of I/O worker goroutines.
	Workers int
	// SQERingSize is the io_uring submission queue size (power of 2).
	SQERingSize int
	// BufferPool is the number of pre-allocated I/O buffers.
	BufferPool int
	// BufferSize is the resolved per-connection I/O buffer size in bytes.
	BufferSize int
	// MaxEvents is the max events returned per epoll_wait call.
	MaxEvents int
	// MaxConns is the resolved max connections per worker.
	MaxConns int
	// SocketRecv is the resolved SO_RCVBUF size.
	SocketRecv int
	// SocketSend is the resolved SO_SNDBUF size.
	SocketSend int
}

ResolvedResources contains the final computed values after applying defaults, user overrides, and hard caps. Used by engine implementations at startup.

type Resources

type Resources struct {
	// Workers is the number of I/O worker goroutines (0 = GOMAXPROCS).
	Workers int
	// BufferSize is the per-connection I/O buffer size in bytes (0 = engine default).
	BufferSize int
	// SocketRecv is the SO_RCVBUF size for accepted connections (0 = OS default).
	SocketRecv int
	// SocketSend is the SO_SNDBUF size for accepted connections (0 = OS default).
	SocketSend int
	// MaxConns is the max simultaneous connections per worker (0 = unlimited).
	MaxConns int
}

Resources allows user overrides of default resource values. Zero values mean "use engine default".

func (Resources) Resolve

func (r Resources) Resolve() ResolvedResources

Resolve applies hardcoded defaults, user overrides, and hard caps.

type WorkerScalingConfig added in v1.4.1

type WorkerScalingConfig struct {
	// Strategy picks the seed-state strategy. Zero value is
	// ScalingStrategyStartHigh, which is the data-validated default for
	// most production workloads. See WorkerScalingStrategy for tuning.
	Strategy WorkerScalingStrategy
	// MinActive is the floor on the active worker count. The scaler will
	// never reduce active workers below this. Defaults to max(2, NumCPU/2).
	// Set to NumCPU to force the scaler to always run at full capacity
	// (effectively a static-w=NumCPU configuration).
	MinActive int
	// TargetConnsPerWorker is the active-worker scaling target. The scaler
	// computes desired = ceil(activeConns / TargetConnsPerWorker), clamps
	// to [MinActive, NumWorkers], and steers active toward that. Default 20.
	// Higher values keep more conns per worker (better batching, less
	// parallelism). Lower values prefer parallelism over batching.
	TargetConnsPerWorker int
	// Interval controls how often the scaler reevaluates active count.
	// Default 250ms. Lower values respond to load changes faster but burn
	// more CPU on the controller goroutine.
	Interval time.Duration
	// ScaleUpStep is the maximum number of workers the scaler will
	// resume per tick. Default 2 — wider bursts disrupt SO_REUSEPORT
	// load balancing more than they help. Bigger values are tempting on
	// SPIKE workloads but produce worse throughput per the v1.4.1
	// SPIKE-test sweep (upStep=4 and upStep=8 both lost to upStep=2).
	ScaleUpStep int
	// ScaleDownStep is the maximum number of workers the scaler will
	// pause per tick when load drops. Default 1 — scale-down too quickly
	// and you can't recover throughput when load comes back.
	ScaleDownStep int
	// ScaleDownHysteresis adds a buffer between desired and active
	// before scale-down fires: scale-down only if desired ≤ active -
	// ScaleDownHysteresis - 1. Default 1 (so a desired-of-N triggers
	// scale-down only when active is N+2 or higher).
	ScaleDownHysteresis int
	// ScaleDownIdleTicks is how many consecutive sub-threshold ticks
	// must pass before a single scale-down step fires. Default 4
	// (= 1 second at the default 250ms interval). Tunes how patient
	// the scaler is about temporary lulls: a request-rate dip of one
	// tick will not trigger scale-down.
	ScaleDownIdleTicks int
	// Trace logs every scaler decision (active, desired, idle_ticks).
	// Default false. Use when diagnosing scaling behaviour.
	Trace bool
}

WorkerScalingConfig controls the dynamic worker scaler used by the iouring, epoll, and adaptive engines. Zero values mean "use the scaler's default" — see field comments for what those are. Pass via celeris.Config.WorkerScaling to enable; nil disables the scaler entirely (the engine runs all configured workers all the time, like versions before the scaler was introduced).

The scaler tracks the engine's activeConns counter and adjusts the number of "active" workers (workers participating in the SO_REUSEPORT group) so that conns / active is roughly TargetConnsPerWorker. Scale-up is reactive (next tick after a load increase). Scale-down is hysteretic — must observe ScaleDownIdleTicks consecutive ticks below the hysteresis threshold before reducing one worker.

type WorkerScalingStrategy added in v1.4.1

type WorkerScalingStrategy int

WorkerScalingStrategy selects the seed strategy for the dynamic worker scaler. The zero value (ScalingStrategyStartHigh) is the recommended default: start at NumWorkers active, scale down once load is observably low. This preserves SO_REUSEPORT distribution at startup, which the spike-B sweep showed is dramatically better on ramp / oscil traffic patterns (+34-78 % across all three engines).

const (
	// ScalingStrategyStartHigh seeds the scaler at NumWorkers active.
	// Best for production where traffic ramps from idle and bursts.
	// Zero value — selected when the field is unset.
	ScalingStrategyStartHigh WorkerScalingStrategy = 0
	// ScalingStrategyStartLow seeds the scaler at MinActive. Best when
	// the application has a long idle warmup before any conns arrive
	// and saving CPU during that idle period matters more than peak
	// throughput on the first burst.
	ScalingStrategyStartLow WorkerScalingStrategy = 1
)

Jump to

Keyboard shortcuts

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