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, string, bool, CancellationToken)

Creates a new WebSocket connection entry under the given, already-minted id -- the caller (WebSocketManagerGrpcService.Connect) mints the id and activates the owning WebSocketConnectionActor for it before this ever runs, so the id always comes from outside now rather than being generated in here.

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

Parameters

id string
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>

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

TryLoadPersistedAsync(string, CancellationToken)

If id isn't already local, checks the persisted directory and, if found, materializes it into this instance's local view. Used only by WebSocketConnectionActor.OnActivateAsync to self-heal after Dapr activates this connection's actor on an instance that never opened its socket -- a genuine failover/rebalance of an already-connected id, not a fresh CreateConnectionAsync(string, string, bool, CancellationToken) (which registers the connection locally itself, so this is always a no-op during a normal Connect). Returns null if already local, or if there's truly no persisted record either.

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

Parameters

id string
cancellationToken CancellationToken

Returns

Task<WebSocketConnection>

UpdateConnectionAsync(WebSocketConnection, CancellationToken)

Updates an existing WebSocket connection.

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

Parameters

connection WebSocketConnection
cancellationToken CancellationToken

Returns

Task