Harden CultMesh subscription readiness

This commit is contained in:
Meta Vens 2026-07-13 04:19:54 +01:00
commit 61257d5a20
4 changed files with 96 additions and 19 deletions

View file

@ -115,17 +115,25 @@ namespace GameCult.Mesh
}
/// <summary>Opens a live collection of one typed schema by stable provider identity.</summary>
public Task<CultMeshCollectionHandle<TDocument>> CollectionAsync<TDocument>(
string endpointId,
CancellationToken cancellationToken)
where TDocument : class =>
CollectionAsync<TDocument>(endpointId, includeInitialSnapshot: true, cancellationToken);
/// <summary>Opens a live collection and optionally skips replaying its initial snapshot.</summary>
public async Task<CultMeshCollectionHandle<TDocument>> CollectionAsync<TDocument>(
string endpointId,
bool includeInitialSnapshot = true,
CancellationToken cancellationToken = default)
where TDocument : class
{
ThrowIfDisposed();
if (string.IsNullOrWhiteSpace(endpointId)) throw new ArgumentException("Endpoint identity is required.", nameof(endpointId));
var identity = endpointId.Trim();
var key = identity + "\u001f" + typeof(TDocument).AssemblyQualifiedName;
var key = identity + "\u001f" + typeof(TDocument).AssemblyQualifiedName + "\u001f" + includeInitialSnapshot;
var lazy = _collections.GetOrAdd(key, _ => new Lazy<Task<object>>(
async () => await OpenCollectionAsync<TDocument>(identity).ConfigureAwait(false),
async () => await OpenCollectionAsync<TDocument>(identity, includeInitialSnapshot).ConfigureAwait(false),
LazyThreadSafetyMode.ExecutionAndPublication));
try
{
@ -157,12 +165,44 @@ namespace GameCult.Mesh
$"CultMesh endpoint '{endpointId}' did not publish {typeof(TDocument).FullName} record '{recordKey}'.");
}
/// <summary>Reads one typed record once with an explicit response deadline.</summary>
public async Task<TDocument> ReadAsync<TDocument>(
string endpointId,
string recordKey,
TimeSpan responseTimeout,
CancellationToken cancellationToken = default)
where TDocument : class
{
var documents = await ReadManyCoreAsync<TDocument>(
endpointId,
new[] { recordKey },
responseTimeout,
cancellationToken)
.ConfigureAwait(false);
return documents.FirstOrDefault()
?? throw new InvalidOperationException(
$"CultMesh endpoint '{endpointId}' did not publish {typeof(TDocument).FullName} record '{recordKey}'.");
}
/// <summary>Reads typed records once in one request through the reusable document session.</summary>
public async Task<IReadOnlyList<TDocument>> ReadManyAsync<TDocument>(
string endpointId,
IReadOnlyList<string> recordKeys,
CancellationToken cancellationToken = default)
where TDocument : class
=> await ReadManyCoreAsync<TDocument>(
endpointId,
recordKeys,
TimeSpan.FromSeconds(10),
cancellationToken)
.ConfigureAwait(false);
private async Task<IReadOnlyList<TDocument>> ReadManyCoreAsync<TDocument>(
string endpointId,
IReadOnlyList<string> recordKeys,
TimeSpan responseTimeout,
CancellationToken cancellationToken)
where TDocument : class
{
ThrowIfDisposed();
if (string.IsNullOrWhiteSpace(endpointId)) throw new ArgumentException("Endpoint identity is required.", nameof(endpointId));
@ -177,7 +217,7 @@ namespace GameCult.Mesh
new CultMeshSnapshotRequestOptions
{
ConnectTimeout = TimeSpan.FromSeconds(5),
ResponseTimeout = TimeSpan.FromSeconds(10),
ResponseTimeout = responseTimeout,
MessageIdPrefix = "cultmesh-client-read"
},
networkRegistry,
@ -223,11 +263,11 @@ namespace GameCult.Mesh
}
}
private async Task<object> OpenCollectionAsync<TDocument>(string endpointId)
private async Task<object> OpenCollectionAsync<TDocument>(string endpointId, bool includeInitialSnapshot)
where TDocument : class
{
var session = await ConnectAsync(endpointId, CultMeshProtocols.Documents).ConfigureAwait(false);
var binding = new RemoteCollectionBinding<TDocument>(session, endpointId);
var binding = new RemoteCollectionBinding<TDocument>(session, endpointId, includeInitialSnapshot);
_documentResources.Add(binding);
try
{
@ -261,6 +301,8 @@ namespace GameCult.Mesh
private readonly CultCache _cache;
private readonly CultNetDatabaseSubscriptionClient _subscription;
private readonly TaskCompletionSource<bool> _ready = new(TaskCreationOptions.RunContinuationsAsynchronously);
private readonly object _subscribeLock = new();
private Task? _subscribeTask;
private IDisposable? _stateWatch;
private bool _disposed;
@ -286,11 +328,20 @@ namespace GameCult.Mesh
{
_stateWatch = _session.WatchState()
.Where(state => state.Status == CultMeshSessionStatus.Online)
.Subscribe(state => { _ = SubscribeAsync(); });
_ = SubscribeAsync();
.Subscribe(state => { _ = EnsureSubscribedAsync(); });
_ = EnsureSubscribedAsync();
await _ready.Task.ConfigureAwait(false);
}
private Task EnsureSubscribedAsync()
{
lock (_subscribeLock)
{
if (_subscribeTask is { IsCompleted: false }) return _subscribeTask;
return _subscribeTask = SubscribeAsync();
}
}
private async Task SubscribeAsync()
{
try
@ -325,14 +376,18 @@ namespace GameCult.Mesh
private readonly string _subscriptionId;
private readonly CultCache _cache;
private readonly CultNetDatabaseSubscriptionClient _subscription;
private readonly bool _includeInitialSnapshot;
private readonly TaskCompletionSource<bool> _ready = new(TaskCreationOptions.RunContinuationsAsynchronously);
private readonly object _subscribeLock = new();
private Task? _subscribeTask;
private IDisposable? _stateWatch;
private bool _disposed;
public RemoteCollectionBinding(CultMeshSession session, string endpointId)
public RemoteCollectionBinding(CultMeshSession session, string endpointId, bool includeInitialSnapshot)
{
_session = session;
_subscriptionId = "cultmesh-collection:" + endpointId + ":" + typeof(TDocument).FullName;
_includeInitialSnapshot = includeInitialSnapshot;
var cacheRegistry = CultMesh.CreateCultCacheDocumentRegistry(typeof(TDocument));
var networkRegistry = CultMesh.CreateCultNetDocumentRegistry(new[] { typeof(TDocument) }, cacheRegistry);
_cache = new CultCache(cacheRegistry);
@ -348,11 +403,20 @@ namespace GameCult.Mesh
{
_stateWatch = _session.WatchState()
.Where(state => state.Status == CultMeshSessionStatus.Online)
.Subscribe(state => { _ = SubscribeAsync(); });
_ = SubscribeAsync();
.Subscribe(state => { _ = EnsureSubscribedAsync(); });
_ = EnsureSubscribedAsync();
await _ready.Task.ConfigureAwait(false);
}
private Task EnsureSubscribedAsync()
{
lock (_subscribeLock)
{
if (_subscribeTask is { IsCompleted: false }) return _subscribeTask;
return _subscribeTask = SubscribeAsync();
}
}
private async Task SubscribeAsync()
{
try
@ -360,7 +424,8 @@ namespace GameCult.Mesh
if (_disposed) return;
await _subscription.SubscribeAsync(
_subscriptionId,
schemaIds: new[] { CultDocumentRegistry.Shared.GetRequired<TDocument>().SchemaId })
schemaIds: new[] { CultDocumentRegistry.Shared.GetRequired<TDocument>().SchemaId },
includeSnapshot: _includeInitialSnapshot)
.ConfigureAwait(false);
_ready.TrySetResult(true);
}

View file

@ -303,6 +303,14 @@ namespace GameCult.Networking
RecordKeys = message.RecordKeys
}));
}
else
{
peer.SendCultNet(new CultNetSnapshotResponseRawMessage
{
MessageId = message.MessageId,
Documents = Array.Empty<CultNetRawDocumentRecord>()
});
}
return Task.CompletedTask;
}

View file

@ -53,13 +53,9 @@ namespace GameCult.Networking
if (string.IsNullOrWhiteSpace(subscriptionId)) throw new ArgumentException("Subscription id is required.", nameof(subscriptionId));
var messageId = Guid.NewGuid().ToString("N");
TaskCompletionSource<IReadOnlyList<object>>? completion = null;
if (includeSnapshot)
{
completion = new TaskCompletionSource<IReadOnlyList<object>>(TaskCreationOptions.RunContinuationsAsynchronously);
if (!_initialSnapshots.TryAdd(messageId, completion))
throw new InvalidOperationException($"Duplicate database subscription message id '{messageId}'.");
}
var completion = new TaskCompletionSource<IReadOnlyList<object>>(TaskCreationOptions.RunContinuationsAsynchronously);
if (!_initialSnapshots.TryAdd(messageId, completion))
throw new InvalidOperationException($"Duplicate database subscription message id '{messageId}'.");
_client.SendCultNet(new CultNetDatabaseSubscribeMessage
{
@ -69,7 +65,7 @@ namespace GameCult.Networking
SchemaIds = schemaIds?.Where(value => !string.IsNullOrWhiteSpace(value)).Distinct(StringComparer.Ordinal).ToArray(),
IncludeSnapshot = includeSnapshot
});
return completion?.Task ?? Task.FromResult<IReadOnlyList<object>>(Array.Empty<object>());
return completion.Task;
}
/// <summary>Stops one live remote subscription.</summary>

View file

@ -77,6 +77,14 @@ namespace GameCult.Networking
RecordKeys = request.RecordKeys
}));
}
else
{
peer.SendCultNet(new CultNetSnapshotResponseRawMessage
{
MessageId = request.MessageId,
Documents = Array.Empty<CultNetRawDocumentRecord>()
});
}
return Task.CompletedTask;
}