Fix heartbeat timer to prevent unnecessary pulls#794
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR addresses the issue of unnecessary pull requests by pausing the heartbeat timer while yielding messages.
- Introduces calls to StopHeartbeatTimer and ResetHeartbeatTimer in asynchronous message iteration methods.
- Adds a heartbeat timer control method in NatsJSConsume and duplicates the logic in NatsJSFetch.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/NATS.Client.JetStream/NatsJSConsumer.cs | Inserts heartbeat timer control calls to prevent unnecessary pull requests. |
| src/NATS.Client.JetStream/Internal/NatsJSConsume.cs | Adds StopHeartbeatTimer and modifies ResetHeartbeatTimer implementation. |
| src/NATS.Client.JetStream/Internal/NatsJSFetch.cs | Duplicates StopHeartbeatTimer with an idle timeout check to match heartbeat logic. |
Comments suppressed due to low confidence (2)
src/NATS.Client.JetStream/Internal/NatsJSConsume.cs:168
- The StopHeartbeatTimer implementation in NatsJSConsume.cs does not check for an idle timeout (unlike NatsJSFetch.cs). Verify that this inconsistency in behavior is intentional to avoid unintended timer actions.
public void StopHeartbeatTimer() => _timer.Change(Timeout.Infinite, Timeout.Infinite);
src/NATS.Client.JetStream/Internal/NatsJSConsume.cs:170
- ResetHeartbeatTimer in NatsJSConsume.cs resets both the due time and period to _hbTimeout, whereas the pattern in NatsJSFetch.cs uses Timeout.Infinite for the period. Confirm that this difference is deliberate to ensure consistent timer behavior.
public void ResetHeartbeatTimer() => _timer.Change(_hbTimeout, _hbTimeout);
mtmk
added a commit
that referenced
this pull request
Apr 9, 2025
Merged
mtmk
added a commit
that referenced
this pull request
Apr 9, 2025
This was referenced Mar 2, 2026
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.
if application is hanging on the message in foreach loop (as we are yielding) we stop the timer until iteration moved on to stop timer kicking in and issuing extra pull requests. This doesn't affect message delivery but increments consumer sequence unnecessarily. this behavior is now consistent with the Go client.
fixes #793