internal void RecordOpenedConnection(GatewayInboundConnection connection, ClientGrainId clientId) { lock (lockable) { logger.LogInformation((int)ErrorCode.GatewayClientOpenedSocket, "Recorded opened connection from endpoint {EndPoint}, client ID {ClientId}.", connection.RemoteEndPoint, clientId); ClientState clientState; if (clients.TryGetValue(clientId, out clientState)) { var oldSocket = clientState.Connection; if (oldSocket != null) { // The old socket will be closed by itself later. clientConnections.TryRemove(oldSocket, out _); } } else { clientState = new ClientState(clientId, messagingOptions.ClientDropTimeout); clients[clientId] = clientState; MessagingStatisticsGroup.ConnectedClientCount.Increment(); } clientState.RecordConnection(connection); clientConnections[connection] = clientState; clientRegistrar.ClientAdded(clientId); } }
// This function is run under global lock // There is NO need to acquire individual ClientState lock, since we only access client Id (immutable) and close an older socket. private void DropClient(ClientState client) { if (logger.IsEnabled(LogLevel.Information)) { logger.LogInformation( (int)ErrorCode.GatewayDroppingClient, "Dropping client {ClientId}, {IdleDuration} after disconnect with no reconnect", client.Id, DateTime.UtcNow.Subtract(client.DisconnectedSince)); } clients.TryRemove(client.Id, out _); GatewayInboundConnection oldConnection = client.Connection; if (oldConnection != null) { // this will not happen, since we drop only already disconnected clients, for socket is already null. But leave this code just to be sure. client.RecordDisconnection(); clientConnections.TryRemove(oldConnection, out _); oldConnection.CloseAsync(exception: null).Ignore(); } Interlocked.Increment(ref clientsCollectionVersion); MessagingStatisticsGroup.ConnectedClientCount.DecrementBy(1); }
// This function is run under global lock // There is NO need to acquire individual ClientState lock, since we only access client Id (immutable) and close an older socket. private void DropClient(ClientState client) { logger.Info(ErrorCode.GatewayDroppingClient, "Dropping client {0}, {1} after disconnect with no reconnect", client.Id, DateTime.UtcNow.Subtract(client.DisconnectedSince)); clients.TryRemove(client.Id, out _); clientRegistrar.ClientDropped(client.Id); GatewayInboundConnection oldConnection = client.Connection; if (oldConnection != null) { // this will not happen, since we drop only already disconnected clients, for socket is already null. But leave this code just to be sure. client.RecordDisconnection(); clientConnections.TryRemove(oldConnection, out _); oldConnection.Close(new ConnectionAbortedException("Dropping client")); } MessagingStatisticsGroup.ConnectedClientCount.DecrementBy(1); }
internal void RecordClosedConnection(GatewayInboundConnection connection) { if (connection == null) { return; } lock (lockable) { if (!clientConnections.TryGetValue(connection, out var clientState)) { return; } clientConnections.TryRemove(connection, out _); clientState.RecordDisconnection(); logger.LogInformation( (int)ErrorCode.GatewayClientClosedSocket, "Recorded closed socket from endpoint {Endpoint}, client ID {clientId}.", connection.RemoteEndPoint?.ToString() ?? "null", clientState.Id); } }
internal void RecordConnection(GatewayInboundConnection connection) { Connection = connection; DisconnectedSince = DateTime.MaxValue; }