resource

package
v1.5.5 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 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.

Key exported symbols:

Documentation

Full guides and examples: https://goceleris.dev/docs/configuration

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

func DeriveMemoryLimit added in v1.5.4

func DeriveMemoryLimit(workers int) int64

DeriveMemoryLimit returns a generous soft heap ceiling for `workers` I/O workers: max(256 MiB, workers*32 MiB). It is sized HIGH on purpose — the goal is to clip the connection-ramp balloon, not to run the heap tight (a too-tight limit GC-thrashes under load). Operators that want the peak-RSS reduction pass this into Resources.MemoryLimitBytes; it is never applied implicitly.

func NextPowerOf2 added in v1.5.0

func NextPowerOf2(v int) int

NextPowerOf2 returns the smallest power of two ≥ v. Exposed for engine packages that need to size io_uring rings, provided buffer rings, and similar power-of-two kernel structures from a base count derived from user-supplied config.

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
	// ReadHeaderTimeout is the max duration for reading the request
	// headers ONLY (status line + headers + final CRLF). Zero falls
	// back to ReadTimeout. A short value here is the canonical defence
	// against slowloris-style DoS: clients that dribble headers byte-
	// by-byte get their connection killed within ReadHeaderTimeout
	// instead of holding a goroutine + listener-backlog slot for the
	// full ReadTimeout. The std engine wires this to
	// http.Server.ReadHeaderTimeout. The iouring + epoll engines
	// enforce it inside their H1 header read loop.
	//
	// Note: iouring/epoll's own SO_REUSEPORT-fronted multi-worker
	// design absorbs a lot of slowloris pressure through queue
	// scaling (16 workers × 4096 backlog ≈ 64 k slots). Std is on
	// a single net.Listen() queue and falls over much sooner. The
	// fix matters most for std but is sound on all engines.
	ReadHeaderTimeout 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
}

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
	// WorkloadHint is an OPTIONAL operator concurrency declaration; the only
	// input that can make the adaptive engine START on io_uring (see WorkloadHint).
	WorkloadHint WorkloadHint
	// MemoryLimitBytes is an OPTIONAL soft heap ceiling (runtime/debug
	// SetMemoryLimit). 0 = unset: celeris does not touch the process GC, so
	// embedders keep full control. When >0, the server applies it at start —
	// the GC then collects before the heap balloons during a connection-ramp
	// burst (with the default GOGC=100 the live-heap-doubling ramp spike is
	// the dominant peak-RSS contributor), trading a few extra GC cycles
	// during the ramp for a lower high-water mark; steady-state RSS sits far
	// below the limit so steady throughput is unaffected. NOTE: SetMemoryLimit
	// is PROCESS-GLOBAL, so this is opt-in only — set it just when celeris
	// owns the process (e.g. a dedicated server binary). See DeriveMemoryLimit.
	MemoryLimitBytes int64
}

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 WorkloadHint added in v1.5.3

type WorkloadHint int

WorkloadHint is an OPTIONAL operator declaration of the expected steady-state concurrency, consumed only by the adaptive engine's start-engine decision.

Because established connections cannot migrate between epoll and io_uring, the START engine decides keep-alive throughput; and the expected concurrency is unknowable at Listen() time (no connections exist yet). This hint is the ONLY input that can make the adaptive engine START on io_uring. Absent it, the engine defaults to epoll (every server ramps from zero connections — the low-concurrency regime where epoll wins on both throughput and tail latency) and lets the runtime switch route NEW connections up to io_uring under load.

const (
	// WorkloadUnspecified (zero value) leaves the start-engine choice to the
	// default policy: start epoll, promote new conns to io_uring under load.
	WorkloadUnspecified WorkloadHint = iota
	// WorkloadLowConcurrency explicitly declares thin/latency-sensitive traffic
	// — start (and stay) on epoll.
	WorkloadLowConcurrency
	// WorkloadHighConcurrency explicitly declares many h1 keep-alive
	// connections per worker — start on io_uring (when kernel + memlock allow).
	WorkloadHighConcurrency
)

Jump to

Keyboard shortcuts

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