Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/IO.Ably.Shared/Realtime/Workflows/RealtimeWorkflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,17 @@ private async Task<RealtimeCommand> HandleSetStateCommand(RealtimeCommand comman
State.Connection.ClearKeyAndId();
}

var connectingHost = AttemptsHelpers.GetHost(State, Client.Options.FullRealtimeHost());
var defaultRealtimeHost = Client.Options.FullRealtimeHost();

// Always retry on defaultPrimaryHost first when connecting command triggered by Disconnected/Suspended state timeout.
var connectingHost = defaultRealtimeHost;

// Otherwise use host fallbacks if connecting command triggered by other commands
if (cmd.TriggeredByMessage.Contains("OnTimeOut()") == false)
{
connectingHost = AttemptsHelpers.GetHost(State, defaultRealtimeHost);
}

SetNewHostInState(connectingHost);

var connectingState = new ConnectionConnectingState(ConnectionManager, Logger);
Expand Down Expand Up @@ -787,12 +797,13 @@ ErrorInfo TransformIfTokenErrorAndNotRetryable()
break;
case SetDisconnectedStateCommand cmd:

var (retryInstantly, clearKey) = await GetDisconnectFlags();
if (clearKey)
if (cmd.ClearConnectionKey)
{
State.Connection.ClearKey();
}

var retryInstantly = await CheckInstantRetryFlag();

var disconnectedState = new ConnectionDisconnectedState(ConnectionManager, cmd.Error, Logger)
{
RetryInstantly = retryInstantly,
Expand Down Expand Up @@ -823,19 +834,19 @@ ErrorInfo TransformIfTokenErrorAndNotRetryable()
return SetConnectingStateCommand.Create().TriggeredBy(command);
}

async Task<(bool retry, bool clearKey)> GetDisconnectFlags()
async Task<bool> CheckInstantRetryFlag()
{
if (cmd.RetryInstantly)
{
return (true, cmd.ClearConnectionKey);
return true;
}

if ((cmd.Error != null && cmd.Error.IsRetryableStatusCode()) || cmd.Exception != null)
{
return (await Client.RestClient.CanConnectToAbly(), true);
return await Client.RestClient.CanConnectToAbly();
}

return (false, cmd.ClearConnectionKey);
return false;
}

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,30 @@ public static void FakeMessageReceived(this AblyRealtime client, Message message
new ProtocolMessage(ProtocolMessage.MessageAction.Message) { Messages = new[] { message }, Channel = channel });
}

public static async Task DisconnectWithRetryableError(this AblyRealtime client)
public static async Task DisconnectWithRetryableError(this AblyRealtime client, bool waitForDisconnectedState = true)
{
client.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Disconnected)
{
Error = new ErrorInfo { StatusCode = HttpStatusCode.GatewayTimeout }
});

await client.WaitForState(ConnectionState.Disconnected);
if (waitForDisconnectedState)
{
await client.WaitForState(ConnectionState.Disconnected);
}
}

public static async Task DisconnectWithNonRetryableError(this AblyRealtime client, bool waitForDisconnectedState = true)
{
client.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Disconnected)
{
Error = new ErrorInfo { StatusCode = HttpStatusCode.Forbidden }
});

if (waitForDisconnectedState)
{
await client.WaitForState(ConnectionState.Disconnected);
}
}

public static async Task ConnectClient(this AblyRealtime client)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using FluentAssertions;
using IO.Ably.Realtime;
using IO.Ably.Realtime.Workflow;
using IO.Ably.Tests.Infrastructure;
using IO.Ably.Types;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -342,6 +343,98 @@ public async Task WhenItMovesFromDisconnectedToSuspended_ShouldTryDefaultHostAga
realtimeHosts.Last().Should().Be("realtime.ably.io");
}

[Fact]
[Trait("spec", "RTN17f")]
public async Task WhenNonRetryableError_ShouldAlwaysTryDefaultHostFirst()
{
var client = await GetConnectedClient(opts =>
{
opts.DisconnectedRetryTimeout = TimeSpan.FromSeconds(2);
opts.SuspendedRetryTimeout = TimeSpan.FromSeconds(2);
});

// Reduced connectionStateTTL for limited disconnected retries upto 20 seconds
client.State.Connection.ConnectionStateTtl = TimeSpan.FromSeconds(20);

var realtimeHosts = new List<string>();
FakeTransportFactory.InitialiseFakeTransport = p => realtimeHosts.Add(p.Parameters.Host);

client.Connection.On(ConnectionEvent.Connecting, stateChange =>
{
if (stateChange.Previous == ConnectionState.Disconnected)
{
client.DisconnectWithNonRetryableError(false);
}
});
Comment thread
sacOO7 marked this conversation as resolved.

// Receive first disconnect message on CONNECTED client, will call above callback after timeout
await client.DisconnectWithNonRetryableError();

await new ConditionalAwaiter(() => client.Connection.State == ConnectionState.Suspended, null, 120);

client.Connection.State.Should().Be(ConnectionState.Suspended);
await client.WaitForState(ConnectionState.Connecting);

client.DisconnectWithNonRetryableError(false);

await client.WaitForState(ConnectionState.Suspended);
await client.WaitForState(ConnectionState.Connecting);

realtimeHosts.Should().AllBe("realtime.ably.io");
}

[Fact]
[Trait("spec", "RTN17j")]
public async Task WhenInternetConnectionIsDown_ShouldAlwaysTryDefaultHostFirst()
{
var response = new HttpResponseMessage(HttpStatusCode.Forbidden)
{
Content = new StringContent("Internet not available")
};

var handler = new FakeHttpMessageHandler(response);

var realtimeHosts = new List<string>();
FakeTransportFactory.InitialiseFakeTransport = p => realtimeHosts.Add(p.Parameters.Host);

var client = GetClientWithFakeTransportAndMessageHandler(
opts =>
{
opts.DisconnectedRetryTimeout = TimeSpan.FromSeconds(2);
opts.SuspendedRetryTimeout = TimeSpan.FromSeconds(2);
},
handler);
client.Options.SkipInternetCheck = false;

// Reduced connectionStateTTL for limited disconnected retries upto 20 seconds
client.State.Connection.ConnectionStateTtl = TimeSpan.FromSeconds(20);

await client.ConnectClient(); // On the default host

client.Connection.On(ConnectionEvent.Connecting, stateChange =>
{
if (stateChange.Previous == ConnectionState.Disconnected)
{
client.DisconnectWithRetryableError(false);
}
});
Comment thread
sacOO7 marked this conversation as resolved.

// Receive first disconnect message on CONNECTED client, will call above callback after timeout
await client.DisconnectWithRetryableError();

await new ConditionalAwaiter(() => client.Connection.State == ConnectionState.Suspended, null, 120);

client.Connection.State.Should().Be(ConnectionState.Suspended);
await client.WaitForState(ConnectionState.Connecting);

client.DisconnectWithRetryableError(false);

await client.WaitForState(ConnectionState.Suspended);
await client.WaitForState(ConnectionState.Connecting);

realtimeHosts.Should().AllBe("realtime.ably.io");
}

public ConnectionFallbackSpecs(ITestOutputHelper output)
: base(output)
{
Expand Down
Loading