2026-07-12 20:56:45 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2026-07-12 21:03:55 +01:00
|
|
|
using System.Collections.Concurrent;
|
2026-07-12 20:56:45 +01:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2026-07-12 21:03:55 +01:00
|
|
|
using GameCult.Caching;
|
2026-07-12 20:56:45 +01:00
|
|
|
using GameCult.Networking;
|
2026-07-12 21:03:55 +01:00
|
|
|
using R3;
|
2026-07-12 20:56:45 +01:00
|
|
|
|
|
|
|
|
namespace GameCult.Mesh
|
|
|
|
|
{
|
|
|
|
|
/// <summary>Configures one application-lifetime CultMesh client.</summary>
|
|
|
|
|
public sealed class CultMeshClientOptions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>Gets or sets the rendezvous endpoints used to bootstrap stable identities.</summary>
|
|
|
|
|
public IReadOnlyList<string> RendezvousEndpoints { get; set; } = Array.Empty<string>();
|
|
|
|
|
|
|
|
|
|
/// <summary>Gets or sets Verse discovery transport and persistence options.</summary>
|
|
|
|
|
public CultMeshVerseDiscoveryClientOptions Discovery { get; set; } = new();
|
|
|
|
|
|
|
|
|
|
/// <summary>Gets or sets session path and diagnostic policy.</summary>
|
|
|
|
|
public CultMeshSessionManagerOptions Sessions { get; set; } = new();
|
|
|
|
|
|
2026-07-19 17:57:26 +01:00
|
|
|
/// <summary>Gets or sets schema connectors. Length-prefixed TCP is used by default.</summary>
|
2026-07-12 20:56:45 +01:00
|
|
|
public IReadOnlyList<ICultMeshTransportConnector>? Connectors { get; set; }
|
2026-07-13 04:43:27 +01:00
|
|
|
|
2026-07-19 17:57:26 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets streaming content connectors. TCP content delivery is used by default;
|
|
|
|
|
/// applications may replace it or explicitly add legacy RUDP.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IReadOnlyList<ICultMeshContentTransportConnector>? ContentConnectors { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets realtime state connectors. No connector is installed implicitly;
|
|
|
|
|
/// promoted runtimes should register QUIC and may explicitly add a legacy fallback.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IReadOnlyList<ICultMeshRealtimeTransportConnector> RealtimeConnectors { get; set; } =
|
|
|
|
|
Array.Empty<ICultMeshRealtimeTransportConnector>();
|
|
|
|
|
|
2026-07-13 04:43:27 +01:00
|
|
|
/// <summary>Gets or sets the deadline before an unanswered subscription intent is replayed.</summary>
|
|
|
|
|
public TimeSpan SubscriptionResponseTimeout { get; set; } = TimeSpan.FromSeconds(2);
|
2026-07-12 20:56:45 +01:00
|
|
|
}
|
|
|
|
|
|
2026-08-17 20:39:24 +02:00
|
|
|
/// <summary>Owns one shared live document subscription until disposed.</summary>
|
|
|
|
|
public sealed class CultMeshDocumentLease<TDocument> : IDisposable where TDocument : class
|
|
|
|
|
{
|
|
|
|
|
private Action? _release;
|
|
|
|
|
|
|
|
|
|
internal CultMeshDocumentLease(CultMeshDocumentHandle<TDocument> handle, Action release)
|
|
|
|
|
{
|
|
|
|
|
Handle = handle ?? throw new ArgumentNullException(nameof(handle));
|
|
|
|
|
_release = release ?? throw new ArgumentNullException(nameof(release));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Gets the live typed document handle owned by this lease.</summary>
|
|
|
|
|
public CultMeshDocumentHandle<TDocument> Handle { get; }
|
|
|
|
|
|
|
|
|
|
public void Dispose() => Interlocked.Exchange(ref _release, null)?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Owns one shared live collection subscription until disposed.</summary>
|
|
|
|
|
public sealed class CultMeshCollectionLease<TDocument> : IDisposable where TDocument : class
|
|
|
|
|
{
|
|
|
|
|
private Action? _release;
|
|
|
|
|
|
|
|
|
|
internal CultMeshCollectionLease(CultMeshCollectionHandle<TDocument> handle, Action release)
|
|
|
|
|
{
|
|
|
|
|
Handle = handle ?? throw new ArgumentNullException(nameof(handle));
|
|
|
|
|
_release = release ?? throw new ArgumentNullException(nameof(release));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Gets the live typed collection handle owned by this lease.</summary>
|
|
|
|
|
public CultMeshCollectionHandle<TDocument> Handle { get; }
|
|
|
|
|
|
|
|
|
|
public void Dispose() => Interlocked.Exchange(ref _release, null)?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 20:56:45 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Owns discovery and reusable sessions for one application lifetime.
|
|
|
|
|
/// Applications address stable identities; CultMesh owns physical routes and reconnection.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class CultMeshClient : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly CultMeshDiscoveryService _discovery;
|
|
|
|
|
private readonly CultMeshSessionManager _sessions;
|
2026-07-13 04:43:27 +01:00
|
|
|
private readonly TimeSpan _subscriptionResponseTimeout;
|
2026-08-17 20:39:24 +02:00
|
|
|
private readonly ConcurrentDictionary<string, SharedResource> _documents = new(StringComparer.Ordinal);
|
|
|
|
|
private readonly ConcurrentDictionary<string, SharedResource> _collections = new(StringComparer.Ordinal);
|
2026-07-12 20:56:45 +01:00
|
|
|
private bool _disposed;
|
|
|
|
|
|
|
|
|
|
public CultMeshClient(CultMeshClientOptions options)
|
|
|
|
|
{
|
|
|
|
|
if (options == null) throw new ArgumentNullException(nameof(options));
|
2026-07-13 04:43:27 +01:00
|
|
|
if (options.SubscriptionResponseTimeout <= TimeSpan.Zero)
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(options.SubscriptionResponseTimeout));
|
2026-07-12 20:56:45 +01:00
|
|
|
var endpoints = options.RendezvousEndpoints
|
|
|
|
|
.Where(endpoint => !string.IsNullOrWhiteSpace(endpoint))
|
|
|
|
|
.Distinct(StringComparer.Ordinal)
|
|
|
|
|
.ToArray();
|
|
|
|
|
if (endpoints.Length == 0)
|
|
|
|
|
throw new ArgumentException("At least one CultMesh rendezvous endpoint is required.", nameof(options));
|
|
|
|
|
|
|
|
|
|
var discoveryClient = new CultMeshVerseDiscoveryClient(options.Discovery);
|
|
|
|
|
_discovery = new CultMeshDiscoveryService(
|
|
|
|
|
endpoints.Select(endpoint => new RendezvousLookupSource(endpoint, discoveryClient, options.Discovery)),
|
|
|
|
|
new CultMeshDiscoveryServiceOptions
|
|
|
|
|
{
|
|
|
|
|
Clock = options.Discovery.Clock,
|
|
|
|
|
Diagnostics = options.Discovery.Diagnostics,
|
|
|
|
|
Store = options.Discovery.DiscoveryStore
|
|
|
|
|
});
|
|
|
|
|
_sessions = new CultMeshSessionManager(
|
|
|
|
|
_discovery,
|
|
|
|
|
options.Connectors ?? new ICultMeshTransportConnector[]
|
|
|
|
|
{
|
2026-07-19 17:57:26 +01:00
|
|
|
new CultMeshTcpSchemaTransportConnector(clock: options.Sessions.Clock)
|
|
|
|
|
},
|
|
|
|
|
options.ContentConnectors ?? new ICultMeshContentTransportConnector[]
|
|
|
|
|
{
|
|
|
|
|
new CultMeshTcpContentTransportConnector()
|
2026-07-12 20:56:45 +01:00
|
|
|
},
|
2026-07-19 17:57:26 +01:00
|
|
|
options.RealtimeConnectors,
|
2026-07-12 20:56:45 +01:00
|
|
|
options.Sessions);
|
2026-07-13 04:43:27 +01:00
|
|
|
_subscriptionResponseTimeout = options.SubscriptionResponseTimeout;
|
2026-07-12 20:56:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Connects to a stable endpoint identity using one application protocol.</summary>
|
|
|
|
|
public Task<CultMeshSession> ConnectAsync(
|
|
|
|
|
string endpointId,
|
|
|
|
|
CultMeshProtocolId protocol,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
ThrowIfDisposed();
|
|
|
|
|
return _sessions.ConnectAsync(CultMeshEndpointId.Parse(endpointId), protocol, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-19 17:57:26 +01:00
|
|
|
/// <summary>Connects the reusable realtime state plane by stable endpoint identity.</summary>
|
|
|
|
|
public Task<CultMeshRealtimeSession> ConnectRealtimeAsync(
|
|
|
|
|
string endpointId,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
ThrowIfDisposed();
|
|
|
|
|
return _sessions.ConnectRealtimeAsync(CultMeshEndpointId.Parse(endpointId), cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 20:56:45 +01:00
|
|
|
/// <summary>Connects to a stable endpoint identity using one application protocol.</summary>
|
|
|
|
|
public Task<CultMeshSession> ConnectAsync(
|
|
|
|
|
CultMeshEndpointId endpointId,
|
|
|
|
|
CultMeshProtocolId protocol,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
ThrowIfDisposed();
|
|
|
|
|
return _sessions.ConnectAsync(endpointId, protocol, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-14 01:19:26 +01:00
|
|
|
/// <summary>Creates a verified-transfer provider over this client's reusable content session owner.</summary>
|
|
|
|
|
public CultMeshSessionContentProvider ContentProvider(
|
|
|
|
|
string providerId,
|
2026-07-19 17:57:26 +01:00
|
|
|
string endpointId)
|
2026-07-14 01:19:26 +01:00
|
|
|
{
|
|
|
|
|
ThrowIfDisposed();
|
|
|
|
|
return new CultMeshSessionContentProvider(
|
|
|
|
|
providerId,
|
|
|
|
|
_sessions,
|
2026-07-19 17:57:26 +01:00
|
|
|
CultMeshEndpointId.Parse(endpointId));
|
2026-07-14 01:19:26 +01:00
|
|
|
}
|
|
|
|
|
|
2026-07-19 13:57:06 +01:00
|
|
|
/// <summary>Creates a direct body provider over this client's reusable negotiated session owner.</summary>
|
|
|
|
|
public CultMeshSessionBodyProvider BodyProvider(
|
|
|
|
|
string providerId,
|
|
|
|
|
string endpointId,
|
|
|
|
|
CultMeshSessionBodyProviderOptions? options = null)
|
|
|
|
|
{
|
|
|
|
|
ThrowIfDisposed();
|
|
|
|
|
return new CultMeshSessionBodyProvider(
|
|
|
|
|
providerId,
|
|
|
|
|
_sessions,
|
|
|
|
|
CultMeshEndpointId.Parse(endpointId),
|
|
|
|
|
options);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 20:39:24 +02:00
|
|
|
/// <summary>Leases one shared live typed document by stable provider identity and record key.</summary>
|
|
|
|
|
public async Task<CultMeshDocumentLease<TDocument>> LeaseDocumentAsync<TDocument>(
|
2026-07-12 21:03:55 +01:00
|
|
|
string endpointId,
|
|
|
|
|
string recordKey,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
|
where TDocument : class
|
|
|
|
|
{
|
|
|
|
|
ThrowIfDisposed();
|
|
|
|
|
if (string.IsNullOrWhiteSpace(endpointId)) throw new ArgumentException("Endpoint identity is required.", nameof(endpointId));
|
|
|
|
|
if (string.IsNullOrWhiteSpace(recordKey)) throw new ArgumentException("Record key is required.", nameof(recordKey));
|
|
|
|
|
var key = endpointId.Trim() + "\u001f" + typeof(TDocument).AssemblyQualifiedName + "\u001f" + recordKey.Trim();
|
2026-08-17 20:39:24 +02:00
|
|
|
while (true)
|
2026-07-12 21:03:55 +01:00
|
|
|
{
|
2026-08-17 20:39:24 +02:00
|
|
|
var resource = _documents.GetOrAdd(key, _ => new SharedResource(
|
|
|
|
|
async owner => await OpenDocumentAsync<TDocument>(endpointId.Trim(), recordKey.Trim(), owner).ConfigureAwait(false)));
|
|
|
|
|
if (!resource.TryAcquire())
|
|
|
|
|
{
|
|
|
|
|
RemoveExact(_documents, key, resource);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var binding = (RemoteDocumentBinding<TDocument>)await AwaitForCallerAsync(resource.Value, cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
return new CultMeshDocumentLease<TDocument>(
|
|
|
|
|
binding.Handle,
|
|
|
|
|
() => Release(_documents, key, resource));
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
Release(_documents, key, resource);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2026-07-12 21:03:55 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 20:39:24 +02:00
|
|
|
/// <summary>Leases a shared live collection of one typed schema by stable provider identity.</summary>
|
|
|
|
|
public Task<CultMeshCollectionLease<TDocument>> LeaseCollectionAsync<TDocument>(
|
2026-07-13 04:19:54 +01:00
|
|
|
string endpointId,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
where TDocument : class =>
|
2026-08-17 20:39:24 +02:00
|
|
|
LeaseCollectionAsync<TDocument>(endpointId, includeInitialSnapshot: true, cancellationToken);
|
2026-07-13 04:19:54 +01:00
|
|
|
|
2026-08-17 20:39:24 +02:00
|
|
|
/// <summary>Leases a live collection and optionally skips replaying its initial snapshot.</summary>
|
|
|
|
|
public async Task<CultMeshCollectionLease<TDocument>> LeaseCollectionAsync<TDocument>(
|
2026-07-12 21:07:21 +01:00
|
|
|
string endpointId,
|
2026-07-13 04:19:54 +01:00
|
|
|
bool includeInitialSnapshot = true,
|
2026-07-12 21:07:21 +01:00
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
|
where TDocument : class
|
|
|
|
|
{
|
|
|
|
|
ThrowIfDisposed();
|
|
|
|
|
if (string.IsNullOrWhiteSpace(endpointId)) throw new ArgumentException("Endpoint identity is required.", nameof(endpointId));
|
|
|
|
|
var identity = endpointId.Trim();
|
2026-07-13 04:19:54 +01:00
|
|
|
var key = identity + "\u001f" + typeof(TDocument).AssemblyQualifiedName + "\u001f" + includeInitialSnapshot;
|
2026-08-17 20:39:24 +02:00
|
|
|
while (true)
|
2026-07-12 21:07:21 +01:00
|
|
|
{
|
2026-08-17 20:39:24 +02:00
|
|
|
var resource = _collections.GetOrAdd(key, _ => new SharedResource(
|
|
|
|
|
async owner => await OpenCollectionAsync<TDocument>(identity, includeInitialSnapshot, owner).ConfigureAwait(false)));
|
|
|
|
|
if (!resource.TryAcquire())
|
|
|
|
|
{
|
|
|
|
|
RemoveExact(_collections, key, resource);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var binding = (RemoteCollectionBinding<TDocument>)await AwaitForCallerAsync(resource.Value, cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
return new CultMeshCollectionLease<TDocument>(
|
|
|
|
|
binding.Handle,
|
|
|
|
|
() => Release(_collections, key, resource));
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
Release(_collections, key, resource);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2026-07-12 21:07:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 20:39:24 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Submits one typed document to a provider by stable identity. Submission is uncommitted input;
|
|
|
|
|
/// only a provider-authored receipt or resulting state can establish acceptance.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task SubmitDocumentAsync<TDocument>(
|
|
|
|
|
string endpointId,
|
|
|
|
|
string recordKey,
|
|
|
|
|
TDocument document,
|
|
|
|
|
string sourceRuntimeId,
|
|
|
|
|
string sourceRole,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
|
where TDocument : class
|
|
|
|
|
{
|
|
|
|
|
ThrowIfDisposed();
|
|
|
|
|
if (string.IsNullOrWhiteSpace(endpointId)) throw new ArgumentException("Endpoint identity is required.", nameof(endpointId));
|
|
|
|
|
if (string.IsNullOrWhiteSpace(recordKey)) throw new ArgumentException("Record key is required.", nameof(recordKey));
|
|
|
|
|
if (document == null) throw new ArgumentNullException(nameof(document));
|
|
|
|
|
if (string.IsNullOrWhiteSpace(sourceRuntimeId)) throw new ArgumentException("Source runtime identity is required.", nameof(sourceRuntimeId));
|
|
|
|
|
if (string.IsNullOrWhiteSpace(sourceRole)) throw new ArgumentException("Source role is required.", nameof(sourceRole));
|
|
|
|
|
|
|
|
|
|
var cacheRegistry = CultMesh.CreateCultCacheDocumentRegistry(typeof(TDocument));
|
|
|
|
|
var networkRegistry = CultMesh.CreateCultNetDocumentRegistry(new[] { typeof(TDocument) }, cacheRegistry);
|
|
|
|
|
var message = networkRegistry.CreateRawDocumentPutMessage(
|
|
|
|
|
"cultmesh-client-submit-" + Guid.NewGuid().ToString("N"),
|
|
|
|
|
new CultRecordHandle<TDocument>(new CultRecordKey(recordKey.Trim())),
|
|
|
|
|
document,
|
|
|
|
|
new CultNetDocumentMessageOptions
|
|
|
|
|
{
|
|
|
|
|
SourceRuntimeId = sourceRuntimeId.Trim(),
|
|
|
|
|
SourceRole = sourceRole.Trim()
|
|
|
|
|
});
|
|
|
|
|
var session = await ConnectAsync(endpointId.Trim(), CultMeshProtocols.Documents, cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
session.OpenSchemaClient().SendCultNet(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Gets the number of currently leased shared document subscriptions.</summary>
|
|
|
|
|
public int ActiveDocumentResourceCount => _documents.Count;
|
|
|
|
|
|
|
|
|
|
/// <summary>Gets the number of currently leased shared collection subscriptions.</summary>
|
|
|
|
|
public int ActiveCollectionResourceCount => _collections.Count;
|
|
|
|
|
|
2026-07-12 21:27:01 +01:00
|
|
|
/// <summary>Reads one typed record once through the reusable document session.</summary>
|
|
|
|
|
public async Task<TDocument> ReadAsync<TDocument>(
|
|
|
|
|
string endpointId,
|
|
|
|
|
string recordKey,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
|
where TDocument : class
|
|
|
|
|
{
|
|
|
|
|
ThrowIfDisposed();
|
|
|
|
|
if (string.IsNullOrWhiteSpace(endpointId)) throw new ArgumentException("Endpoint identity is required.", nameof(endpointId));
|
|
|
|
|
if (string.IsNullOrWhiteSpace(recordKey)) throw new ArgumentException("Record key is required.", nameof(recordKey));
|
2026-07-12 21:48:28 +01:00
|
|
|
var documents = await ReadManyAsync<TDocument>(endpointId, new[] { recordKey }, cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
return documents.FirstOrDefault()
|
|
|
|
|
?? throw new InvalidOperationException(
|
|
|
|
|
$"CultMesh endpoint '{endpointId}' did not publish {typeof(TDocument).FullName} record '{recordKey}'.");
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-13 04:19:54 +01:00
|
|
|
/// <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}'.");
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 21:48:28 +01:00
|
|
|
/// <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
|
2026-07-13 04:19:54 +01:00
|
|
|
=> 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
|
2026-07-12 21:48:28 +01:00
|
|
|
{
|
|
|
|
|
ThrowIfDisposed();
|
|
|
|
|
if (string.IsNullOrWhiteSpace(endpointId)) throw new ArgumentException("Endpoint identity is required.", nameof(endpointId));
|
|
|
|
|
if (recordKeys == null || recordKeys.Count == 0 || recordKeys.Any(string.IsNullOrWhiteSpace))
|
|
|
|
|
throw new ArgumentException("At least one non-empty record key is required.", nameof(recordKeys));
|
2026-07-12 21:27:01 +01:00
|
|
|
var cacheRegistry = CultMesh.CreateCultCacheDocumentRegistry(typeof(TDocument));
|
|
|
|
|
var networkRegistry = CultMesh.CreateCultNetDocumentRegistry(new[] { typeof(TDocument) }, cacheRegistry);
|
|
|
|
|
using var snapshot = await CultMeshSnapshotSession.ConnectAsync(
|
|
|
|
|
_sessions,
|
|
|
|
|
CultMeshEndpointId.Parse(endpointId),
|
2026-07-12 21:55:46 +01:00
|
|
|
CultMeshProtocols.Content,
|
2026-07-12 21:52:29 +01:00
|
|
|
new CultMeshSnapshotRequestOptions
|
|
|
|
|
{
|
|
|
|
|
ConnectTimeout = TimeSpan.FromSeconds(5),
|
2026-07-13 04:19:54 +01:00
|
|
|
ResponseTimeout = responseTimeout,
|
2026-07-12 21:52:29 +01:00
|
|
|
MessageIdPrefix = "cultmesh-client-read"
|
|
|
|
|
},
|
2026-07-12 21:27:01 +01:00
|
|
|
networkRegistry,
|
|
|
|
|
cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
2026-07-12 21:48:28 +01:00
|
|
|
return await snapshot.FetchDocumentsAsync<TDocument>(
|
|
|
|
|
recordKeys: recordKeys,
|
2026-07-12 21:27:01 +01:00
|
|
|
schemaIds: new[] { cacheRegistry.GetRequired<TDocument>().SchemaId })
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 20:56:45 +01:00
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (_disposed) return;
|
|
|
|
|
_disposed = true;
|
2026-08-17 20:39:24 +02:00
|
|
|
foreach (var resource in _documents.Values.Concat(_collections.Values).Distinct()) resource.Dispose();
|
2026-07-12 21:03:55 +01:00
|
|
|
_documents.Clear();
|
2026-07-12 21:07:21 +01:00
|
|
|
_collections.Clear();
|
2026-07-12 20:56:45 +01:00
|
|
|
_sessions.Dispose();
|
|
|
|
|
_discovery.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ThrowIfDisposed()
|
|
|
|
|
{
|
|
|
|
|
if (_disposed) throw new ObjectDisposedException(nameof(CultMeshClient));
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 20:39:24 +02:00
|
|
|
private async Task<object> OpenDocumentAsync<TDocument>(
|
|
|
|
|
string endpointId,
|
|
|
|
|
string recordKey,
|
|
|
|
|
SharedResource owner)
|
2026-07-12 21:03:55 +01:00
|
|
|
where TDocument : class
|
|
|
|
|
{
|
|
|
|
|
var session = await ConnectAsync(endpointId, CultMeshProtocols.Documents).ConfigureAwait(false);
|
2026-07-13 04:43:27 +01:00
|
|
|
var binding = new RemoteDocumentBinding<TDocument>(session, endpointId, recordKey, _subscriptionResponseTimeout);
|
2026-08-17 20:39:24 +02:00
|
|
|
owner.Attach(binding);
|
2026-07-12 21:03:55 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await binding.StartAsync().ConfigureAwait(false);
|
|
|
|
|
return binding;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2026-08-17 20:39:24 +02:00
|
|
|
owner.Detach(binding);
|
2026-07-12 21:03:55 +01:00
|
|
|
binding.Dispose();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 20:39:24 +02:00
|
|
|
private async Task<object> OpenCollectionAsync<TDocument>(
|
|
|
|
|
string endpointId,
|
|
|
|
|
bool includeInitialSnapshot,
|
|
|
|
|
SharedResource owner)
|
2026-07-12 21:07:21 +01:00
|
|
|
where TDocument : class
|
|
|
|
|
{
|
|
|
|
|
var session = await ConnectAsync(endpointId, CultMeshProtocols.Documents).ConfigureAwait(false);
|
2026-07-13 04:43:27 +01:00
|
|
|
var binding = new RemoteCollectionBinding<TDocument>(session, endpointId, includeInitialSnapshot, _subscriptionResponseTimeout);
|
2026-08-17 20:39:24 +02:00
|
|
|
owner.Attach(binding);
|
2026-07-12 21:07:21 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await binding.StartAsync().ConfigureAwait(false);
|
|
|
|
|
return binding;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2026-08-17 20:39:24 +02:00
|
|
|
owner.Detach(binding);
|
2026-07-12 21:07:21 +01:00
|
|
|
binding.Dispose();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 21:03:55 +01:00
|
|
|
private static async Task<T> AwaitForCallerAsync<T>(Task<T> shared, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (!cancellationToken.CanBeCanceled) return await shared.ConfigureAwait(false);
|
|
|
|
|
var cancelled = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
|
|
|
using (cancellationToken.Register(() => cancelled.TrySetResult(true)))
|
|
|
|
|
{
|
|
|
|
|
if (await Task.WhenAny(shared, cancelled.Task).ConfigureAwait(false) != shared)
|
|
|
|
|
throw new OperationCanceledException(cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
return await shared.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 20:39:24 +02:00
|
|
|
private static void Release(
|
|
|
|
|
ConcurrentDictionary<string, SharedResource> resources,
|
|
|
|
|
string key,
|
|
|
|
|
SharedResource resource)
|
|
|
|
|
{
|
|
|
|
|
if (!resource.Release()) return;
|
|
|
|
|
RemoveExact(resources, key, resource);
|
|
|
|
|
resource.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void RemoveExact(
|
|
|
|
|
ConcurrentDictionary<string, SharedResource> resources,
|
|
|
|
|
string key,
|
|
|
|
|
SharedResource resource)
|
|
|
|
|
{
|
|
|
|
|
((ICollection<KeyValuePair<string, SharedResource>>)resources)
|
|
|
|
|
.Remove(new KeyValuePair<string, SharedResource>(key, resource));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private sealed class SharedResource : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly object _gate = new();
|
|
|
|
|
private readonly Lazy<Task<object>> _value;
|
|
|
|
|
private IDisposable? _attached;
|
|
|
|
|
private int _leases;
|
|
|
|
|
private bool _retired;
|
|
|
|
|
|
|
|
|
|
public SharedResource(Func<SharedResource, Task<object>> open)
|
|
|
|
|
{
|
|
|
|
|
if (open == null) throw new ArgumentNullException(nameof(open));
|
|
|
|
|
_value = new Lazy<Task<object>>(() => open(this), LazyThreadSafetyMode.ExecutionAndPublication);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<object> Value => _value.Value;
|
|
|
|
|
|
|
|
|
|
public bool TryAcquire()
|
|
|
|
|
{
|
|
|
|
|
lock (_gate)
|
|
|
|
|
{
|
|
|
|
|
if (_retired) return false;
|
|
|
|
|
_leases++;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Release()
|
|
|
|
|
{
|
|
|
|
|
lock (_gate)
|
|
|
|
|
{
|
|
|
|
|
if (_leases <= 0) return false;
|
|
|
|
|
_leases--;
|
|
|
|
|
if (_leases != 0) return false;
|
|
|
|
|
_retired = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Attach(IDisposable resource)
|
|
|
|
|
{
|
|
|
|
|
if (resource == null) throw new ArgumentNullException(nameof(resource));
|
|
|
|
|
lock (_gate)
|
|
|
|
|
{
|
|
|
|
|
if (_retired)
|
|
|
|
|
{
|
|
|
|
|
resource.Dispose();
|
|
|
|
|
throw new ObjectDisposedException(nameof(SharedResource));
|
|
|
|
|
}
|
|
|
|
|
if (_attached != null) throw new InvalidOperationException("CultMesh shared resource already has a live binding.");
|
|
|
|
|
_attached = resource;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Detach(IDisposable resource)
|
|
|
|
|
{
|
|
|
|
|
lock (_gate)
|
|
|
|
|
{
|
|
|
|
|
if (ReferenceEquals(_attached, resource)) _attached = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
IDisposable? attached;
|
|
|
|
|
lock (_gate)
|
|
|
|
|
{
|
|
|
|
|
_retired = true;
|
|
|
|
|
attached = _attached;
|
|
|
|
|
_attached = null;
|
|
|
|
|
}
|
|
|
|
|
attached?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 21:03:55 +01:00
|
|
|
private sealed class RemoteDocumentBinding<TDocument> : IDisposable where TDocument : class
|
|
|
|
|
{
|
|
|
|
|
private readonly CultMeshSession _session;
|
|
|
|
|
private readonly string _recordKey;
|
|
|
|
|
private readonly string _subscriptionId;
|
|
|
|
|
private readonly CultCache _cache;
|
|
|
|
|
private readonly CultNetDatabaseSubscriptionClient _subscription;
|
2026-07-13 04:43:27 +01:00
|
|
|
private readonly TimeSpan _responseTimeout;
|
|
|
|
|
private readonly CancellationTokenSource _lifetime = new();
|
2026-07-12 23:27:21 +01:00
|
|
|
private readonly TaskCompletionSource<bool> _ready = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
2026-07-13 04:19:54 +01:00
|
|
|
private readonly object _subscribeLock = new();
|
|
|
|
|
private Task? _subscribeTask;
|
2026-07-13 04:25:03 +01:00
|
|
|
private long _subscribedGeneration = -1;
|
2026-07-12 21:03:55 +01:00
|
|
|
private IDisposable? _stateWatch;
|
|
|
|
|
private bool _disposed;
|
|
|
|
|
|
2026-07-13 04:43:27 +01:00
|
|
|
public RemoteDocumentBinding(
|
|
|
|
|
CultMeshSession session,
|
|
|
|
|
string endpointId,
|
|
|
|
|
string recordKey,
|
|
|
|
|
TimeSpan responseTimeout)
|
2026-07-12 21:03:55 +01:00
|
|
|
{
|
|
|
|
|
_session = session;
|
2026-07-13 04:43:27 +01:00
|
|
|
_responseTimeout = responseTimeout;
|
2026-07-12 21:03:55 +01:00
|
|
|
_recordKey = recordKey;
|
|
|
|
|
_subscriptionId = "cultmesh-document:" + endpointId + ":" + recordKey;
|
|
|
|
|
var cacheRegistry = CultMesh.CreateCultCacheDocumentRegistry(typeof(TDocument));
|
|
|
|
|
var networkRegistry = CultMesh.CreateCultNetDocumentRegistry(new[] { typeof(TDocument) }, cacheRegistry);
|
|
|
|
|
_cache = new CultCache(cacheRegistry);
|
|
|
|
|
_subscription = new CultNetDatabaseSubscriptionClient(session.OpenSchemaClient(), _cache, networkRegistry);
|
|
|
|
|
Handle = CultMesh.Document<TDocument>(
|
|
|
|
|
_cache,
|
|
|
|
|
new CultRecordKey(recordKey),
|
|
|
|
|
CultMesh.Verse(endpointId, "cultmesh-client").Context,
|
|
|
|
|
routeHint: new CultMeshRouteHint(CultMeshLocalityKind.Network, endpointId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CultMeshDocumentHandle<TDocument> Handle { get; }
|
|
|
|
|
|
|
|
|
|
public async Task StartAsync()
|
|
|
|
|
{
|
|
|
|
|
_stateWatch = _session.WatchState()
|
|
|
|
|
.Where(state => state.Status == CultMeshSessionStatus.Online)
|
2026-07-13 04:19:54 +01:00
|
|
|
.Subscribe(state => { _ = EnsureSubscribedAsync(); });
|
|
|
|
|
_ = EnsureSubscribedAsync();
|
2026-07-12 23:27:21 +01:00
|
|
|
await _ready.Task.ConfigureAwait(false);
|
2026-07-12 21:03:55 +01:00
|
|
|
}
|
|
|
|
|
|
2026-07-13 04:19:54 +01:00
|
|
|
private Task EnsureSubscribedAsync()
|
|
|
|
|
{
|
|
|
|
|
lock (_subscribeLock)
|
|
|
|
|
{
|
2026-07-13 04:25:03 +01:00
|
|
|
var generation = _session.PhysicalGeneration;
|
|
|
|
|
if (_subscribedGeneration == generation && _subscribeTask != null) return _subscribeTask;
|
|
|
|
|
_subscribedGeneration = generation;
|
2026-07-13 04:19:54 +01:00
|
|
|
return _subscribeTask = SubscribeAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 21:03:55 +01:00
|
|
|
private async Task SubscribeAsync()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-07-13 04:43:27 +01:00
|
|
|
while (!_disposed)
|
|
|
|
|
{
|
|
|
|
|
using var deadline = CancellationTokenSource.CreateLinkedTokenSource(_lifetime.Token);
|
|
|
|
|
deadline.CancelAfter(_responseTimeout);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _subscription.SubscribeAsync(
|
|
|
|
|
_subscriptionId,
|
|
|
|
|
recordKeys: new[] { _recordKey },
|
|
|
|
|
schemaIds: new[] { CultDocumentRegistry.Shared.GetRequired<TDocument>().SchemaId },
|
|
|
|
|
cancellationToken: deadline.Token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
_ready.TrySetResult(true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException) when (!_lifetime.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException) when (_lifetime.IsCancellationRequested)
|
|
|
|
|
{
|
2026-07-12 21:03:55 +01:00
|
|
|
}
|
2026-07-12 23:27:21 +01:00
|
|
|
catch (ObjectDisposedException) when (_disposed)
|
2026-07-12 21:03:55 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (_disposed) return;
|
|
|
|
|
_disposed = true;
|
2026-07-13 04:43:27 +01:00
|
|
|
_lifetime.Cancel();
|
2026-07-12 21:07:21 +01:00
|
|
|
_stateWatch?.Dispose();
|
|
|
|
|
_subscription.Dispose();
|
2026-07-12 23:27:21 +01:00
|
|
|
_ready.TrySetException(new ObjectDisposedException(GetType().FullName));
|
2026-07-12 21:07:21 +01:00
|
|
|
_cache.Dispose();
|
2026-07-13 04:43:27 +01:00
|
|
|
_lifetime.Dispose();
|
2026-07-12 21:07:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private sealed class RemoteCollectionBinding<TDocument> : IDisposable where TDocument : class
|
|
|
|
|
{
|
|
|
|
|
private readonly CultMeshSession _session;
|
|
|
|
|
private readonly string _subscriptionId;
|
|
|
|
|
private readonly CultCache _cache;
|
|
|
|
|
private readonly CultNetDatabaseSubscriptionClient _subscription;
|
2026-07-13 04:43:27 +01:00
|
|
|
private readonly TimeSpan _responseTimeout;
|
|
|
|
|
private readonly CancellationTokenSource _lifetime = new();
|
2026-07-13 04:19:54 +01:00
|
|
|
private readonly bool _includeInitialSnapshot;
|
2026-07-12 23:27:21 +01:00
|
|
|
private readonly TaskCompletionSource<bool> _ready = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
2026-07-13 04:19:54 +01:00
|
|
|
private readonly object _subscribeLock = new();
|
|
|
|
|
private Task? _subscribeTask;
|
2026-07-13 04:25:03 +01:00
|
|
|
private long _subscribedGeneration = -1;
|
2026-07-12 21:07:21 +01:00
|
|
|
private IDisposable? _stateWatch;
|
|
|
|
|
private bool _disposed;
|
|
|
|
|
|
2026-07-13 04:43:27 +01:00
|
|
|
public RemoteCollectionBinding(
|
|
|
|
|
CultMeshSession session,
|
|
|
|
|
string endpointId,
|
|
|
|
|
bool includeInitialSnapshot,
|
|
|
|
|
TimeSpan responseTimeout)
|
2026-07-12 21:07:21 +01:00
|
|
|
{
|
|
|
|
|
_session = session;
|
2026-07-13 04:43:27 +01:00
|
|
|
_responseTimeout = responseTimeout;
|
2026-07-12 21:07:21 +01:00
|
|
|
_subscriptionId = "cultmesh-collection:" + endpointId + ":" + typeof(TDocument).FullName;
|
2026-07-13 04:19:54 +01:00
|
|
|
_includeInitialSnapshot = includeInitialSnapshot;
|
2026-07-12 21:07:21 +01:00
|
|
|
var cacheRegistry = CultMesh.CreateCultCacheDocumentRegistry(typeof(TDocument));
|
|
|
|
|
var networkRegistry = CultMesh.CreateCultNetDocumentRegistry(new[] { typeof(TDocument) }, cacheRegistry);
|
|
|
|
|
_cache = new CultCache(cacheRegistry);
|
|
|
|
|
_subscription = new CultNetDatabaseSubscriptionClient(session.OpenSchemaClient(), _cache, networkRegistry);
|
|
|
|
|
Handle = CultMesh.Collection<TDocument>(
|
|
|
|
|
_cache,
|
|
|
|
|
routeHint: new CultMeshRouteHint(CultMeshLocalityKind.Network, endpointId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CultMeshCollectionHandle<TDocument> Handle { get; }
|
|
|
|
|
|
|
|
|
|
public async Task StartAsync()
|
|
|
|
|
{
|
|
|
|
|
_stateWatch = _session.WatchState()
|
|
|
|
|
.Where(state => state.Status == CultMeshSessionStatus.Online)
|
2026-07-13 04:19:54 +01:00
|
|
|
.Subscribe(state => { _ = EnsureSubscribedAsync(); });
|
|
|
|
|
_ = EnsureSubscribedAsync();
|
2026-07-12 23:27:21 +01:00
|
|
|
await _ready.Task.ConfigureAwait(false);
|
2026-07-12 21:07:21 +01:00
|
|
|
}
|
|
|
|
|
|
2026-07-13 04:19:54 +01:00
|
|
|
private Task EnsureSubscribedAsync()
|
|
|
|
|
{
|
|
|
|
|
lock (_subscribeLock)
|
|
|
|
|
{
|
2026-07-13 04:25:03 +01:00
|
|
|
var generation = _session.PhysicalGeneration;
|
|
|
|
|
if (_subscribedGeneration == generation && _subscribeTask != null) return _subscribeTask;
|
|
|
|
|
_subscribedGeneration = generation;
|
2026-07-13 04:19:54 +01:00
|
|
|
return _subscribeTask = SubscribeAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 21:07:21 +01:00
|
|
|
private async Task SubscribeAsync()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-07-13 04:43:27 +01:00
|
|
|
while (!_disposed)
|
|
|
|
|
{
|
|
|
|
|
using var deadline = CancellationTokenSource.CreateLinkedTokenSource(_lifetime.Token);
|
|
|
|
|
deadline.CancelAfter(_responseTimeout);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _subscription.SubscribeAsync(
|
|
|
|
|
_subscriptionId,
|
|
|
|
|
schemaIds: new[] { CultDocumentRegistry.Shared.GetRequired<TDocument>().SchemaId },
|
|
|
|
|
includeSnapshot: _includeInitialSnapshot,
|
|
|
|
|
cancellationToken: deadline.Token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
_ready.TrySetResult(true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException) when (!_lifetime.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException) when (_lifetime.IsCancellationRequested)
|
|
|
|
|
{
|
2026-07-12 21:07:21 +01:00
|
|
|
}
|
2026-07-12 23:27:21 +01:00
|
|
|
catch (ObjectDisposedException) when (_disposed)
|
2026-07-12 21:07:21 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (_disposed) return;
|
|
|
|
|
_disposed = true;
|
2026-07-13 04:43:27 +01:00
|
|
|
_lifetime.Cancel();
|
2026-07-12 21:03:55 +01:00
|
|
|
_stateWatch?.Dispose();
|
|
|
|
|
_subscription.Dispose();
|
2026-07-12 23:27:21 +01:00
|
|
|
_ready.TrySetException(new ObjectDisposedException(GetType().FullName));
|
2026-07-12 21:03:55 +01:00
|
|
|
_cache.Dispose();
|
2026-07-13 04:43:27 +01:00
|
|
|
_lifetime.Dispose();
|
2026-07-12 21:03:55 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 20:56:45 +01:00
|
|
|
private sealed class RendezvousLookupSource : ICultMeshLookupSource
|
|
|
|
|
{
|
|
|
|
|
private readonly string _endpoint;
|
|
|
|
|
private readonly CultMeshVerseDiscoveryClient _client;
|
|
|
|
|
private readonly CultMeshVerseDiscoveryClientOptions _options;
|
|
|
|
|
|
|
|
|
|
public RendezvousLookupSource(
|
|
|
|
|
string endpoint,
|
|
|
|
|
CultMeshVerseDiscoveryClient client,
|
|
|
|
|
CultMeshVerseDiscoveryClientOptions options)
|
|
|
|
|
{
|
|
|
|
|
_endpoint = endpoint;
|
|
|
|
|
_client = client;
|
|
|
|
|
_options = options;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string SourceId => "rendezvous:" + _endpoint;
|
|
|
|
|
|
|
|
|
|
public async Task<IReadOnlyList<CultMeshDiscoveryObservation>> LookupAsync(
|
|
|
|
|
CultMeshDiscoveryQuery query,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
var response = await _client.FetchAsync(
|
|
|
|
|
_endpoint,
|
|
|
|
|
new CultMeshVerseCatalogRequestMessage
|
|
|
|
|
{
|
|
|
|
|
VerseIds = query.VerseIds.Count == 0 ? null : query.VerseIds.ToArray(),
|
|
|
|
|
TransportVersion = "cultmesh.v0"
|
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
var observedAt = _options.Clock.UtcNow;
|
|
|
|
|
return response.Verses.Select(message => new CultMeshDiscoveryObservation(
|
|
|
|
|
message.ToVerseDescriptor(),
|
|
|
|
|
SourceId,
|
|
|
|
|
observedAt,
|
|
|
|
|
observedAt + _options.ObservationTtl,
|
|
|
|
|
CultMeshDiscoveryTrust.Unsigned,
|
|
|
|
|
response.SchemaVersion)).ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|