Skip to content

perf: skip the WebSocket handshake probe without an Upgrade header - #55

Merged
lesnik512 merged 1 commit into
mainfrom
perf/skip-ws-probe-without-upgrade
Oct 17, 2026
Merged

lesnik512 merged 1 commit into
mainfrom
perf/skip-ws-probe-without-upgrade

Conversation

@lesnik512

Copy link
Copy Markdown
Member

The middleware chose the child's scope by running web.WebSocketResponse().can_prepare(request) on every request. For a plain HTTP request that probe builds a WebSocketResponse, then builds, raises and catches an HTTPBadRequest, because the first check in aiohttp's _handshake is that Upgrade equals websocket.

The probe now runs only when the request has an Upgrade header. Without one the result was already "not a WebSocket", so the scope chosen for every request is the same as before. Any request that does carry the header is still decided by aiohttp's own check, as ADR-0002 requires. The ADR gets one sentence about the skip.

Measured by calling the middleware directly with a mocked GET request, median of 7 runs x 50k calls, run twice:

middleware us per request
main 3.99
branch 1.30

The new test wraps web.WebSocketResponse in a mock and asserts that a plain GET never constructs one. It fails on main with assert 1 == 0. The existing WebSocket tests cover the upgrade path.

@lesnik512
lesnik512 merged commit 2513af2 into main Oct 17, 2026
12 checks passed
@lesnik512
lesnik512 deleted the perf/skip-ws-probe-without-upgrade branch October 17, 2026 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant