Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/NATS.Client.JetStream/Internal/NatsJSConsume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ public ValueTask CallMsgNextAsync(string origin, ConsumerGetnextRequest request,
cancellationToken: cancellationToken);
}

public void StopHeartbeatTimer() => _timer.Change(Timeout.Infinite, Timeout.Infinite);

public void ResetHeartbeatTimer() => _timer.Change(_hbTimeout, _hbTimeout);

public void Delivered(int msgSize)
Expand Down
10 changes: 10 additions & 0 deletions src/NATS.Client.JetStream/Internal/NatsJSFetch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ public ValueTask CallMsgNextAsync(ConsumerGetnextRequest request, CancellationTo
serializer: NatsJSJsonSerializer<ConsumerGetnextRequest>.Default,
cancellationToken: cancellationToken);

public void StopHeartbeatTimer()
{
// if we don't have an idle timeout, we don't need to reset the timer
// because we don't expect any heartbeats.
if (_idle == TimeSpan.Zero)
return;

_hbTimer.Change(Timeout.Infinite, Timeout.Infinite);
}

public void ResetHeartbeatTimer()
{
// if we don't have an idle timeout, we don't need to reset the timer
Expand Down
12 changes: 12 additions & 0 deletions src/NATS.Client.JetStream/NatsJSConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ public async IAsyncEnumerable<NatsJSMsg<T>> ConsumeAsync<T>(
if (!read)
break;

// if yield is blocked by the application code, we don't want
// heartbeat timer kicking in and issuing unnecessary pull requests.
cc.StopHeartbeatTimer();
yield return jsMsg;
cc.ResetHeartbeatTimer();
cc.Delivered(jsMsg.Size);
}
}
Expand Down Expand Up @@ -202,7 +206,11 @@ public async IAsyncEnumerable<NatsJSMsg<T>> FetchAsync<T>(
if (!read)
break;

// if yield is blocked by the application code, we don't want
// heartbeat timer kicking in and issuing unnecessary pull requests.
fc.StopHeartbeatTimer();
yield return jsMsg;
fc.ResetHeartbeatTimer();
}
}
}
Expand Down Expand Up @@ -263,7 +271,11 @@ public async IAsyncEnumerable<NatsJSMsg<T>> FetchNoWaitAsync<T>(
await using var fc = await FetchInternalAsync<T>(opts with { NoWait = true }, serializer, cancellationToken).ConfigureAwait(false);
await foreach (var jsMsg in fc.Msgs.ReadAllAsync(cancellationToken).ConfigureAwait(false))
{
// if yield is blocked by the application code, we don't want
// heartbeat timer kicking in and issuing unnecessary pull requests.
fc.StopHeartbeatTimer();
yield return jsMsg;
fc.ResetHeartbeatTimer();
}
}

Expand Down