Table of Contents

Class DistributedWebSocketConnectionStore

Namespace
Virtufin.WebSocketManager.Services
Assembly
Virtufin.WebSocketManager.dll

Distributed implementation of IWebSocketConnectionStore that uses Dapr state store for persistence.

public class DistributedWebSocketConnectionStore : IWebSocketConnectionStore
Inheritance
DistributedWebSocketConnectionStore
Implements
Inherited Members

Constructors

DistributedWebSocketConnectionStore(IDaprConnectionRepository, IInstanceIdProvider)

Initializes a new instance of the DistributedWebSocketConnectionStore class.

public DistributedWebSocketConnectionStore(IDaprConnectionRepository repository, IInstanceIdProvider instanceIdProvider)

Parameters

repository IDaprConnectionRepository

The Dapr connection repository for persistence.

instanceIdProvider IInstanceIdProvider

The instance ID provider.

Methods

ClearAllConnectionsAsync(CancellationToken)

Removes all WebSocket connections.

public Task ClearAllConnectionsAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task

CreateConnectionAsync(string, bool, CancellationToken)

Creates a new WebSocket connection entry.

public ValueTask<WebSocketConnection> CreateConnectionAsync(string url, bool autoReconnect, CancellationToken cancellationToken = default)

Parameters

url string
autoReconnect bool
cancellationToken CancellationToken

Returns

ValueTask<WebSocketConnection>

GetAllConnectionsAsync(CancellationToken)

Gets all WebSocket connections.

public Task<IEnumerable<WebSocketConnection>> GetAllConnectionsAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task<IEnumerable<WebSocketConnection>>

GetConnectionAsync(string, CancellationToken)

Gets a WebSocket connection by its ID.

public Task<WebSocketConnection?> GetConnectionAsync(string id, CancellationToken cancellationToken = default)

Parameters

id string
cancellationToken CancellationToken

Returns

Task<WebSocketConnection>

GetConnectionById(string)

Synchronous lookup of a local connection by ID (in-memory only). Returns null for connections owned by other instances.

public WebSocketConnection? GetConnectionById(string id)

Parameters

id string

Returns

WebSocketConnection

GetLocalConnections()

Snapshot of connections owned by this instance (in-memory only). Used by ConnectionHeartbeatService to refresh state-store TTLs without depending on send/publish traffic.

public IReadOnlyCollection<WebSocketConnection> GetLocalConnections()

Returns

IReadOnlyCollection<WebSocketConnection>

ReclaimAsync(string, CancellationToken)

Re-stamps ownership of a connection last known to be owned by another (now unreachable) instance to this one, persists it, and registers it in this instance's local, in-memory view -- unlike GetConnectionAsync(string, CancellationToken), which is local-only and would otherwise never see a connection this instance didn't originally create. Returns the reclaimed connection (caller is responsible for actually re-establishing the socket if desired), or null if no persisted record exists for id.

public Task<WebSocketConnection?> ReclaimAsync(string id, CancellationToken cancellationToken = default)

Parameters

id string
cancellationToken CancellationToken

Returns

Task<WebSocketConnection>

RemoveConnectionAsync(string, CancellationToken)

Removes a WebSocket connection by its ID.

public Task RemoveConnectionAsync(string id, CancellationToken cancellationToken = default)

Parameters

id string
cancellationToken CancellationToken

Returns

Task

TouchAsync(string, CancellationToken)

Refreshes the state-store TTL on the connection record without changing its payload. Called after a successful Send to keep an idle but live socket from being garbage-collected.

public Task TouchAsync(string id, CancellationToken cancellationToken = default)

Parameters

id string
cancellationToken CancellationToken

Returns

Task

UpdateConnectionAsync(WebSocketConnection, CancellationToken)

Updates an existing WebSocket connection.

public Task UpdateConnectionAsync(WebSocketConnection connection, CancellationToken cancellationToken = default)

Parameters

connection WebSocketConnection
cancellationToken CancellationToken

Returns

Task