perf: enable TCP_NODELAY on sockets - #4336
Merged
Merged
Conversation
adriangb
added a commit
to adriangb/sqlx
that referenced
this pull request
Aug 21, 2026
Correctness: - Windows writes idle and interval together through `SIO_KEEPALIVE_VALS` and cannot leave one of them alone, so an unset one was being sent as 0 ms rather than left at the system default. Substitute libpq's own Windows values there (2h idle, 1s interval), as `pqSetKeepalivesWin32` does. - Normalize the parameters in one place, `TcpKeepalive::normalized()`: zero means "system default", and fractional durations round up to whole seconds. `socket2` truncates, so a sub-second value reached `setsockopt()` as a literal 0, which Linux rejects with `EINVAL`; only the URL parser normalized before, leaving both builders (and MySQL, which has no URL form) exposed. - A value the kernel rejects now fails with `Error::Configuration` naming the parameters instead of a bare `Io(EINVAL)` that mentions neither. - `?keepalives_idle=0` on its own left keepalive off, because 0 was folded to "unset" before deciding whether any parameter had been given. Any of the three now turns keepalive on, as in libpq. - Negative values clamp to "system default" as libpq's `strtol` path does, rather than failing to parse. - `build_url()` emits the keepalive parameters, so they survive `to_url_lossy()` alongside `sslmode` and `statement-cache-capacity`. - Raise the workspace `tokio` floor to 1.27, the first release with `AsFd`/`AsSocket` on `TcpStream`, which `SockRef::from()` requires. Docs: - Correct the platform-support paragraph: `idle` is silently dropped on OpenBSD, Haiku and Vita, and `retries` fails the connection rather than being ignored where `TCP_KEEPCNT` is missing. - List the four URL parameters in the `PgConnectOptions` table with a note on the semantics, and say on the MySQL side that no URL form exists. - Cite transact-rs#4336 for `TCP_NODELAY` rather than transact-rs#3055, which was reverted by transact-rs#4022. - Correct the socket2 dependency comment: `tokio` only bundles socket2 0.6 from 1.47 onwards. Tests: - Exercise the `setsockopt()` path on a loopback socket, reading the values back and covering the zero, sub-second and kernel-rejected cases. Only the builder and URL parsing were covered before, so none of the above was observable. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Hi @dmitryvk, many thanks for the fix; this looks more like a regression, currently with the latest version:
But the fix makes it behave like before; I caught this while testing with (https://github.com/nbari/dbpulse/) @abonander any idea when this will be released? |
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.

Setting TCP_NODELAY removes unnecessary delays when sending network packets, removing ~40ms delay in some cases.
Does your PR solve an issue?
Fixes #4335
Is this a breaking change?
No, not a breaking change. This only improves performance in some cases.