Skip to content

Commit

Permalink
rename constants for clarity (#2048)
Browse files Browse the repository at this point in the history
  • Loading branch information
terencefan authored Sep 24, 2024
1 parent 3ce4eba commit 9f17c81
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 } };
Expand All @@ -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 } };
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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,
};
}
Expand All @@ -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,
});

Expand All @@ -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)
{
Expand All @@ -197,4 +207,4 @@ internal static bool TryGetValue(ServicePingMessage pingMessage, string key, out
value = null;
return false;
}
}
}

0 comments on commit 9f17c81

Please sign in to comment.