From 9f17c818eb06899d6184b7e14ca7f8fde393f422 Mon Sep 17 00:00:00 2001 From: Terence Fan Date: Tue, 24 Sep 2024 15:18:15 +0800 Subject: [PATCH] rename constants for clarity (#2048) --- .../ServiceConnectionContainerBase.cs | 12 +++--- .../RuntimeServicePingMessages.cs | 38 ++++++++++++------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/Microsoft.Azure.SignalR.Common/ServiceConnections/ServiceConnectionContainerBase.cs b/src/Microsoft.Azure.SignalR.Common/ServiceConnections/ServiceConnectionContainerBase.cs index 52cfa92f2..ae64d5ad4 100644 --- a/src/Microsoft.Azure.SignalR.Common/ServiceConnections/ServiceConnectionContainerBase.cs +++ b/src/Microsoft.Azure.SignalR.Common/ServiceConnections/ServiceConnectionContainerBase.cs @@ -26,6 +26,7 @@ internal abstract class ServiceConnectionContainerBase : IServiceConnectionConta private static readonly int MaxReconnectBackOffInternalInMilliseconds = 1000; private static readonly TimeSpan MessageWriteRetryDelay = TimeSpan.FromMilliseconds(200); + private static readonly int MessageWriteMaxRetry = 3; // Give (interval * 3 + 1) delay when check value expire. @@ -119,6 +120,7 @@ protected ServiceConnectionContainerBase(IServiceConnectionFactory serviceConnec Logger = logger ?? throw new ArgumentNullException(nameof(logger)); ServiceConnectionFactory = serviceConnectionFactory; Endpoint = endpoint; + // use globally unique AckHanlder if not specified // It is possible that the multiple MapHub calls the same hub, so that ack messages could be received by another instance of ServiceConnectionContainer // Use the ack handler singleton to allow ack message to be acked by another container instance @@ -395,19 +397,15 @@ protected virtual ServiceConnectionStatus GetStatus() : ServiceConnectionStatus.Disconnected; } - protected async Task WriteFinAsync(IServiceConnection c, GracefulShutdownMode mode) - { - var message = RuntimeServicePingMessage.GetFinPingMessage(mode); - await c.WriteAsync(message); - } - protected async Task RemoveConnectionAsync(IServiceConnection c, GracefulShutdownMode mode) { var retry = 0; while (retry < MaxRetryRemoveSeverConnection) { using var source = new CancellationTokenSource(); - _ = WriteFinAsync(c, mode); + + var message = RuntimeServicePingMessage.GetFinPingMessage(mode); + _ = c.WriteAsync(message); var task = await Task.WhenAny(c.ConnectionOfflineTask, Task.Delay(Constants.Periods.RemoveFromServiceTimeout, source.Token)); diff --git a/src/Microsoft.Azure.SignalR.Common/ServiceMessages/RuntimeServicePingMessages.cs b/src/Microsoft.Azure.SignalR.Common/ServiceMessages/RuntimeServicePingMessages.cs index 222976278..17121b8c7 100644 --- a/src/Microsoft.Azure.SignalR.Common/ServiceMessages/RuntimeServicePingMessages.cs +++ b/src/Microsoft.Azure.SignalR.Common/ServiceMessages/RuntimeServicePingMessages.cs @@ -10,27 +10,38 @@ namespace Microsoft.Azure.SignalR; internal static class RuntimeServicePingMessage { private const string EchoKey = "echo"; + private const string OfflineKey = "offline"; + private const string TargetKey = "target"; + private const string StatusKey = "status"; + private const string ShutdownKey = "shutdown"; + private const string ServersKey = "servers"; + private const string ClientCountKey = "clientcount"; + private const string ServerCountKey = "servercount"; + private const string CapacityKey = "capacity"; + private const string DiagnosticLogsMessagingTypeKey = "d-m"; private const string MessagingLogEnableValue = "1"; private const string StatusActiveValue = "1"; + private const string StatusInactiveValue = "0"; - private const string ShutdownFinKeepAliveValue = "fin:2"; - private const string ShutdownFinMigratableValue = "fin:1"; private const string ShutdownFinValue = "fin:0"; + private const string ShutdownFinMigrateClientsValue = "fin:1"; + + private const string ShutdownFinWaitForClientsValue = "fin:2"; + private const string ShutdownFinAckValue = "finack"; - private const char ServerListSeparator = ';'; private static readonly ServicePingMessage StatusActive = new ServicePingMessage { Messages = new[] { StatusKey, StatusActiveValue } }; @@ -41,11 +52,11 @@ internal static class RuntimeServicePingMessage private static readonly ServicePingMessage ShutdownFin = new ServicePingMessage { Messages = new[] { ShutdownKey, ShutdownFinValue } }; - private static readonly ServicePingMessage ShutdownFinMigratable = - new ServicePingMessage { Messages = new[] { ShutdownKey, ShutdownFinMigratableValue } }; + private static readonly ServicePingMessage ShutdownFinMigrateClients = + new ServicePingMessage { Messages = new[] { ShutdownKey, ShutdownFinMigrateClientsValue } }; - private static readonly ServicePingMessage ShutdownFinKeepAlive = - new ServicePingMessage { Messages = new[] { ShutdownKey, ShutdownFinKeepAliveValue } }; + private static readonly ServicePingMessage ShutdownFinWaitForClients = + new ServicePingMessage { Messages = new[] { ShutdownKey, ShutdownFinWaitForClientsValue } }; private static readonly ServicePingMessage ShutdownFinAck = new ServicePingMessage { Messages = new[] { ShutdownKey, ShutdownFinAckValue } }; @@ -84,7 +95,6 @@ public static ServicePingMessage GetStatusPingMessage(bool isActive, int clientC ? new ServicePingMessage { Messages = new[] { StatusKey, StatusActiveValue, ClientCountKey, clientCount.ToString() } } : new ServicePingMessage { Messages = new[] { StatusKey, StatusInactiveValue, ClientCountKey, clientCount.ToString() } }; - public static bool TryGetStatus(this ServicePingMessage ping, out bool isActive) { if (!TryGetValue(ping, StatusKey, out var value)) @@ -150,8 +160,8 @@ public static ServicePingMessage GetFinPingMessage(GracefulShutdownMode mode = G { return mode switch { - GracefulShutdownMode.WaitForClientsClose => ShutdownFinKeepAlive, - GracefulShutdownMode.MigrateClients => ShutdownFinMigratable, + GracefulShutdownMode.WaitForClientsClose => ShutdownFinWaitForClients, + GracefulShutdownMode.MigrateClients => ShutdownFinMigrateClients, _ => ShutdownFin, }; } @@ -165,8 +175,8 @@ public static bool IsFin(this ServiceMessage serviceMessage) => serviceMessage is ServicePingMessage ping && TryGetValue(ping, ShutdownKey, out var value) && (value switch { ShutdownFinValue => true, - ShutdownFinMigratableValue => true, - ShutdownFinKeepAliveValue => true, + ShutdownFinMigrateClientsValue => true, + ShutdownFinWaitForClientsValue => true, _ => false, }); @@ -185,7 +195,7 @@ internal static bool TryGetValue(ServicePingMessage pingMessage, string key, out return false; } - for (int i = 0; i < pingMessage.Messages.Length - 1; i += 2) + for (var i = 0; i < pingMessage.Messages.Length - 1; i += 2) { if (pingMessage.Messages[i] == key) { @@ -197,4 +207,4 @@ internal static bool TryGetValue(ServicePingMessage pingMessage, string key, out value = null; return false; } -} \ No newline at end of file +}