Repository navigation
fix: evict least-active sessions when accept queue is full - #1192
Merged
Merged
Conversation
llenotre
reviewed
Mar 11, 2026
|
|
||
| /// Evict the least recently active sessions to make room for new connections. | ||
| /// Returns the number of sessions evicted. | ||
| fn evict_least_active_sessions(&self, count: usize) -> usize { |
Contributor
There was a problem hiding this comment.
This function uses two collect, making two memory allocations. Moreover, its time complexity is O(n) where n is current number of sessions, making it very inefficient.
And since it is called for each new connection, it might have a disastrous impact on performance.
Collaborator
Author
There was a problem hiding this comment.
You are right, I have updated the code, besides, as it run only when the flag is true, and by default it is false (the defensive choice), I think it should okay.
llenotre
reviewed
Mar 11, 2026
llenotre
left a comment
Contributor
There was a problem hiding this comment.
Likely performance issues
FlorentinDUBOIS
force-pushed
the
fix/accept-queue-eviction
branch
from
April 28, 2026 14:20
b9011bb to
165fe6e
Compare
Add an opt-in `evict_on_queue_full` knob (proto `ServerConfig` field
22, default `false`) that, when set, evicts the oldest 1% of
non-listener sessions to make room for queued sockets when
`SessionManager::check_limits` refuses a new accept.
Selection uses `select_nth_unstable_by_key` (introselect, O(n)
average) over `Session::last_event()` to partition the candidate
slab in place, avoiding an O(n log n) sort. The cap loop in
`Server::create_sessions` keeps the existing
`incr!("listener.connection_capped")` counter (so dashboards stay
meaningful regardless of eviction outcome), runs the eviction
batch, and re-checks limits before continuing. A new
`sessions.evicted` counter is emitted only when the mitigation
fires.
Eviction is deliberately skipped during graceful `shutting_down`:
forcing sessions closed there defeats the shutdown semantics and
is wasted work since the worker is winding down anyway.
Default `false` because during a DDoS the active sessions are more
likely to be legitimate clients than the queue contents — evicting
them would serve attackers. Operators dominated by normal traffic
spikes can flip it on.
Closes #644, closes #916.
Signed-off-by: Florentin Dubois <[email protected]>
…h small max_connections The eviction batch is `(max_connections / 100).max(1)` — a 1% ratio by design. Below 100 connections the floor of 1 means each cap event evicts a larger share than 1% of capacity (~4% at max_connections=25, ~10% at 10), which can surprise an operator who reads the knob as "1% per round". Mirror the `basic_auth_max_credential_bytes` "33% of buffer_size" pattern: emit a `warn!` from `ConfigBuilder::into_config` when both knobs would interact, so the discrepancy surfaces at boot rather than under traffic. No hard-error — operators on tiny test configs may legitimately want eviction. Signed-off-by: Florentin Dubois <[email protected]>
…evicted Add the `evict_on_queue_full` row to the global Parameters table in `doc/configure.md`, the `sessions.evicted` counter row to the metrics section, and a Keep-a-Changelog `### Added` entry under `## [Unreleased]` (the main-scoped Unreleased section, ordered before the existing `### Security` per Keep-a-Changelog convention). The doc row calls out the small-`max_connections` warning behaviour so operators don't have to read source to discover when the 1% ratio drifts. Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
force-pushed
the
fix/accept-queue-eviction
branch
from
April 29, 2026 07:33
165fe6e to
04c3edd
Compare
FlorentinDUBOIS
added a commit
that referenced
this pull request
May 2, 2026
The CHANGELOG previously carried three concurrent Unreleased blocks — `## [Unreleased]` (main-scoped post-1.1.1 fixes from PRs #1162, #1191, #1192, #1193, #1194, #1196, #1202, #1223), `## feat/answer-templates-rewrite-auth - Unreleased` (answer templates, URL rewrite, basic auth), and `## feat/h2-mux - Unreleased` (the H2 multiplexer rewrite from PR #1209). All three feature stacks are on `main`; the per-branch dividers were release- engineering staging that no longer reflects current state. Changes: - Drop the `## feat/answer-templates-rewrite-auth - Unreleased` and `## feat/h2-mux - Unreleased` dividers; their bodies now appear under `## [Unreleased]` with provenance markers (`### Added — Answer templates / ...`, `### Added — H2 multiplexer (PR #1209)`). - Promote the breaking changes (proto-tag renumbers, dashboard metric renames, `http.errors` labelling, pattern-trie regex anchoring, empty-val del-header semantic, sub-16393 `buffer_size` rejection, cipher-list constant rename) to a top-level `### Breaking changes (v1.1.x → v2.0.0)` paragraph at the top of the section so operators see them first. - Drop emoji from top-level subheaders (🌟 → Added, 🔄 → Changed, ⛑️ → Fixed, ✍️ → Changed, 📚 → Documentation, 🤖 → CI,⚠️ → Behaviour-affecting). Per- commit-log subsection at depth ≥ 4 keeps minor emoji ornaments where they don't conflict with Keep-a-Changelog vocabulary. - Preserve historical release entries (`## 1.1.1`, `## 1.1.0`, …) verbatim; they are released and immutable. Net effect: a single `## [Unreleased]` block ready for the v2.0.0 release-cut in §3.4.c of `tasks/todo.md`. Future passes can de-duplicate the multiple `### Added`/`### Changed`/`### Fixed` sub-blocks into one each per Keep-a-Changelog strict shape; this commit is the structural collapse only. Refs: tasks/todo.md §3.1.a; codex M12 amendment (3-way not 2-way collapse). Signed-off-by: Florentin Dubois <[email protected]>
jrcribb
pushed a commit
to jrcribb/sozu
that referenced
this pull request
Aug 10, 2026
…viction fix: evict least-active sessions when accept queue is full
jrcribb
pushed a commit
to jrcribb/sozu
that referenced
this pull request
Aug 10, 2026
The CHANGELOG previously carried three concurrent Unreleased blocks — `## [Unreleased]` (main-scoped post-1.1.1 fixes from PRs sozu-proxy#1162, sozu-proxy#1191, sozu-proxy#1192, sozu-proxy#1193, sozu-proxy#1194, sozu-proxy#1196, sozu-proxy#1202, sozu-proxy#1223), `## feat/answer-templates-rewrite-auth - Unreleased` (answer templates, URL rewrite, basic auth), and `## feat/h2-mux - Unreleased` (the H2 multiplexer rewrite from PR sozu-proxy#1209). All three feature stacks are on `main`; the per-branch dividers were release- engineering staging that no longer reflects current state. Changes: - Drop the `## feat/answer-templates-rewrite-auth - Unreleased` and `## feat/h2-mux - Unreleased` dividers; their bodies now appear under `## [Unreleased]` with provenance markers (`### Added — Answer templates / ...`, `### Added — H2 multiplexer (PR sozu-proxy#1209)`). - Promote the breaking changes (proto-tag renumbers, dashboard metric renames, `http.errors` labelling, pattern-trie regex anchoring, empty-val del-header semantic, sub-16393 `buffer_size` rejection, cipher-list constant rename) to a top-level `### Breaking changes (v1.1.x → v2.0.0)` paragraph at the top of the section so operators see them first. - Drop emoji from top-level subheaders (🌟 → Added, 🔄 → Changed, ⛑️ → Fixed, ✍️ → Changed, 📚 → Documentation, 🤖 → CI,⚠️ → Behaviour-affecting). Per- commit-log subsection at depth ≥ 4 keeps minor emoji ornaments where they don't conflict with Keep-a-Changelog vocabulary. - Preserve historical release entries (`## 1.1.1`, `## 1.1.0`, …) verbatim; they are released and immutable. Net effect: a single `## [Unreleased]` block ready for the v2.0.0 release-cut in §3.4.c of `tasks/todo.md`. Future passes can de-duplicate the multiple `### Added`/`### Changed`/`### Fixed` sub-blocks into one each per Keep-a-Changelog strict shape; this commit is the structural collapse only. Refs: tasks/todo.md §3.1.a; codex M12 amendment (3-way not 2-way collapse). Signed-off-by: Florentin Dubois <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
evict_least_active_sessions()toSessionManagerthat selects the oldest 1% of non-listener sessions bylast_event()and closes them when the accept queue is fullevict_on_queue_fullconfig option (default:true) toServerConfigcreate_sessions()when connection limits are reachedReview fixes applied
shut_down_sessions_by_frontend_tokenssort_by_keywith O(n)select_nth_unstable_by_keyfor the hot pathcan_accept = truebypass → propercheck_limits()re-validation after evictiontest_max_connectionse2e test by disabling eviction in test configCloses #644, closes #916
Test plan
cargo test --workspacepassescargo clippy --workspacecleantest_max_connections