Fix KeyWatcher#updates(timeout:) to never drop a received entry#176
Open
Xayc73 wants to merge 1 commit intonats-io:mainfrom
Open
Fix KeyWatcher#updates(timeout:) to never drop a received entry#176Xayc73 wants to merge 1 commit intonats-io:mainfrom
KeyWatcher#updates(timeout:) to never drop a received entry#176Xayc73 wants to merge 1 commit intonats-io:mainfrom
Conversation
KeyWatcher#updates(timeout:) to never drop a received entry**KeyWatcher#updates(timeout:) to never drop a received entry
Author
|
@vankiru Hi! Could you take a look when you have a moment? I’d like to know the current state of the gem and whether there’s anything we can do to move this MR forward. Thanks! |
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
NATS::KeyWatcher#updates(timeout:)can raiseNATS::Timeoutafter@_updates.pop(timeout:)has already returned an entry, because it wraps thepopcall with a post-check timeout helper (MonotonicTime.with_nats_timeout). Under high load / scheduling delays / tiny timeouts, this leads to a “logical drop”: the caller receives a timeout exception and typically ignores that iteration, even though an update was actually dequeued.This MR changes
KeyWatcher#updatesso that timeouts are only raised when no value was returned. If an entry was received, it is always returned to the caller.Expected result
updates(timeout:)returns an entry from the internal queue, it is never discarded via a late timeout exception.nilmarker),updatesreturnsnil.updatesraisesNATS::Timeout.Root cause analysis
Current implementation:
KeyWatcher#updates(timeout:)does:@_updates.pop(timeout:)MonotonicTime.with_nats_timeout(timeout)checks elapsed time after the block returnsNATS::TimeoutThis is correct for “deadline-style” APIs, but not for “return-or-timeout” queue reads, because the timeout exception can be raised even after a value has been successfully read.
What changed
KeyWatcher#updatesno longer wraps@_updates.popwithMonotonicTime.with_nats_timeout.popand raises only if:nil, andThis ensures that a returned entry always wins over a late timeout.
Why this approach
KeyWatcher#updates, not toMonotonicTime.with_nats_timeout, because that helper is used in other places (e.g. request/flush/waits) where its semantics may be relied upon.watchcontract aroundnilmarker vs. real timeout remains intact.Files changed
lib/nats/io/kv.rb: adjustKeyWatcher#updates(timeout:)timeout semantics.spec/kv_spec.rb: add regression spec for “entry returned but elapsed time > timeout”.