Skip to content

perf(socket): FrontRustls::socket_read issues one EAGAIN recv per H2 frame while plaintext is already buffered #1588

Closed

Description

@FlorentinDUBOIS

Site

FrontRustls::socket_read (lib/src/socket.rs:588-640 at b5169d44, read_tls at :622) calls ServerConnection::read_tls on every entry, before it looks at the plaintext rustls has already decrypted. The H2 read path (lib/src/protocol/mux/h2.rs:8034) reads frame by frame: a 9-byte header, then the payload. One TLS record usually carries several frames, so every call after the first finds the plaintext it needs already buffered, yet still issues a recv(2) that returns EAGAIN.

Measurement

Release build of origin/main b5169d44, one worker, 20 requests to a local backend, syscalls counted with an LD_PRELOAD fd interposer and with intentrace -p on the worker:

scenario recv/req recv EAGAIN/req
H2 TLS, one connection per request 19.85 12.85
H2 TLS, 20 requests multiplexed on one connection 10.30 6.50

Of the 234 recv that returned EAGAIN in the H2 per-connection window that was attributed by backtrace, 206 come from this one call site, in runs of 4 to 5 consecutive calls: one per frame after the first frame of a record.

Proposed fix

rustls's Reader::read returns WouldBlock only when no plaintext is buffered (rustls-0.23.45/src/conn.rs:193), and wants_read() is false while plaintext is pending (src/common_state.rs:674-684). So:

  1. drain the already-decrypted plaintext first;
  2. call read_tls only when that leaves the caller's buffer unfilled;
  3. after a read_tls that returned bytes without producing plaintext (partial record), loop and read again, exactly as today.

This is what tokio-rustls does (poll_fill_buf only reads TLS while wants_read()), and what OpenSSL/HAProxy do implicitly (SSL_read serves buffered records without touching the BIO).

EOF, close_notify, SocketResult and the edge-triggered readiness contract must stay unchanged: every EAGAIN still ends the call with WouldBlock, which update_readiness turns into "drop READABLE until the next event". A separate "WouldBlock seen this readiness turn" memory would need the mux to reset it on every READABLE event; whether it saves anything on top of the reordering is to be measured before adding it.

Expected gain (estimate, to be verified): about -9.8 syscalls per request for H2 one-connection-per-request, -4.65 for multiplexed H2.

Activity

  1. added 4 commits that reference this issue on Sep 26, 2026
    a76edbd
    6829200
    94a8cd4
    4b11f3f
  2. added a commit that references this issue on Sep 26, 2026
    97a9799
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions