Expose min_flush_interval on connect() and Client#987
Open
mgalletti wants to merge 1 commit into
Open
Conversation
The 5 ms minimum flush interval between socket writes (write-coalescing for concurrent publishers) was hardcoded, forcing sequential publish/ack workloads to pay it per message with no way to opt out short of overriding the private _min_flush_interval attribute. Add a keyword-only min_flush_interval option to connect() and Client.__init__ (default unchanged at 0.005), validate that it is not negative, and expose it as a read-only property. Setting it to 0 flushes immediately, which favors sequential publish/ack loops. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Motivation
The client's write loop coalesces socket writes by enforcing a minimum interval between flushes, hardcoded to 5 ms (
_DEFAULT_MIN_FLUSH_INTERVAL). Write-coalescing is the right default when many tasks publish concurrently — one flush amortizes across all their writes.For sequential publish→await-reply workloads (request/reply, or publish-then-wait-for-ack loops), the interval works against the caller instead: each message pays up to the full 5 ms before its bytes reach the socket, capping a single connection at roughly 200 msgs/s even when the server round trip is well under a millisecond. Today the only way around this is overriding the private
_min_flush_intervalattribute, which isn't safe across releases.Changes
min_flush_interval: float = 0.005parameter onconnect()andClient.__init__. The default is unchanged, so existing callers see no behavior difference.0is valid and means flush immediately (no coalescing); negative values raiseValueError, validated alongside the existinginbox_prefixchecks.Client.min_flush_intervalproperty, matching the style ofstatus/last_error.Testing
nats-core/tests/test_client.py: the 5 ms default, connecting withmin_flush_interval=0.0including a publish/subscribe round trip through the write loop, and rejection of negative values.test_client.pysuite passes (178 tests);ruff check,ruff format --check, andcodespellare clean