Repository navigation
Description
This issue is a self-contained design proposal for compile-time-linked Rust hooks in sozu-lib, in response to the original ask in #1178. It folds the maintainer reply, six design decisions, and findings from a multi-stream code review (correctness, project-guidelines conformance, simplification, security threat-hunt, and a second-model cross-check). Implementation does not start until this proposal is approved.
Implementation phasing, PR sequencing, risk register, items requiring resolution before implementation, and cross-references continue in the first comment below (the proposal is split across two posts because the full body exceeds GitHub's per-comment size limit).
Original ask: #1178.
Deployment target: kemeter/sozune — a Traefik-style higher-level reverse proxy built on top of sozu-lib. Stock sozu binary continues unchanged; Sozune ships the operator-facing middleware (basic auth, rate limit, headers, strip-prefix, redirects, compression, backend-timeout) by linking against sozu-lib and registering custom hooks.
Out of scope: WebAssembly / dynamic plugin loading. Pure Rust, compile-time-linked.
1. Context
1.1 The ask
@Shine-neko (issue #1178) wants a way to plug a function/callback at two stages of the pipeline: rewrite or modify requests (path, headers) before routing, and rewrite or modify responses (headers, status) before sending them back. @Wonshtrum (maintainer) replied that the closest existing primitives are kawa_h1::ParserCallbacks::on_request_headers / on_response_headers plus the URL-rewrite / header-injection / redirect / basic-auth feature stack that landed via PRs #1206-#1210, but these are static configuration, not operator-supplied logic. The maintainer explicitly invited a programmable surface.
1.2 The reference points
Three reference designs were studied to anchor sōzu's choices:
- Pingora (Cloudflare): a single
ProxyHttptrait with ~30 hooks, allasync fn, monomorphised global perService. Per-sessiontype CTXassociated type. Strong fit for tokio-based proxies; doesn't translate to sōzu's mio-onlylib/. - HAProxy: three pluggability surfaces — native filter modules (C, sync function pointers,
flt_opsstruct with lifecycle + channel + HTTP/TCP hooks), Lua scripting (in-VM coroutines viacore.register_actioncheap andcore.register_filterbody-streaming), and SPOE (out-of-process NOTIFY/ACK protocol for verdict-only enrichment). - Sōzu's existing primitives:
kawa_h1::ParserCallbacks::on_request_headers/on_response_headers(parser callbacks, internal),Router::route_from_request(resolves cluster + per-frontend policy), config-time per-frontend programmability (headers: Vec<Header>,redirect,rewrite_*,required_auth,answers."NNN",max_connections_per_ip).
1.3 The design decisions (operator-confirmed)
Six questions were asked, six answers locked:
| Q | Question | Decision |
|---|---|---|
| Q1 | Storage location — where the hook reference is held | (b) Listener-owned registry, frontends/clusters reference hooks by ID |
| Q2 | Sync vs async — what shape the hook signatures take | (i) Pure sync + (ii) yield-on-pending mio integration. Both ship in v1, NOT deferred to v1.5 |
| Q3 | API surface — single trait or two-tier | (b) Two-tier — HeaderHook cheap + always available, BodyHook opt-in |
| Q4 | Operator deployment shape — distribution model | (a) Pingora-style binary build — operator (Sozune) recompiles their own binary linking sozu-lib |
| Q5 | ABI stability — facade layer or direct internal exposure | Expose internal types directly — &mut HttpContext to hooks; Sozune accepts coupling |
| Q6 | Header edit type — new HeaderEdit enum or reuse existing Header |
(a) Reuse Header — operators emit Vec<Header> from hooks, same downstream pipeline |
Plus the per-stream state slot:
- TypeId-keyed
HashMaponHttpContext, lazy-allocated (Option<HashMap<TypeId, Box<dyn Any + Send + Sync>>>). Each hook's state type is its own slot — no collisions, no string IDs, no operator burden.
1.4 Hard constraints inherited from sōzu
- No
async fninlib/. mio + edge-triggered epoll, single-threaded per worker. Hooks cannot beasync fn. The yield-on-pending path satisfies operator I/O needs without breaking this rule. - No
Arc<Mutex>inside the event loop. Slab-allocated session state. Hooks see&mut HttpContextper-session; cross-session shared state belongs in&selfon the hook impl (e.g.Arc<RwLock<BanList>>). - No panic on network-facing input. Hooks must convert errors into
HookOutcome::Reject(status)orHookError.unwrap/expect/panic!forbidden in the protocol layer. - Edge-triggered epoll discipline. When a hook queues bytes for write, it MUST call
signal_pending_writeon the readiness tracker — there is no wake-up for free. - Buffer-pool-owned bodies. Body-stage hooks operate on per-session buffer-pool checkouts; chunk size cannot grow beyond the buffer slot.
- H2 stream ordering. Per-stream pending state must respect HTTP/2 framing — pending on stream 5 must NOT block stream 7 from making progress, but stream 5's response cannot go out before stream 5 finishes its hook chain.
2. Public API specification
2.1 Module layout
New module: lib/src/hooks/
lib/src/hooks/
├── mod.rs # public trait + types + builder API
├── outcome.rs # HookOutcome / WakeupSource / WakeupReason / HookError
├── registry.rs # HookRegistry — per-listener storage
├── dispatch.rs # Router-side dispatch (Phase B + C glue)
├── wakeup.rs # mio Poll integration (Phase C)
└── builtins/ # Phase E — sōzu's own middlewares as hook impls
├── mod.rs
├── basic_auth.rs
├── header_edit.rs
├── rate_limit.rs
├── redirect.rs
└── rewrite.rs
2.2 Trait surface
// lib/src/hooks/mod.rs
use std::any::Any;
use std::time::Duration;
use crate::protocol::http::editor::HttpContext;
use sozu_command::proto::command::Header;
/// Stage-1 hook — sees parsed request / response headers, can mutate
/// `HttpContext.headers_request` / `headers_response` (which feed the
/// existing `apply_request_rewrites_and_headers` /
/// `apply_response_header_edits` pipeline). Cheap path; always
/// available. Hook output is byte-validated in the dispatcher before
/// reaching the wire (see §2.7 step 6).
pub trait HeaderHook: Send + Sync + 'static {
/// Operator-supplied identifier (no default). Used as the metric
/// label suffix (`hooks.duration.<name>`) and log envelope tag —
/// must be a stable identifier (`type_name::<Self>()` includes
/// module paths and is documented as best-effort, so it is not
/// the default).
fn name(&self) -> &'static str;
/// Called after `ParserCallbacks::on_request_headers` and after
/// `Router::route_from_request` resolves the cluster.
fn on_request_headers(
&self,
_ctx: &mut HttpContext,
_wake: WakeupReason,
) -> HookResult {
Ok(HookOutcome::Continue)
}
/// Called just before `kawa.prepare(...)` on the response side, in
/// reverse scope order vs. request (cluster → frontend → listener).
fn on_response_headers(
&self,
_ctx: &mut HttpContext,
_wake: WakeupReason,
) -> HookResult {
Ok(HookOutcome::Continue)
}
}
/// Stage-2 hook — opt-in body-byte interception. Implementing this
/// trait also requires `HeaderHook` (the body stage runs after the
/// header stage of the same hook). Body hooks are gated behind a
/// listener-builder method `with_body_hook(name, Arc<dyn BodyHook>)`
/// distinct from `with_hook(...)` so the buffer-pool path is only
/// enabled for hooks that opt in.
pub trait BodyHook: HeaderHook {
/// In-place body chunk mutation. Cannot grow the chunk beyond the
/// buffer-pool slot. Hooks that shrink the chunk MUST keep
/// downstream framing consistent — sōzu auto-recomputes
/// `Content-Length` from the post-hook total on H1 (or marks the
/// response as `Transfer-Encoding: chunked` if the original CL is
/// authoritative on a different hop) before forwarding. H2:
/// post-mutation DATA-frame size drives outbound flow-control
/// debit. END_STREAM is never swapped under the operator's feet.
/// Returns `HookOutcome::Continue` to forward, `Reject(status)`
/// to abort BEFORE response bytes are emitted, `AbortStream { error }`
/// to abort mid-response, `Pending(...)` to yield.
fn on_request_body_chunk(
&self,
_ctx: &mut HttpContext,
_chunk: &mut BodyChunk<'_>,
_wake: WakeupReason,
) -> HookResult {
Ok(HookOutcome::Continue)
}
fn on_response_body_chunk(
&self,
_ctx: &mut HttpContext,
_chunk: &mut BodyChunk<'_>,
_wake: WakeupReason,
) -> HookResult {
Ok(HookOutcome::Continue)
}
}
pub struct BodyChunk<'a> {
pub bytes: &'a mut [u8],
pub end_stream: bool,
}
pub type HookResult = Result<HookOutcome, HookError>;2.3 Outcome enum
// lib/src/hooks/outcome.rs
use std::time::Duration;
use std::os::fd::RawFd;
use sozu_command::proto::command::Header;
/// Hook return value. Four terminal cases. Header edits are emitted
/// by mutating `HttpContext.headers_request` / `headers_response`
/// directly (Q6 reuses the existing `Header` plumbing); the
/// dispatcher byte-validates every emitted `Header` before it reaches
/// the wire (rejects CRLF / NUL / non-tchar in keys, C0 controls in
/// values per `parse_header_edit` in `command/src/config.rs:1397-1408`,
/// promoted to a shared crate location).
#[non_exhaustive]
pub enum HookOutcome {
/// Proceed to the next hook in the chain (or to the next pipeline
/// stage if this is the last hook).
Continue,
/// Short-circuit BEFORE response bytes have been emitted. Sōzu
/// renders the matching default answer template via
/// `set_default_answer` (`mux/answers.rs:193`); for 429 only,
/// `set_default_answer_with_retry_after` (`answers.rs:207`)
/// threading the cluster's effective retry_after via
/// `SessionManager::effective_retry_after` (`server.rs:243`).
/// `RejectStatus` is a newtype enforcing 4xx/5xx (and 301/302/308)
/// at construction; out-of-range values surface as `HookError`
/// at the dispatcher.
Reject {
status: RejectStatus,
/// Optional cause carried into the per-hook error log; not
/// rendered on the wire.
message: Option<String>,
},
/// Abort an in-flight response stream. Used when a body hook
/// detects a problem after response headers (or partial body)
/// have already been emitted. H2: emit RST_STREAM with the
/// operator's chosen H2 error code. H1: write-only shutdown
/// (NEVER `Shutdown::Both` on TLS frontends — write-only
/// shutdown avoids the TCP RST that truncates already-queued
/// response bytes); the truncated body is the best-effort
/// signal to the client.
AbortStream {
error: AbortStreamError,
},
/// Hook needs to wait for an external event. Sōzu's mio loop
/// re-enters the hook (with the same `&mut HttpContext` and the
/// hook's own state slot) when the wake source fires. `timeout`
/// is capped per worker by `hooks.max_pending_timeout_seconds`
/// (default 5 s, hard ceiling 60 s); concurrent `HookPending` per
/// worker is capped at `hooks.max_concurrent_pending_per_worker`
/// (default ≤ 32 768 to keep headroom inside the 65 536-token
/// range — see §4.1 / §4.5).
Pending {
wake: WakeupSource,
timeout: Duration,
/// Same `RejectStatus` constraint as `Reject`.
wake_timeout_status: RejectStatus,
},
}
/// Newtype over `u16` enforcing 4xx/5xx (and 301/302/308) at
/// construction. `TryFrom<u16>` returns `InvalidRejectStatusError` for
/// out-of-range values; the dispatcher converts those into
/// `HookError { status: 500, message: "invalid Reject status: <N>" }`
/// and bumps `hooks.invalid_reject_status.<hook_name>`.
pub struct RejectStatus(u16);
#[non_exhaustive]
pub enum AbortStreamError {
/// H2: emit RST_STREAM with this error code. H1: write-only shutdown.
Internal,
Cancel,
EnhanceYourCalm,
Custom(u32),
}
/// What the hook is waiting for. The mio loop registers the
/// appropriate event source on the worker's Poll.
#[non_exhaustive]
pub enum WakeupSource {
/// File descriptor became readable (e.g. a TCP socket the hook
/// opened to a sidecar Redis). Ownership transfers to sōzu on
/// `Pending` return — `EPOLL_CTL_DEL` runs in
/// `WakeupSourceHandle::Drop` before fd close, so a double-close
/// cannot race a kernel fd-reuse. Operator MUST set `O_CLOEXEC`
/// and MUST NOT register the same fd on any other Poll.
FdReadable(OwnedFd),
/// Channel message arrived. The caller must pre-register the
/// channel via `HookRegistry::register_channel(token)` at
/// listener-build time; this variant just stores the token. The
/// registry monitors `Disconnected` on the channel handle — if
/// the operator's background thread drops its `Sender`, the
/// pending hook is re-entered with `WakeupReason::Timeout` (and
/// `hooks.channel.disconnected.<hook_name>` increments) instead
/// of leaking the wake-up entry until `timeout` elapses.
ChannelReady(usize),
/// Wall-clock delay (e.g. exponential backoff between retries).
/// Integrates with the worker's existing Timer machinery.
Delay(Duration),
}
/// Passed when a hook is invoked — `Initial` on first entry,
/// `Ready` / `Timeout` on re-entry from a previous `Pending`. Hooks
/// that issue multiple Pendings across phases use `source` to
/// identify which pending operation completed.
#[non_exhaustive]
pub enum WakeupReason {
Initial,
Ready { source: WakeupSourceId },
Timeout { source: WakeupSourceId },
}
/// Opaque identifier for a `WakeupSource` returned by a `Pending`.
/// Generated at Pending-return time, plumbed through the registry,
/// surfaced on re-entry so hooks can disambiguate concurrent waits.
pub struct WakeupSourceId(NonZeroU64);
pub struct HookError {
pub status: RejectStatus,
pub message: String,
}2.4 HttpContext extension
// lib/src/protocol/kawa_h1/editor.rs (existing struct extended)
use std::any::{Any, TypeId};
use std::collections::HashMap;
pub struct HttpContext {
// ... all existing fields unchanged ...
/// Per-session, per-hook state slot. Each hook stores its state
/// keyed by its own `TypeId`, so two hooks with different state
/// types never collide. `None` until the first hook stashes
/// anything — stock sōzu deployments without hooks pay zero.
pub(crate) hooks_state: Option<HashMap<TypeId, Box<dyn Any + Send + Sync>>>,
}
impl HttpContext {
pub fn set_hook_state<T: Any + Send + Sync>(&mut self, state: T) {
self.hooks_state
.get_or_insert_with(HashMap::new)
.insert(TypeId::of::<T>(), Box::new(state));
}
pub fn hook_state<T: Any + Send + Sync>(&self) -> Option<&T> {
self.hooks_state
.as_ref()?
.get(&TypeId::of::<T>())?
.downcast_ref::<T>()
}
pub fn hook_state_mut<T: Any + Send + Sync>(&mut self) -> Option<&mut T> {
self.hooks_state
.as_mut()?
.get_mut(&TypeId::of::<T>())?
.downcast_mut::<T>()
}
pub fn take_hook_state<T: Any + Send + Sync>(&mut self) -> Option<T> {
self.hooks_state
.as_mut()?
.remove(&TypeId::of::<T>())?
.downcast::<T>()
.ok()
.map(|b| *b)
}
}Construction site impact. HttpContext is constructed at five known sites today: mux/h2.rs:7422, mux/answers.rs:358, mux/mod.rs:494 (HttpContext::new), kawa_h1/editor.rs:833 + :862 (test fixtures), kawa_h1/mod.rs:314. The new hooks_state: Option<...> field defaults to None; the explicit struct literals at the first three sites still need their fields updated in Phase A.
Debug impl. HttpContext has #[derive(Debug)] at editor.rs:115 (struct definition at editor.rs:116). Box<dyn Any> is not Debug; the derive must be replaced with a hand-rolled impl Debug for HttpContext that lists every existing field by name and renders the new field as hooks_state: Option<...> (count only). A unit test in Phase A pins format!("{:?}", ctx) is panic-free with stashed Box<dyn Any> state — error-path log dumps ({:?} at e.g. mux/router.rs:~374) must keep working. The derivative crate is NOT a workspace dep; do not add it. No Clone / PartialEq impact.
State-discipline contract. Two hooks both stashing common types (Vec<u8>, String) collide under TypeId::of::<Vec<u8>>() — last writer wins, reader silently misclassifies. Concrete failure mode: auth hook stores user ID, logging hook overwrites with fingerprint, rate-limit hook reads ctx.hook_state::<Vec<u8>>() and gets the fingerprint — per-IP rate-limit silently degrades to per-fingerprint, allowing distributed credential stuffing under a single IP. Hook state MUST be a hook-private newtype:
struct MyAuthState(Vec<u8>); // OK — distinct TypeIdlib/src/hooks/macros.rs ships a define_hook_state! macro that generates the newtype + Send/Sync bound + custom Debug (skips bytes). Documented as the only supported way to declare hook state. The registry logs a rate-limited warn! when two registered hooks call set_hook_state::<T> with the same T::TypeId.
Field-mutability matrix. Hooks see &mut HookMut (a sealed wrapper over HttpContext), NOT &mut HttpContext directly. The HookMut API gates writes on routing-frozen fields:
| Field | Hook access |
|---|---|
cluster_id, backend_id |
read-only |
method, authority, path (post-route) |
read-only |
tls_*, keep_alive, protocol |
read-only |
headers_request, headers_response |
read-write |
hooks_state (via set_/hook_state* helpers) |
read-write |
frontend_id, request_id, session_id |
read-only |
Mutating authority after routing was the explicit operator-footgun risk; the read-only gate forces hooks to emit Header { key: "Host", val: ... } edits instead, which flow through downstream re-validation (see §2.7 step 7).
Backing store. §3 estimates 1–3 stashed states per request. For that size, Vec<(TypeId, Box<dyn Any + Send + Sync>)> with linear scan is cache-friendlier than HashMap (no hash, no drop overhead, smaller). The four convenience methods (set_/hook_state*) keep their signatures; backing store is an implementation detail.
2.5 Builder API
// lib/src/protocol/listener.rs (or HttpListener / HttpsListener equivalent)
use std::sync::Arc;
use crate::hooks::{HeaderHook, BodyHook, HookRegistry};
impl HttpListener {
/// Register a header-stage hook by name. The name is the ID
/// frontends and clusters reference in their `hook_ids: Vec<String>`.
/// Must equal `hook.name()` — mismatch returns
/// `HookRegisterError::NameMismatch`. Registering twice with the
/// same name returns `HookRegisterError::Duplicate`. The registry
/// also runs the state-discipline lint (§2.4) and emits a `warn!`
/// when two hooks declare the same `TypeId` for their state slot.
pub fn with_hook(
&mut self,
name: impl Into<String>,
hook: Arc<dyn HeaderHook>,
) -> Result<&mut Self, HookRegisterError>;
/// Register a body-stage hook. Implies header-stage registration
/// at the same name (since `BodyHook: HeaderHook`).
pub fn with_body_hook(
&mut self,
name: impl Into<String>,
hook: Arc<dyn BodyHook>,
) -> Result<&mut Self, HookRegisterError>;
}2.6 Proto schema additions
// command/src/command.proto
message RequestHttpFrontend {
// ... existing fields ...
repeated string hook_ids = 16; // pick the next available tag at edit time
}
message Cluster {
// ... existing fields ...
repeated string hook_ids = 16; // pick the next available tag at edit time
}Validation: at config-load time AND at runtime AddHttpFrontend / AddCluster, every hook_id must resolve in the listener's registry. Unknown ID → reject the request with a typed HookConfigError carrying the offending ID, do not silently accept.
Cluster scope on a listener-owned registry — open question. Cluster is a global resource; AddCluster carries no listener context. Resolution of where the cluster's hook_ids get validated (worker-global registry vs. validation deferred to AddHttpFrontend time when a frontend pulls a cluster) is deferred to a dedicated planning session before Phase A starts. v1 keeps cluster hook scope per Q1 — only the validation site is to be decided. Tracked at §8.1 item 2.
Cargo-clean dependency. The workspace's prost map-field derive handling can leave stale generated code after a command.proto edit; run cargo clean -p sozu-command-lib && cargo build -p sozu-command-lib once after editing hook_ids tags before the next workspace build.
Cross-version safety. prost skips unknown fields silently → an older sōzu worker deserialising a config with hook_ids set silently treats the frontend as no-hook (degrade-open). For v1 this is the documented contract; future work may add a WorkerCapabilities advertisement so the master can refuse-application on mixed-version partial upgrades.
2.7 Dispatch order
The dispatch helpers in lib/src/hooks/dispatch.rs are called from Router::connect (mux/router.rs:90 → :141), NOT from inside Router::route_from_request (which would require widening that function's return type). Both H1 and H2 reach Router::connect through the unified mux dispatch path (HTTP/1.1 listeners flow through HttpStateMachine::Mux(MuxClear)), so a single hook insertion point covers both protocols.
Naming note. The "listener-post-route" chain fires AFTER route_from_request returns Ok — chosen for v1 because rewriting path/authority and then re-routing is a deeper change to Router than the wave can absorb. v2 may add a true pre-route stage that runs before route_from_request. The original issue #1178 ask for "rewrite path/headers before routing" is therefore deferred — operators can mutate via headers_request edits but those do not change the routing decision in v1.
Request side (fires AFTER existing config-time programmability):
1. ParserCallbacks::on_request_headers (sōzu invariants — Host validation, CL/TE reconciliation)
2. Router::route_from_request (config-time: redirect, auth, rewrite_*)
3. listener-post-route hook chain (listener.hook_ids on the listener config)
4. frontend hook chain (frontend.hook_ids)
5. cluster hook chain (cluster.hook_ids)
6. Hook output validation: every Header emitted by hooks 3-5 is byte-validated
(rejects CRLF / NUL / non-tchar in keys, C0 in values) by the helper
promoted from command/src/config.rs:1397-1408 (parse_header_edit) into a
shared crate location. Invalid bytes → HookError { status: 500 }, bump
hooks.invalid_header.<hook_name>.
7. Invariant re-validation: re-fire CL/TE consistency, Host single-value, no
forbidden-byte check on the post-hook header set. Mandatory whenever the
hook chain emitted any header edit. Failure → HookError { status: 500 }.
8. Backend connect
Trailer-frame coverage. pkawa::handle_trailer (lib/src/protocol/mux/pkawa.rs:1038) is plumbed through the same hook chain (steps 3-7) so trailer headers do not bypass listener-scoped policy. Concrete plumbing shape (a second invocation with a trailer-flag in WakeupReason, vs. a separate hook entry-point on HeaderHook) is settled at Phase B implementation time.
Response side (reverse):
1. Backend response received
2. cluster hook chain (response variant, reverse iteration)
3. frontend hook chain (response variant)
4. listener-post-route hook chain (response variant)
5. Hook output validation (same byte-validation gate as request side)
6. Invariant re-validation (response variant)
7. ParserCallbacks::on_response_headers + existing config-time response edits
8. kawa.prepare → wire bytes
Outcome routing:
- First
Continueaccumulates header edits and proceeds. Header-edit ordering across scopes is deterministic last-writer-wins in dispatch order (listener → frontend → cluster on request, reversed on response). - First
Reject(status)short-circuits viaset_default_answer(mux/answers.rs:193) — orset_default_answer_with_retry_after(answers.rs:207) for 429 specifically, threadingSessionManager::effective_retry_after(server.rs:243). Reject is valid only BEFORE response bytes have been emitted. ARejecton the response side after:statushas been queued is converted toAbortStream { error: Internal }and awarn!is logged so the hook author sees the misuse. - First
AbortStream { error }aborts an in-flight response. H2: emit RST_STREAM with the operator's chosenH2ErrorCode. H1: write-only shutdown (NEVERShutdown::Bothon TLS frontends — write-only shutdown avoids the TCP RST that truncates already-queued response bytes). - First
Pending(...)parks the session via the wake-up registry (Phase C). Subject to per-worker caps (§4.5).
2.8 Panic-safety contract
&mut HookMut exposed to a hook is not naturally UnwindSafe (it wraps interior pointers, kawa storage). The dispatcher wraps every hook call in catch_unwind. On Err, the dispatcher MUST:
- Clear
ctx.headers_requestandctx.headers_responsepost-route snapshots (the panicking hook may have left them half-populated). - Drop
ctx.hooks_stateentirely (the panicking hook may have left dangling invariants in its own slot). - Emit
hooks.panicked.<hook_name>(rate-limited counter — operators see drift; the worker stays up). - Route to
set_default_answerwith status 500. - Mark the session for close after the default answer is written.
Phase A includes a regression test that panics from a HeaderHook mid-mutation and asserts no partial bytes reach the wire. Workspace Cargo.toml commits to panic = "unwind" so a future change is a deliberate decision the hook subsystem can react to. "Worker should not crash" is the load-bearing operator posture.
3. Per-stream state slot — semantics + cost
3.1 Lookup pattern
Each hook stashes state at TypeId::of::<MyHookPrivateState>() and reads it back at the same key. Two hooks with different nominal state types occupy different slots; common-type collisions (Vec<u8> × Vec<u8>) are an operator footgun (see §2.4 "State-discipline contract" — newtype mandatory; the registry warns on TypeId duplication at registration time).
References into per-stream kawa storage MUST NOT be stashed; only owned bytes (a Vec<u8> newtype, a String newtype, etc.). Buffer-pool slots are per-stream; aliasing across streams is undefined behaviour. The define_hook_state! macro in lib/src/hooks/macros.rs enforces the newtype + the Send/Sync bound at compile time.
3.2 Cost analysis
Steady-state per request: ~5-10 hook calls (3 scopes × 1-3 middlewares). Each hook access does 1 TypeId comparison + 1 HashMap lookup + 1 downcast = single-digit nanoseconds. Compared to TLS handshake (~ms) or backend connect (~ms), invisible.
Memory: each stashed state boxed once. Sozune-typical mix (rate-limit verdict, auth result, compression negotiation) = 3-5 boxed allocations per request, ~100 bytes. Negligible.
When hooks are unused (stock sōzu, no Sozune-style binary): hooks_state: None — one extra usize on HttpContext, zero allocations.
3.3 Why TypeId beats alternatives
| Alternative | Rejected because |
|---|---|
Make HttpContext generic over Extra |
Refactor every call site. Sozune wants several middleware-specific state types, not one big struct. |
Single untyped Box<dyn Any> slot |
Only one hook can use it. Two middlewares collide silently. |
Pingora-style type CTX associated type |
Sōzu's hook trait is dyn-dispatched. Associated types don't survive type erasure. |
| Slab-keyed slot ID (operator manages a numeric ID per hook) | Operator burden. TypeId map gets the same lookup cost without manual ID tracking; if profiling shows hot, swap the backing store. |
4. Yield-on-pending wake-up subsystem (Phase C)
The most complex sub-system. Documented here in full because it touches the mio loop directly.
4.1 Token namespace
The worker's mio Poll currently allocates tokens for: front sockets (slab-keyed, low range), back sockets (slab + offset), command channel (fixed token), timer (fixed token), health-check probes (carved-out range [1<<24, 1<<24 + 1<<16), with the assertion at lib/src/health_check.rs:55-56 that session tokens stay capped well below 1<<24).
Hooks claim a fresh range:
const HOOKS_TOKEN_BASE: usize = 1 << 25;
const HOOKS_TOKEN_CAPACITY: usize = 1 << 16; // 65 536 tokensAllocation via a modulo allocator that skips in-flight tokens, refusing allocation rather than colliding. The wake-up registry caps active entries at min(HOOKS_TOKEN_CAPACITY / 2, hooks.max_concurrent_pending_per_worker) so a peer cannot exhaust the token range — refused allocations bump hooks.pending.refused_cap and the hook is rejected with HookError { status: 500 }. Verify before Phase A lands that max_connections * slab_entries_per_connection < (1 << 24) still holds with hooks adding pressure.
4.2 Registry
// lib/src/hooks/wakeup.rs
struct HookWakeupEntry {
session_id: SessionId,
stream_id: Option<StreamId>, // Some on H2, None on H1 / TCP
chain_position: ChainPosition, // listener-post-route[i], frontend[j], cluster[k]
direction: HookDirection, // request | response
hook: Arc<dyn HeaderHook>,
/// The fd or channel handle registered on the Poll. Owned here
/// so its lifetime is bounded by the registry; `Drop` runs
/// `EPOLL_CTL_DEL` (for fds) or `disconnect` (for channels)
/// before the source is closed, eliminating fd-reuse races.
source: WakeupSourceHandle,
/// Identifier surfaced on re-entry as
/// `WakeupReason::Ready { source }` so hooks issuing multiple
/// Pendings across phases can disambiguate.
source_id: WakeupSourceId,
deadline: Instant,
wake_timeout_status: RejectStatus,
}
pub struct HookWakeupRegistry {
entries: HashMap<Token, HookWakeupEntry>,
/// Reverse index: per-(session_id, stream_id) so RST_STREAM and
/// session-close sweeps run in O(active-pending-for-stream)
/// without walking all entries.
by_stream: HashMap<(SessionId, Option<StreamId>), SmallVec<[Token; 4]>>,
next_token: Token, // modulo allocator
active_count: AtomicUsize,
max_active: usize,
}4.3 Re-entry path
When the mio loop returns an event with a token in the hooks range:
- Look up the
HookWakeupEntry. If absent (already cancelled by RST_STREAM or session close), drop the event without bumping any counter. - Resolve
(session_id, stream_id)against the slab. If gone (session closed mid-pending), drop the entry + bumphooks.cancelled_pending. - If alive: borrow
&mut HttpContext, re-invoke the hook withWakeupReason::Ready { source: source_id }(orTimeout { source }from the timer path). The hook may return:Continue— proceed to the next chain step (or pipeline stage).Reject(status)— convert viaset_default_answer(answers.rs:193) for non-429 codes,set_default_answer_with_retry_afterfor 429. If response bytes have already been emitted (response-side wake), convert toAbortStream { error: Internal }andwarn!.AbortStream { error }— emit RST_STREAM (H2) or write-only shutdown (H1).Pending(...)— re-register on a new token; deadline accumulates against the originaltimeout-cap. A second-Pending afterTimeoutre-entry is rejected withwake_timeout_statusto bound starvation.
Edge-triggered epoll discipline. On any path that queues bytes for write — header-edit application, default-answer write, AbortStream RST emission — the dispatcher MUST call signal_pending_write on the readiness tracker before returning to the loop, per §1.4 invariant 4. Edge-triggered epoll does not deliver a wake-up event for free when readiness is computed from the read path; forgetting this produces "stuck session" bugs that only manifest at exact byte boundaries.
4.4 Cancellation
Three cancellation paths:
- Session close.
SessionManager::on_session_closecallsHookWakeupRegistry::sweep_session(session_id), which uses theby_streamreverse index to find every entry for that session in O(active-pending-count-for-session) and drops them. Source handles drop → fds get unregistered from the Poll, channels disconnect their readiness signal. Counterhooks.cancelled_pendingincrements per swept entry. - H2 RST_STREAM on a hook-pending stream.
mux::h2::handle_rst_stream(or the equivalent pkawa path) callsHookWakeupRegistry::sweep_stream(session_id, stream_id)synchronously, BEFORE the H2 flood detector glitch counter sees the close. Late-firing wakes for that stream return "already cancelled" at step 1 of §4.3 and drop without bumpingglitch_count— preserving the existingH2FloodDetectorposture (the detector currently counts WINDOW_UPDATE on closed streams as a glitch, so race conditions between hook resume and stream close must be cleaned synchronously). - Channel sender drop. When the operator's background thread panics and drops its
mpsc::Sender, the registry detectsDisconnectedon the channel handle and re-enters the hook withWakeupReason::Timeoutimmediately (no need to wait for the configuredtimeout). The hook chooses to short-circuit or retry.hooks.channel.disconnected.<hook_name>increments.
4.5 Timeout enforcement
Every Pending carries timeout: Duration. The dispatcher caps this at hooks.max_pending_timeout_seconds (worker-config, default 5 s, hard ceiling 60 s) — operator-supplied timeouts above the cap are rejected with HookError { status: 500, message: "Pending timeout exceeds cap" } and hooks.pending.timeout_capped.<hook_name> increments. The worker's existing Timer machinery (the same one that drives front_timeout / back_timeout) registers a Timeout event at now + min(timeout, cap).
If the Timeout fires before a Ready wake, the hook is re-invoked with WakeupReason::Timeout { source }. If it returns Pending again, sōzu rejects with wake_timeout_status to bound starvation — the configured fallback. Per-worker concurrent-Pending count is also capped at hooks.max_concurrent_pending_per_worker (default ≤ 32 768) to keep headroom inside the 65 536-token range; exceeding the cap returns HookError { status: 500 } at registration without entering Pending state.
4.6 H2 stream ordering invariant
Per-stream pending state lives on the H2 connection's Stream slot in mux::stream. Hook-pending is a flag on Stream, NOT a new variant of StreamState. The actual StreamState enum at lib/src/protocol/mux/stream.rs:35 is a linkage state machine (Idle / Link / Linked(Token) / Unlinked / Recycle); RFC 9113 §5 protocol state lives elsewhere (front_received_end_of_stream, back_received_end_of_stream, per-stream readiness on the H2 Connection). Adding HookPending to StreamState would lose the backend Token carried by Linked(Token).
The shape:
// lib/src/protocol/mux/stream.rs (extended)
pub struct Stream {
pub state: StreamState,
// ... existing fields ...
pub hook_pending: Option<HookPendingTag>,
}
pub struct HookPendingTag {
pub token: Token, // index into HookWakeupRegistry
pub direction: HookDirection,
pub started_at: Instant,
}Transitions: hook_pending = Some(_) on HookOutcome::Pending; cleared on hook re-entry returning anything other than Pending; cleared on RST_STREAM / session close. StreamState itself is untouched — Linked(backend_token) still carries its backend identity through a hook-pending window.
The connection-level scheduler skips streams with hook_pending.is_some() and rotates to ready streams. Response order is preserved by the existing FIFO bookkeeping that sequenced :status emission per RFC 9113 §5.
LIFECYCLE.md gets a new section: "Hook-pending streams and the response-order invariant" — written against the actual StreamState enum (linkage states), NOT the RFC-9113 abstract names.
4.6.1 Read-side flow control while hook-pending
While a request-side hook is parked (hook_pending.is_some()), the connection MUST NOT WINDOW_UPDATE the client for that stream and MUST NOT forward DATA payloads to a backend (which may not yet be connected — request hook chain fires after route_from_request but before backend connect, per §2.7). Without this gate, a hostile (or merely fast) client streaming the request body during a hook park is a memory-pressure DoS surface multiplied by concurrent connections.
Concretely:
mux::h2::handle_data_frame(lib/src/protocol/mux/h2.rs:4264) treats hook-pending streams as suspended for forwarding; received DATA accumulates againstmax_request_body_sizeand trips 413 on overflow. No WINDOW_UPDATE is emitted.- After resume (
Continueclearshook_pending), the dispatcher emits the deferred WINDOW_UPDATE and resumes the read path. - After resume (
Reject/AbortStream), the read path closes via the standard short-circuit; deferred bytes are discarded. - For response-side
hook_pending, the read path (request body) is unaffected — only the write path is suspended.
4.7 Channel-fed background-thread pattern
The canonical I/O workaround for "I need to consult Redis":
- Operator's Sozune binary spawns a background thread holding the Redis connection.
- Background thread maintains an in-memory snapshot (
Arc<RwLock<BanList>>). - When a hook needs the snapshot fresh, it sends a request via a
mpsc::Sender, then returnsPending { wake: WakeupSource::ChannelReady(channel_token), timeout: Duration::from_secs(1), wake_timeout_status: RejectStatus::SERVICE_UNAVAILABLE }. - Background thread queries Redis, sends the verdict back via the channel.
- Hook re-enters with
WakeupReason::Ready { source }, reads the verdict fromctx.hook_state::<RedisVerdict>(), emitsContinueorReject.
Sender-drop semantics. If the background thread panics and the Sender half drops, the registry detects Disconnected on the channel handle and re-enters the hook with WakeupReason::Timeout immediately — no waiting for timeout to elapse. The hook then chooses to short-circuit or retry. hooks.channel.disconnected.<hook_name> increments. Phase C ships an e2e cell "background-thread-panics-during-pending" to pin this contract.
Sozune's middleware crates would ship this pattern as a generic utility (SozuneAsyncResolver<T>) so individual middlewares don't reimplement it. Documented as an example in doc/programmable-hooks.md, NOT shipped inside sozu-lib.
4.8 What's NOT in scope of Phase C
- Per-listener thread-pool for hook I/O (would break
lib/no-async by introducing a tokio runtime). Operators doing thread-pool I/O run their pool in their Sozune binary, send results via the channel pattern above. - Cross-worker coordination of pending state (each worker's wake-up registry is independent). A pending hook on worker 1 doesn't block worker 2; same as sōzu's existing per-worker isolation.
Activity
FlorentinDUBOIS commentedon May 2, 2026
Continuation of the design proposal above — this comment carries the implementation phasing, PR sequencing, risk register, items requiring resolution before implementation starts, and cross-references. The two posts together form the complete self-contained proposal.
5. Phased implementation plan
Six phases, sequenced. Phases A → B → C are the critical path; D/E/F can parallelise after C.
Phase A — Trait module + registry skeleton (no dispatch)
Goal: public surface compiles, registered hooks are inert, stock sōzu binary unchanged.
Lands:
lib/src/hooks/{mod.rs, outcome.rs, registry.rs, macros.rs}— trait + types + per-listener registry +define_hook_state!macro.lib/src/hooks/mod.rs::log_module_context!—HOOKtag, mirroring theMUX-ROUTERoverload pattern atmux/router.rs:46-68(()and($http_context)arms callingHttpContext::log_context()). Directlog::*!calls insidelib/src/hooks/are forbidden bylib/tests/log_layout.rs; new entry added to that test's allowlist OR the new module joins the canonical envelope.HttpContext.hooks_statefield (Vec<(TypeId, Box<dyn Any + Send + Sync>)>, lazyNone) + 4 convenience methods + hand-rolledimpl Debug for HttpContext(replaces the existing#[derive(Debug)]ateditor.rs:115; lists every existing field by name and rendershooks_statecount-only). Update the 5 known construction sites (mux/h2.rs:7422,mux/answers.rs:358,mux/mod.rs:494,kawa_h1/editor.rs:833 + :862,kawa_h1/mod.rs:314).HookView/HookMutsealed wrappers overHttpContext(per §2.4 field-mutability matrix).HttpListener::with_hook(name, Arc<dyn HeaderHook>)+with_body_hookbuilder methods. Operator-suppliedname()is required (no default).RequestHttpFrontend.hook_ids = 16,Cluster.hook_ids = 16(verify next available tag at edit time). Runcargo clean -p sozu-command-libonce after the proto edit.AddHttpFrontendruntime validation: everyhook_idresolves in the listener's registry. Cluster-on-listener-registry validation site is the open question deferred to a dedicated session before this phase starts (§8.1 item 2).Cargo.toml: commit topanic = "unwind"on the relevant profiles (default is unwind on Linux x86_64; this commits to it explicitly so the hook subsystem'scatch_unwindworks in release and a future change topanic = "abort"is a deliberate decision the hook subsystem can react to). The hook dispatcher wraps every hook call incatch_unwind(Phase B); panic-recovery contract is in §2.8 (rolled-back state, 500 default-answer, session close).lib/src/hooks/macros.rs::define_hook_state!— generates a hook-private newtype +impl Debug(skips bytes) + Send/Sync bound. Documented as the only supported way to declare hook state.Excluded: dispatch, wake-up, body hooks, builtins.
Tests:
format!("{:?}", ctx)is panic-free with stashedBox<dyn Any>state.define_hook_state!macro expansion +Debugskip behaviour.HOOKlog envelope passinglib/tests/log_layout.rs.Estimate: ~500 LoC (incl. hand-rolled
Debug, macros,HookView/HookMut, log macro). ~1.5 days. One commitfeat(hooks): trait module + per-listener registry skeleton. UpdatesCLAUDE.md(HOOK tag in the canonical log-context list,hooksfeature flag),CHANGELOG.md(first## [Unreleased]block, plain### Added), anddoc/configure.md(no metrics yet, buthooks.max_pending_timeout_seconds/hooks.max_concurrent_pending_per_workerconfig knobs are reserved here for Phase C). Panic-safety contract details live in §2.8.Phase B — Sync dispatch (Continue / Reject / AbortStream)
Goal: hooks fire on every request, can mutate
HookMut-exposedHttpContextfields, can short-circuit with a status code, can abort an in-flight response.HookOutcome::Pendingis rejected at hook registration time withHookRegisterError::PendingRequiresPhaseCif the hook impl advertises Pending capability — thePendingvariant compiles in the trait surface but cannot actually fire in PR1 (operator footgun avoidance — shipping aPendingthat silently means 503 violates "no panic on network-facing input" in spirit).Lands:
lib/src/hooks/dispatch.rs— request + response dispatch helpers, called fromRouter::connect(NOT from insideroute_from_request). Single dispatch site shared by H1 and H2 through the unified mux entry point; per-cluster metrics fire from the dispatcher only, satisfying H1/H2 metric parity by construction.Router::connectextension: listener-post-route → frontend → cluster chain on request, reverse on response.Headeremitted by a hook is byte-validated by the helper promoted fromcommand/src/config.rs:1397-1408(parse_header_edit's name + value validators) into a shared crate location. Invalid bytes →HookError { status: 500 }, bumphooks.invalid_header.<hook_name>. Phase B test: CRLF in hook-emitted header → 500 on the wire, no\r\nbytes leak.hooks.max_emitted_header_bytes_per_chain(default 2 KB total). Exceeding →HookError { status: 500 }, bumphooks.headers.bytes_added.<hook_name>andhooks.headers.budget_exceeded.<hook_name>.pkawa::handle_trailer(lib/src/protocol/mux/pkawa.rs:1038) so trailer headers do not bypass listener-scoped policy.catch_unwindwrap per hook call + the §2.8 rollback path (clear post-route header snapshots, drophooks_state, route toset_default_answer(500), close session).Reject(status)routing throughset_default_answer(mux/answers.rs:193);set_default_answer_with_retry_after(answers.rs:207) for 429 only, threadingSessionManager::effective_retry_after(server.rs:243).AbortStream { error }routing: H2 emits RST_STREAM with the operator's chosenH2ErrorCode; H1 write-only shutdown (NEVERShutdown::Bothon TLS frontends).hooks.duration.<hook_name>) + chain-level cumulativehooks.chain.cumulative_duration(alarms on chain-level p99/p999, default 5 ms budget — exceeding bumpshooks.chain.budget_exceededand short-circuits withHookError { status: 503 }).Tests: 4-cell
protocol_pair_matrix!× 5 scenarios (no-op hook, header-injection hook, reject-with-403 hook, abort-stream-mid-response hook, CRLF-in-emitted-header → 500 hook) = 20 e2e cells. All TCP-response assertions useloop_read_until_eofper project CLAUDE.md. Plus 5 unit tests on the dispatch helper, theRejectStatusTryFromrejection path, the byte-validator promotion, the panic-recovery rollback, and the chain-budget short-circuit.Estimate: ~1000 LoC (the rollback path, byte validator, HPACK accounting, and trailer plumbing add ~200 LoC over the original estimate). ~2 days. One commit
feat(hooks): sync dispatch with Continue/Reject/AbortStream; PendingRequiresPhaseC. UpdatesCHANGELOG.md(first## [Unreleased]block, plain### Added) anddoc/configure.md(hooks.duration.<name>,hooks.invalid_header.<name>,hooks.headers.bytes_added.<name>,hooks.headers.budget_exceeded.<name>,hooks.chain.cumulative_duration,hooks.chain.budget_exceeded,hooks.panicked.<name>).Merge target is the integration branch agreed at PR-creation time. Use plain
git merge --ff-only <branch>from that target.Phase C — Yield-on-pending wake-up subsystem
Goal: hooks can return
HookOutcome::Pending(...)and sōzu re-enters them when the wake source fires, respecting H2 stream ordering, session-close + RST_STREAM cancellation, the read-side flow-control gate, and per-worker resource caps.Lands:
lib/src/hooks/wakeup.rs— registry + Poll integration + cancellation (3 paths: session close, RST_STREAM, channel sender-drop) + timeout enforcement (capped per worker) + per-worker concurrent-Pending cap.[HOOKS_TOKEN_BASE, HOOKS_TOKEN_BASE + HOOKS_TOKEN_CAPACITY)(= [1<<25, 1<<25 + 1<<16));active_countcapped atmin(HOOKS_TOKEN_CAPACITY / 2, hooks.max_concurrent_pending_per_worker).WakeupSource::FdReadable(OwnedFd)(transfers ownership;EPOLL_CTL_DELonWakeupSourceHandle::Dropbefore fd close).WakeupReason::{Ready { source }, Timeout { source }}+WakeupSourceIdplumbed through the registry so hooks issuing multiple Pendings can disambiguate.Stream.hook_pending: Option<HookPendingTag>flag (NOT a newStreamStatevariant —StreamStatestaysIdle/Link/Linked(Token)/Unlinked/Recycle). Scheduler skips streams withhook_pending.is_some(). BackendTokencarried byLinked(Token)is preserved across the pending window.mux::h2::handle_data_frame(h2.rs:4264) treats hook-pending streams as suspended for forwarding; received DATA accumulates againstmax_request_body_sizeand trips 413 on overflow. No WINDOW_UPDATE while parked. Deferred WINDOW_UPDATE emits on resume.mux::h2::handle_rst_streamcallsHookWakeupRegistry::sweep_stream(session_id, stream_id)synchronously BEFORE flood-detector glitch counting, so late wakes return "already cancelled" and do NOT bumpglitch_count(preservingH2FloodDetector's existing closed-stream-WU-as-glitch posture).Disconnectedon registered channel handles; re-enters hook withWakeupReason::Timeoutimmediately on disconnect.LIFECYCLE.mdupdate — "Hook-pending streams and the response-order invariant" section, written against the actualStreamStateenum (linkage states), NOT the RFC-9113 abstract names.SozuneAsyncResolver<T>documented as an example indoc/programmable-hooks.md(NOT shipped insozu-lib).Tests: 8 e2e cells (h2-h1, h2-h2, plus 6 hot-paths) — hook-with-channel-wake, hook-timeout, session-close-during-pending, H2 stream-5-pending-doesn't-block-stream-7, peer-RST_STREAM-on-HookPending-no-flood-trip, background-thread-panics-during-pending, request-body-flowing-during-HookPending-respects-413, FdReadable-double-close-race-impossible. All TCP-response assertions use
loop_read_until_eof. Plus 8 unit tests on the registry, scheduler, by-stream sweep, channel-disconnect detection, timeout cap, OwnedFd drop ordering, WakeupSourceId disambiguation, and the per-worker concurrent-Pending cap rejection path.Risk: highest in this plan. Hot-path mio change. Three review passes (implementer + independent reviewer + final auditor); the same discipline applies to Phase B and Phase D given e2e cell counts and H2 framing risk. Re-run reviews after each fix commit until a clean pass surfaces no new finding — complex protocol fixes routinely expose adjacent issues.
Estimate: ~1700 LoC (read-side flow-control gate + by-stream reverse index + sender-drop detection + per-worker caps add ~200 LoC over the original). ~3.5 days. Two commits —
feat(hooks): wake-up subsystem+test(hooks): yield-on-pending e2e + LIFECYCLE.md. UpdatesCHANGELOG.md(first## [Unreleased]block) anddoc/configure.md(hooks.cancelled_pending,hooks.pending.refused_cap,hooks.pending.timeout_capped.<name>,hooks.channel.disconnected.<name>). Merge target is the integration branch agreed at PR-creation time.Phase D — Body-stage opt-in (
BodyHook)Goal: hooks that opt in via
with_body_hook(...)see request / response body chunks before they hit the wire.Lands:
BodyHooktrait extendingHeaderHook.BodyChunk { bytes: &mut [u8], end_stream: bool }shape.kawa.prepare. Buffer slot lower bound is 16393 bytes when H2 is enabled (CLAUDE.md gotcha); a Phase D unit test pins this.mux::h2::flush_stream_out, called fromwrite_streamsath2.rs:2255). Highest framing risk. Must respect frame size limits, flow control credits, END_STREAM ordering. Post-mutation byte total drives outbound credit accounting (NOT the original payload length).Content-Lengthfrom the post-hook total on H1 (or marks the responseTransfer-Encoding: chunkedand recomputes if the original CL was authoritative on a different hop) before forwarding. H2: post-mutation DATA-frame size drives the outbound flow-control debit. END_STREAM is NEVER swapped under the operator's feet — final-flag preservation guards in the converter remain authoritative. Defends against theCL.TE-style smuggling primitive that a naive shrink without recomputation creates.AbortStream { error }routing: H2 emits RST_STREAM with the operator's chosen error code; H1 write-only shutdown.Tests: 4-cell × 4 scenarios (body header-strip on request, body bytes-mutation on response, body-shrink-with-Content-Length-auto-recompute, body-stage-Reject-after-header-Continue-cleans-up-state) = 16 e2e cells. All TCP-response assertions use
loop_read_until_eof. Plus 6 unit tests on the H2 DATA-frame path (frame size, credit accounting, END_STREAM preservation, post-shrink CL coherence, body-stage RST_STREAM emission, fuzz target on the hook-mutated chunk path).Estimate: ~700 LoC (length reconciliation adds ~100 LoC). ~2.5 days. One commit
feat(hooks): body-stage opt-in BodyHook. UpdatesCHANGELOG.md(first## [Unreleased]block).Phase E — Built-in middleware ports
Goal: sōzu's existing config-time programmability (basic auth, redirect, rewrite, header-edit, rate-limit) is reimplemented as
HeaderHookimpls inlib/src/hooks/builtins/. Sozune can compose them, override them, or replace them with custom impls.Lands: five sub-modules under
hooks::builtins::, each gated behind a feature flag (hooks-builtin-*):basic_auth.rscluster.authorized_hashes+www_authenticateheader_edit.rsVec<Header>(request + response variants)rate_limit.rsredirect.rsRedirectPolicy::{Permanent, Found, PermanentRedirect, Unauthorized}rewrite.rsrewrite_host/rewrite_path/rewrite_portThe existing config-driven path stays the default. A migration path lets operators flip a frontend / cluster from "config-time policy" to "hook-driven policy" by setting
hook_ids = ["basic-auth"]instead ofrequired_auth = true. Both shapes coexist.Tests: existing e2e tests for each feature (e.g.
redirect_rewrite_auth_tests.rs) gain a parallel test pointing at the hook-driven path. Same fixtures, same assertions.Estimate: ~800 LoC across five sub-modules. ~2 days. Five commits, one per built-in.
Phase F — Operator documentation
Goal: operator-facing docs. Per "Docs stay current," each preceding phase already updated
CHANGELOG.md,doc/configure.md,LIFECYCLE.md, andCLAUDE.mdin the SAME PR — Phase F holds the operator guide only.Lands:
doc/programmable-hooks.md— operator guide. Sections: trait surface, builder API,HookOutcomesemantics (Continue/Reject/AbortStream/Pending),RejectStatusconstraints, hook output byte-validation contract, the field-mutability matrix (HookView/HookMut), the state-discipline newtype rule with thedefine_hook_state!macro, the channel-fed background-thread pattern (SozuneAsyncResolver<T>example), per-stream state slot semantics, panic-safety contract (§2.8), opt-out for stock builds, migration from config-time policy.doc/upgrade/1.x-to-2.0.mdaddendum (or a fresh1.x-to-3.0.mdif hooks ship in v3.0) — note that hooks are additive; cross-version contract forhook_ids(degrade-open on older workers).CHANGELOG.md— final harmonisation pass (target the FIRST## [Unreleased]block per the dual-Unreleased convention; per-phase entries already added in Phases A-E).e2e/COVERAGE.mdmatrix — new "hook scope" axis covering(listener-post-route × frontend × cluster) × (sync × yield-on-pending) × (header × body).RELEASE.md— note hooks ship behind--features hooksso binary-built operators (Sozune) opt in explicitly.Estimate: ~2 days. One commit
docs(hooks): operator guide + matrix axis + RELEASE note. Merge target is the integration branch agreed at PR-creation time.6. PR sequencing
Three pull requests, each independently reviewable + mergeable. Each PR opens only after maintainer approval of the corresponding phase; the merge target is the integration branch agreed at PR-creation time. Commit messages and PR descriptions inline the technical rationale and do not reference "Phase A/B/C" or "see plan" — those labels are private to this document and would not survive review without context.
Stream.hook_pendingfield.hooks-builtin-*feature flags), operator docs. Body hooks ship after the wake-up subsystem so body-stage Pending works from day one.Total: ~7-8 commits across 3 PRs, ~5000 LoC (incl. byte-validation gate, panic-recovery, read-side flow-control, RST sweep, length reconciliation), ~50 new tests.
Calendar: 3-4 weeks of focused work, contingent on review pace.
7. Risk register
Cargo.tomlcommits topanic = "unwind". Dispatcher wraps every hook call incatch_unwind. On panic: clear post-route header snapshots, drophooks_state, bumphooks.panicked.<hook_name>(rate-limited), route toset_default_answer(500), mark session for close. Phase A regression test asserts no partial bytes reach the wire after a mid-mutation panic. Documented operator contract: hooks SHOULD NOT panic; sōzu defends so the worker stays up. See §2.8.Stream.hook_pendingflag (NOT a newStreamStatevariant); read-side flow-control gate per §4.6.1; LIFECYCLE.md invariant; e2e cells cover stream-5-pending-doesn't-block-stream-7 + RST-on-HookPending-no-flood-trip + body-flowing-while-parked-respects-413; auditor pass on Phase C.parse_header_editruns at the dispatcher (§2.7 step 6). Invalid bytes →HookError { status: 500 }. Phase B test pins no\r\nreaches the wire.HookOutcome::Reject { status: u16 }accepts arbitrary codes (200 / 1xx / 6xx)RejectStatusnewtype withTryFrom<u16>enforcing 4xx/5xx (and 301/302/308). Out-of-range surfaces asHookError { status: 500 }and bumpshooks.invalid_reject_status.<hook_name>.HookView/HookMutfield-mutability matrix. Routing-decided fields (cluster_id,backend_id,authority,path,method,tls_*) are read-only to hooks. Edits flow throughheaders_request/headers_responseand re-validate.hooks.max_emitted_header_bytes_per_chain). Exceeding fails closed before HPACK abort, bumpinghooks.headers.budget_exceeded.<hook_name>.pkawa::handle_trailer:1038; trailer headers see the same chain as initial HEADERS. Phase B test: trailer-injectedAuthorizationblocked.Content-Lengthon shrink (or switches to chunked); end_stream guards remain authoritative; framing test matrix; auditor pass; fuzz target on the hook-mutated chunk path.HookOutcome::Pendingwith unbounded timeout pins per-stream resources (DoS amplification)hooks.max_pending_timeout_secondsworker-config (default 5 s, hard ceiling 60 s);hooks.max_concurrent_pending_per_workercap (default ≤ 32 768 of 65 536 token range); refused allocations bumphooks.pending.refused_cap.hooks.cancelled_pendingcounter; e2e regression tests for each path.hooks.duration.<hook_name>histogram + chain-levelhooks.chain.cumulative_duration. 5 ms chain budget short-circuits withHookError { status: 503 }and bumpshooks.chain.budget_exceeded. p99 > 10 mswarn!on individual hooks.Mutexand deadlocks the workerArc<RwLock<_>>read-guard held acrossroute_from_requestborrows starves unrelated streamsVec<u8>,String)define_hook_state!macro generates them; registry warns when two hooks declare the sameTypeId. Operator guide highlights the footgun.Senderdrops mid-pending (background thread panics) → wake never firesDisconnectedon channel handles; re-enters hook withWakeupReason::Timeoutimmediately. Phase C e2e cell covers it.0.xuntil v1; Sozune pinssozu-libversions. No facade layer.hook_idssilently treats frontend as no-hookWorkerCapabilitiesadvertisement could refuse-application instead.AddCluster/AddHttpFrontendagainst the registry; typedHookConfigErrorcarries the offending ID.hooks_statestorageVec<(TypeId, _)>is per-session; freed on session close. No cap needed.RawFdownership confusion / double-close raceWakeupSource::FdReadable(OwnedFd)transfers ownership;EPOLL_CTL_DELruns inWakeupSourceHandle::Dropbefore fd close. Operator must setO_CLOEXEC.8. Open questions deferred to v2
These were considered and intentionally NOT in this plan's scope:
max_connections_per_ip. Cross-worker sync (Redis broadcast or master-fanout) is a separate design pass, only worth doing when an operator hits the limitation in production.lib/, breaking the no-async invariant. Operators using a thread pool host it in their Sozune binary, communicate via channels — same pattern as the Redis example.documentation/middleware/) goes beyond what sōzu has historically supported; the matching builtins (Phase E) cover the existing primitives only. Compression / strip-prefix / backend-timeout / etc. ship in Sozune's own crates linking theHeaderHook/BodyHooktraits.WasmHookimpl ofHeaderHook(the wasm runtime is the operator-supplied layer, sōzu just sees aHeaderHookimpl).route_from_request) because rewriting and re-routing is a deeper change toRouterthan the wave can absorb. v2 may add a pre-route stage; v1 operators who need this can pre-process at the listener level via custom config or accept the post-route limitation.8.1 Open items requiring resolution before Phase A
These need a decision before code lands. They are NOT silent gaps — each is tracked in the §7 risk register row that names the relevant mitigation.
Clusteris global;AddClustercarries no listener context. Resolution of where clusterhook_idsget validated (worker-global registry vs. validation deferred toAddHttpFrontendtime when a frontend pulls a cluster) is the subject of a dedicated planning session before Phase A. v1 keeps cluster scope per Q1; only the validation site is to be decided.pkawa::handle_trailer. The exact wiring (a second invocation of the dispatcher with a trailer-flag inWakeupReason, vs. a separate hook entry-point onHeaderHook) is settled at Phase B implementation time. Decision: plumb (per user q5).AbortStream { error }is the response-side abort path;Rejectafter response bytes are emitted is converted toAbortStream { error: Internal }with awarn!(operator misuse signal).hook_idscross-version contract. Resolved as v1 documented degrade-open. FutureWorkerCapabilitiesadvertisement is v2 work.HttpContextownership. Project memory says each H2 stream owns a distinctHttpContext; one of the security findings was unable to fully verify the current state. 5-minute confirmation againstlib/src/protocol/mux/stream.rsandmux/h2.rs:7422(the H2 HttpContext construction site) before Phase A lands.Cargo.tomlcommits topanic = "unwind"; the dispatcher wraps every hook call incatch_unwind; the §2.8 rollback contract is mandatory. "Worker should not crash" is the load-bearing posture.9. Cross-references
documentation/middleware/).lib/src/protocol/kawa_h1/editor.rs—HttpContextdefinition.pub struct HttpContextateditor.rs:116;#[derive(Debug)]ateditor.rs:115(replaced by hand-rolledimpl Debugin Phase A). Re-exported ascrate::protocol::http::editor::HttpContextviaprotocol/mod.rs:21. Construction sites:mux/h2.rs:7422,mux/answers.rs:358,mux/mod.rs:494,kawa_h1/editor.rs:833 + :862,kawa_h1/mod.rs:314.lib/src/protocol/mux/router.rs::connect— dispatch entry point for Phase B (line ~90 → ~141), wrapsroute_from_request(line ~540). Both H1 and H2 reachconnectthrough the unified mux dispatch path.lib/src/protocol/mux/answers.rs::set_default_answer(line 193) for non-429 codes;::set_default_answer_with_retry_after(line 207) for 429 only.lib/src/protocol/mux/stream.rs:35— actualStreamStateenum (Idle/Link/Linked(Token)/Unlinked/Recycle); hook-pending is a separateOption<HookPendingTag>field onStream, NOT aStreamStatevariant.lib/src/protocol/mux/pkawa.rs:1038—handle_trailer; hook chain is plumbed through here for trailer-frame coverage.lib/src/protocol/mux/h2.rs:4264—handle_data_frame; read-side flow-control gate lands here for hook-pending streams.lib/src/protocol/mux/h2.rs:5152— closed-stream glitch counting; per-stream RST sweep runs synchronously before this path.lib/src/protocol/mux/LIFECYCLE.md— H2 stream state machine documentation; gains a "Hook-pending streams and the response-order invariant" section in Phase C (against the actualStreamStateenum).lib/src/health_check.rs:55-56— assertion that session tokens stay below1<<24; verify still holds with hooks adding pressure.command/src/config.rs:1397-1408—parse_header_editbyte-validator; promoted to a shared crate location and reused at the hook dispatcher.lib/src/metrics/mod.rs— metric macros (incr!,count!,gauge!,time!); updatedoc/configure.mdwhen adding/renaming a public metric.ProxyHttptrait (pingora-proxy/src/proxy_trait.rs:32incloudflare/pingora) — reference design for the hook surface.flt_ops(include/haproxy/filters-t.h:160-203inhaproxy/haproxy) — reference design for the per-stream state pattern.Plan originally written 2026-05-02 in response to operator decisions on the six design questions. Revised 2026-05-02 to fold in 15 must-fix findings from a five-stream review (correctness, guidelines, simplification, security, second-model cross-check) plus operator decisions on panic strategy (
panic = "unwind"workspace-wide),AbortStreamfor response-side reject, trailer-frame plumbing, and cluster scope retention. Outstanding items are tracked in §8.1; resolution required before Phase A starts. Implementation does NOT start until this revised plan is approved.