Preserve CultMesh sessions across endpoint rotation
This commit is contained in:
parent
b4e06bc762
commit
c937ec633b
2 changed files with 166 additions and 28 deletions
|
|
@ -161,17 +161,18 @@ namespace GameCult.Mesh
|
|||
{
|
||||
private readonly Subject<CultMeshSessionState> _states = new();
|
||||
private readonly ConcurrentBag<IDisposable> _subscriptions = new();
|
||||
private readonly ManagedSchemaChannel _channel;
|
||||
private bool _disposed;
|
||||
internal CultMeshSession(CultMeshEndpointId endpointId, CultMeshProtocolId protocol, ICultNetSchemaClient channel, CultMeshSessionState state)
|
||||
{
|
||||
EndpointId = endpointId;
|
||||
Protocol = protocol;
|
||||
Channel = channel;
|
||||
_channel = new ManagedSchemaChannel(channel);
|
||||
State = state;
|
||||
}
|
||||
public CultMeshEndpointId EndpointId { get; }
|
||||
public CultMeshProtocolId Protocol { get; }
|
||||
internal ICultNetSchemaClient Channel { get; }
|
||||
internal ICultNetSchemaClient Channel => _channel;
|
||||
public CultMeshSessionState State { get; private set; }
|
||||
public Observable<CultMeshSessionState> WatchState() => _states;
|
||||
public ICultNetSchemaClient OpenSchemaClient() => new SessionSchemaClient(this);
|
||||
|
|
@ -185,15 +186,90 @@ namespace GameCult.Mesh
|
|||
return subscription;
|
||||
}
|
||||
internal void Transition(CultMeshSessionState state) { State = state; _states.OnNext(state); }
|
||||
internal void ReplacePhysicalChannel(ICultNetSchemaClient channel) => _channel.Replace(channel);
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
foreach (var subscription in _subscriptions) subscription.Dispose();
|
||||
Channel.Dispose();
|
||||
_channel.Dispose();
|
||||
_states.Dispose();
|
||||
}
|
||||
|
||||
private sealed class ManagedSchemaChannel : ICultNetSchemaClient
|
||||
{
|
||||
private readonly object _gate = new();
|
||||
private readonly List<IChannelRegistration> _registrations = new();
|
||||
private ICultNetSchemaClient _physical;
|
||||
private bool _disposed;
|
||||
|
||||
public ManagedSchemaChannel(ICultNetSchemaClient physical) =>
|
||||
_physical = physical ?? throw new ArgumentNullException(nameof(physical));
|
||||
|
||||
public bool Connected { get { lock (_gate) return !_disposed && _physical.Connected; } }
|
||||
public void Connect(string host, int port)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
_physical.Connect(host, port);
|
||||
}
|
||||
}
|
||||
public void SendCultNet<T>(T message) where T : ICultNetSchemaMessage
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
_physical.SendCultNet(message);
|
||||
}
|
||||
}
|
||||
public void OnCultNet<T>(Action<T> callback) where T : ICultNetSchemaMessage
|
||||
{
|
||||
if (callback == null) throw new ArgumentNullException(nameof(callback));
|
||||
lock (_gate)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
var registration = new ChannelRegistration<T>(callback);
|
||||
registration.Attach(_physical);
|
||||
_registrations.Add(registration);
|
||||
}
|
||||
}
|
||||
public void Replace(ICultNetSchemaClient physical)
|
||||
{
|
||||
if (physical == null) throw new ArgumentNullException(nameof(physical));
|
||||
ICultNetSchemaClient previous;
|
||||
lock (_gate)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
previous = _physical;
|
||||
foreach (var registration in _registrations) registration.Attach(physical);
|
||||
_physical = physical;
|
||||
}
|
||||
previous.Dispose();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
ICultNetSchemaClient physical;
|
||||
lock (_gate)
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
physical = _physical;
|
||||
_registrations.Clear();
|
||||
}
|
||||
physical.Dispose();
|
||||
}
|
||||
private void ThrowIfDisposed() { if (_disposed) throw new ObjectDisposedException(nameof(ManagedSchemaChannel)); }
|
||||
|
||||
private interface IChannelRegistration { void Attach(ICultNetSchemaClient physical); }
|
||||
private sealed class ChannelRegistration<T> : IChannelRegistration where T : ICultNetSchemaMessage
|
||||
{
|
||||
private readonly Action<T> _callback;
|
||||
public ChannelRegistration(Action<T> callback) => _callback = callback;
|
||||
public void Attach(ICultNetSchemaClient physical) => physical.OnCultNet(_callback);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class SessionSubscription<T> : IDisposable where T : ICultNetSchemaMessage
|
||||
{
|
||||
private Action<T>? _callback;
|
||||
|
|
@ -276,7 +352,7 @@ namespace GameCult.Mesh
|
|||
try
|
||||
{
|
||||
var session = await AwaitForCallerAsync(lazy.Value, cancellationToken).ConfigureAwait(false);
|
||||
if (session.State.Status == CultMeshSessionStatus.Online) return session;
|
||||
if (session.State.Status != CultMeshSessionStatus.Offline) return session;
|
||||
_sessions.TryRemove(key, out _);
|
||||
}
|
||||
catch
|
||||
|
|
@ -288,6 +364,16 @@ namespace GameCult.Mesh
|
|||
}
|
||||
|
||||
private async Task<CultMeshSession> ConnectOwnedAsync(string key, CultMeshEndpointId endpointId, CultMeshProtocolId protocol)
|
||||
{
|
||||
var result = await ResolveConnectedPathAsync(endpointId, protocol).ConfigureAwait(false);
|
||||
var session = new CultMeshSession(endpointId, protocol, result.Client,
|
||||
new CultMeshSessionState(CultMeshSessionStatus.Online, _options.Clock.UtcNow, result.Candidate));
|
||||
ObservePhysicalFailure(key, session, result);
|
||||
Emit(endpointId, protocol, "online", result.Candidate.Endpoint);
|
||||
return session;
|
||||
}
|
||||
|
||||
private async Task<ConnectedPath> ResolveConnectedPathAsync(CultMeshEndpointId endpointId, CultMeshProtocolId protocol)
|
||||
{
|
||||
var discovery = await _discovery.ResolveAsync(new CultMeshDiscoveryQuery(endpointId.Value)).ConfigureAwait(false);
|
||||
var candidates = discovery.Candidates.SelectMany(candidate => candidate.Descriptor.DiscoveryEndpoints)
|
||||
|
|
@ -310,26 +396,7 @@ namespace GameCult.Mesh
|
|||
var result = await completed.ConfigureAwait(false);
|
||||
foreach (var loser in attempts)
|
||||
_ = loser.ContinueWith(task => { if (task.Status == TaskStatus.RanToCompletion) task.Result.Client.Dispose(); }, TaskScheduler.Default);
|
||||
var session = new CultMeshSession(endpointId, protocol, result.Client,
|
||||
new CultMeshSessionState(CultMeshSessionStatus.Online, _options.Clock.UtcNow, result.Candidate));
|
||||
if (result.Client is ICultNetSchemaClientHealth health)
|
||||
{
|
||||
_ = health.BackgroundFailure.ContinueWith(task =>
|
||||
{
|
||||
var error = task.Status == TaskStatus.RanToCompletion ? task.Result : task.Exception;
|
||||
session.Transition(new CultMeshSessionState(
|
||||
CultMeshSessionStatus.Offline,
|
||||
_options.Clock.UtcNow,
|
||||
result.Candidate,
|
||||
new CultMeshSessionFailure(
|
||||
CultMeshSessionFailureReason.Transport,
|
||||
error?.Message ?? "Transport background loop stopped.",
|
||||
result.Candidate.Endpoint)));
|
||||
_sessions.TryRemove(key, out _);
|
||||
}, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
|
||||
}
|
||||
Emit(endpointId, protocol, "online", result.Candidate.Endpoint);
|
||||
return session;
|
||||
return result;
|
||||
}
|
||||
catch (Exception error) { failures.Add(error); }
|
||||
}
|
||||
|
|
@ -338,6 +405,60 @@ namespace GameCult.Mesh
|
|||
$"No transport path connected for '{endpointId}' and protocol '{protocol}'.", last);
|
||||
}
|
||||
|
||||
private void ObservePhysicalFailure(string key, CultMeshSession session, ConnectedPath path)
|
||||
{
|
||||
if (path.Client is not ICultNetSchemaClientHealth health) return;
|
||||
_ = health.BackgroundFailure.ContinueWith(
|
||||
task => ReconnectOwnedAsync(key, session, path, task),
|
||||
CancellationToken.None,
|
||||
TaskContinuationOptions.ExecuteSynchronously,
|
||||
TaskScheduler.Default).Unwrap();
|
||||
}
|
||||
|
||||
private async Task ReconnectOwnedAsync(
|
||||
string key,
|
||||
CultMeshSession session,
|
||||
ConnectedPath failedPath,
|
||||
Task<Exception> failureTask)
|
||||
{
|
||||
if (_disposed || session.State.Status == CultMeshSessionStatus.Offline) return;
|
||||
var error = failureTask.Status == TaskStatus.RanToCompletion ? failureTask.Result : failureTask.Exception;
|
||||
var failure = new CultMeshSessionFailure(
|
||||
CultMeshSessionFailureReason.Transport,
|
||||
error?.Message ?? "Transport background loop stopped.",
|
||||
failedPath.Candidate.Endpoint);
|
||||
session.Transition(new CultMeshSessionState(
|
||||
CultMeshSessionStatus.Reconnecting,
|
||||
_options.Clock.UtcNow,
|
||||
failedPath.Candidate,
|
||||
failure));
|
||||
Emit(session.EndpointId, session.Protocol, "reconnecting", failedPath.Candidate.Endpoint);
|
||||
try
|
||||
{
|
||||
var replacement = await ResolveConnectedPathAsync(session.EndpointId, session.Protocol).ConfigureAwait(false);
|
||||
session.ReplacePhysicalChannel(replacement.Client);
|
||||
session.Transition(new CultMeshSessionState(
|
||||
CultMeshSessionStatus.Online,
|
||||
_options.Clock.UtcNow,
|
||||
replacement.Candidate));
|
||||
ObservePhysicalFailure(key, session, replacement);
|
||||
Emit(session.EndpointId, session.Protocol, "online", replacement.Candidate.Endpoint);
|
||||
}
|
||||
catch (Exception reconnectError)
|
||||
{
|
||||
session.Transition(new CultMeshSessionState(
|
||||
CultMeshSessionStatus.Offline,
|
||||
_options.Clock.UtcNow,
|
||||
failedPath.Candidate,
|
||||
new CultMeshSessionFailure(
|
||||
reconnectError is CultMeshSessionException typed ? typed.Failure.Reason : CultMeshSessionFailureReason.Transport,
|
||||
reconnectError.Message,
|
||||
failedPath.Candidate.Endpoint)));
|
||||
_sessions.TryRemove(key, out _);
|
||||
Emit(session.EndpointId, session.Protocol, "offline", failedPath.Candidate.Endpoint);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<ConnectedPath> ConnectCandidateAsync(CultMeshTransportCandidate candidate, CultMeshProtocolId protocol)
|
||||
{
|
||||
var connector = _connectors.FirstOrDefault(item => item.CanConnect(candidate));
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ using System.Threading.Tasks;
|
|||
using FluentAssertions;
|
||||
using GameCult.Networking;
|
||||
using NUnit.Framework;
|
||||
using R3;
|
||||
|
||||
namespace GameCult.Mesh.Tests;
|
||||
|
||||
|
|
@ -83,7 +84,7 @@ public sealed class CultMeshSessionManagerTests
|
|||
}
|
||||
|
||||
[Test]
|
||||
public async Task BackgroundFailureEvictsSessionAndEndpointRotationReconnects()
|
||||
public async Task BackgroundFailureMigratesSameSessionAcrossEndpointRotation()
|
||||
{
|
||||
var clock = new ManualSessionClock();
|
||||
var route = "rudp://first:3076";
|
||||
|
|
@ -98,16 +99,26 @@ public sealed class CultMeshSessionManagerTests
|
|||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, new CultMeshSessionManagerOptions { Clock = clock });
|
||||
var endpoint = CultMeshEndpointId.Parse("odin:aetheria");
|
||||
var first = await manager.ConnectAsync(endpoint, CultMeshProtocols.Documents);
|
||||
var states = new List<CultMeshSessionStatus>();
|
||||
using var stateWatch = first.WatchState().Subscribe(state => states.Add(state.Status));
|
||||
using var schemaLease = first.OpenSchemaClient();
|
||||
var deliveries = 0;
|
||||
schemaLease.OnCultNet<CultNetErrorMessage>(_ => deliveries++);
|
||||
clients[0].Emit(new CultNetErrorMessage { Error = "before rotation" });
|
||||
|
||||
route = "rudp://second:3076";
|
||||
clock.Advance(TimeSpan.FromSeconds(6));
|
||||
clients[0].Fail(new IOException("partition"));
|
||||
await WaitUntilAsync(() => first.State.Status == CultMeshSessionStatus.Offline);
|
||||
await WaitUntilAsync(() => first.State.Status == CultMeshSessionStatus.Online && connector.ConnectCount == 2);
|
||||
var second = await manager.ConnectAsync(endpoint, CultMeshProtocols.Documents);
|
||||
clients[1].Emit(new CultNetErrorMessage { Error = "after rotation" });
|
||||
|
||||
second.Should().NotBeSameAs(first);
|
||||
second.Should().BeSameAs(first);
|
||||
second.State.Path!.Endpoint.Should().Be("rudp://second:3076");
|
||||
states.Should().ContainInOrder(CultMeshSessionStatus.Reconnecting, CultMeshSessionStatus.Online);
|
||||
connector.ConnectCount.Should().Be(2);
|
||||
clients[0].DisposeCount.Should().Be(1);
|
||||
deliveries.Should().Be(2);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
|
@ -244,15 +255,21 @@ public sealed class CultMeshSessionManagerTests
|
|||
private sealed class FakeSchemaClient : ICultNetSchemaClient, ICultNetSchemaClientHealth
|
||||
{
|
||||
private readonly TaskCompletionSource<Exception> _failure = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
private readonly List<Action<CultNetErrorMessage>> _handlers = new();
|
||||
private int _disposeCount;
|
||||
public bool Connected => true;
|
||||
public Task<Exception> BackgroundFailure => _failure.Task;
|
||||
public int DisposeCount => Volatile.Read(ref _disposeCount);
|
||||
public void Connect(string host, int port) { }
|
||||
public void SendCultNet<T>(T message) where T : ICultNetSchemaMessage { }
|
||||
public void OnCultNet<T>(Action<T> callback) where T : ICultNetSchemaMessage { }
|
||||
public void OnCultNet<T>(Action<T> callback) where T : ICultNetSchemaMessage
|
||||
{
|
||||
if (typeof(T) == typeof(CultNetErrorMessage))
|
||||
_handlers.Add(message => callback((T)(object)message));
|
||||
}
|
||||
public void Dispose() => Interlocked.Increment(ref _disposeCount);
|
||||
public void Fail(Exception error) => _failure.TrySetResult(error);
|
||||
public void Emit(CultNetErrorMessage message) { foreach (var handler in _handlers.ToArray()) handler(message); }
|
||||
}
|
||||
|
||||
private sealed class RespondingSchemaClient : ICultNetSchemaClient
|
||||
|
|
|
|||
Loading…
Reference in a new issue