Skip to content

Excessive NatsJSTimeoutNotification from NatsJSConsume due to periodic heartbeat timer #952

@Ivandemidov00

Description

@Ivandemidov00

Proposed change

Context

In NatsJSConsume, the idle heartbeat timer is currently configured as periodic:
_timer.Change(_hbTimeout, _hbTimeout);
This was introduced in PR #185.
Other consumers (e.g. NatsJSOrderedConsume) still use the one-shot timer style:
_timer.Change(_hbTimeout, Timeout.Infinite);

Problem

Because the timer is periodic, as soon as the first real idle timeout occurs, the client starts raising NatsJSTimeoutNotification repeatedly every _hbTimeout until messages arrive again.

Possible approaches:

Hybrid approach:
Keep the timer periodic for safety, but only send NatsJSTimeoutNotification on the first tick after silence. Subsequent ticks would still re-trigger internal pull logic, but not bubble notifications to the user.

Configurable mode:
Allow users to opt into Timeout.Infinite (one-shot) or periodic behavior via NatsSubOpts. This way, high-reliability scenarios can use the safer periodic mode, while others can avoid notification spam.

Use case

Real case

In our environment we have rare but valid messages with payload (not just heartbeats).

Messages may arrive only once in a while, which is still correct behavior for the application.

However, because of the periodic timer, the consumer produces infinite timeout notifications between these payload messages.

This makes it look like the consumer is failing, while in reality it is functioning as expected.

Contribution

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions