Class KeepAliveTracker

Namespace
Cratis.Arc.Queries
Assembly
Cratis.Arc.Core.dll

Tracks the last time a message was sent to determine whether a keep-alive message should be sent.

public class KeepAliveTracker
Inheritance
KeepAliveTracker
Inherited Members
Extension Methods

Remarks

The keep-alive logic is transport-agnostic. Any transport that needs keep-alive behavior should create an instance of this class and call RecordMessageSent() every time a message is dispatched to the client. Before sending a keep-alive, check ShouldSendKeepAlive(TimeSpan) to avoid sending redundant keep-alive messages when normal data is already flowing.

Constructors

KeepAliveTracker()

Initializes a new instance of the KeepAliveTracker class.

public KeepAliveTracker()

KeepAliveTracker(DateTimeOffset)

Initializes a new instance of the KeepAliveTracker class with a specific initial timestamp.

public KeepAliveTracker(DateTimeOffset initialLastMessageSent)

Parameters

initialLastMessageSent DateTimeOffset

The initial value for the last-message-sent timestamp.

Methods

GetTimeUntilNextKeepAlive(TimeSpan)

Gets how long remains until a keep-alive message is due, measured from the last message sent.

public TimeSpan GetTimeUntilNextKeepAlive(TimeSpan interval)

Parameters

interval TimeSpan

The keep-alive interval.

Returns

TimeSpan

The remaining time, or Zero when a keep-alive is already due.

Remarks

Keep-alive loops should wait for exactly this long rather than for a fixed interval. Waiting a fixed interval schedules checks on a grid that is independent of when messages actually go out, so a data message landing mid-interval defers the next keep-alive to the following tick — allowing gaps of up to twice the interval. Clients that treat silence as a dead connection then disconnect while the server still considers the connection healthy.

RecordActivity()

Records any connection activity (message sent or received), resetting the keep-alive timer. No keep-alive message is needed while the connection is actively exchanging data.

public void RecordActivity()

RecordMessageSent()

Records that a message was sent to the client, resetting the keep-alive timer.

public void RecordMessageSent()

ShouldSendKeepAlive(TimeSpan)

Determines whether a keep-alive message should be sent given the configured interval.

public bool ShouldSendKeepAlive(TimeSpan interval)

Parameters

interval TimeSpan

The keep-alive interval.

Returns

bool

true if a keep-alive should be sent; otherwise false.