Materialize exact reactive state on demand
This commit is contained in:
parent
a1f25af2e0
commit
0ef59b50c2
18 changed files with 164 additions and 7 deletions
|
|
@ -317,6 +317,7 @@ namespace GameCult.Mesh
|
|||
public void Observe(CultNetDatabaseSubscriptionDemand demand)
|
||||
{
|
||||
if (demand == null) throw new ArgumentNullException(nameof(demand));
|
||||
if (demand.BodyIds.Count == 0) return;
|
||||
var key = demand.ConsumerRuntimeId + "\u001f" + demand.SubscriptionId;
|
||||
lock (_gate)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -528,6 +528,11 @@ shared-file mapping, or network from observed locality and available adapters.
|
|||
Body bytes never become subscription snapshot payload. Either subscription may
|
||||
legitimately start with `HasValue == false`; it remains active until the provider
|
||||
publishes the exact record, avoiding a startup retry loop or broad snapshot.
|
||||
`CultNetDatabaseSubscriptionServer.DemandChanged` exposes the exact requested
|
||||
record and schema set as well as optional body negotiation. A provider can
|
||||
therefore materialize a computed record only while it has consumers. The watch
|
||||
is installed before demand is announced, so the first publication travels as a
|
||||
normal live change and does not require a bootstrap snapshot.
|
||||
|
||||
### Client Prediction
|
||||
|
||||
|
|
|
|||
|
|
@ -920,6 +920,18 @@ stream demand merely by existing.
|
|||
and server disposal all update the same typed demand projection. Stable peer
|
||||
identity, not per-message wrapper identity, keys subscription lifetime.
|
||||
|
||||
Exact reactive document demand carries the requested record and schema set even
|
||||
when no hot body is involved. Providers may use that demand to materialize
|
||||
computed documents lazily; the subscription watch exists before activation is
|
||||
reported, so frame zero is delivered on the live path. Dynamic state is not
|
||||
forced back through a broad snapshot merely because the record did not exist at
|
||||
subscribe time.
|
||||
|
||||
The client installs the declared delivery mode before sending the subscription.
|
||||
A provider may publish synchronously from demand activation, so a live change
|
||||
can correctly precede the empty bootstrap acknowledgement without briefly
|
||||
becoming durable replica state.
|
||||
|
||||
`CultMesh.SubscribeLiveBodyAsync(...)` is the narrow public construction path.
|
||||
Its `CultMeshLiveBodySubscription` names only the logical body, consumer, and
|
||||
subscription lifetime. `CultMeshLiveBody` owns the ephemeral latest-publication
|
||||
|
|
|
|||
|
|
@ -116,6 +116,7 @@ namespace GameCult.Networking
|
|||
var completion = new TaskCompletionSource<IReadOnlyList<object>>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
if (!_initialSnapshots.TryAdd(messageId, new PendingSubscription(subscriptionId, deliveryMode, completion)))
|
||||
throw new InvalidOperationException($"Duplicate database subscription message id '{messageId}'.");
|
||||
_deliveryModes[subscriptionId] = deliveryMode;
|
||||
|
||||
CancellationTokenRegistration cancellation = default;
|
||||
if (cancellationToken.CanBeCanceled)
|
||||
|
|
@ -151,6 +152,7 @@ namespace GameCult.Networking
|
|||
}
|
||||
catch
|
||||
{
|
||||
_deliveryModes.TryRemove(subscriptionId, out _);
|
||||
if (_initialSnapshots.TryRemove(messageId, out var pending))
|
||||
pending.Completion.TrySetException(new InvalidOperationException("CultNet subscription request could not be sent."));
|
||||
throw;
|
||||
|
|
@ -198,7 +200,6 @@ namespace GameCult.Networking
|
|||
{
|
||||
applied = await _documents.ApplyRawSnapshotResponseAsync(_cache, message).ConfigureAwait(false);
|
||||
}
|
||||
_deliveryModes[pending.SubscriptionId] = pending.DeliveryMode;
|
||||
pending.Completion.TrySetResult(applied);
|
||||
}
|
||||
catch (Exception error)
|
||||
|
|
|
|||
|
|
@ -41,7 +41,11 @@ namespace GameCult.Networking
|
|||
_peerLifecycle.PeerDisconnected += HandlePeerDisconnected;
|
||||
}
|
||||
|
||||
/// <summary>Raised when exact document subscriptions add or remove typed hot-body demand.</summary>
|
||||
/// <summary>
|
||||
/// Raised when an exact subscription is activated or withdrawn. Providers can use
|
||||
/// the requested records and schemas to materialize reactive state only while a
|
||||
/// consumer needs it; body publishers additionally use the negotiated body route.
|
||||
/// </summary>
|
||||
public event Action<CultNetDatabaseSubscriptionDemand>? DemandChanged;
|
||||
|
||||
/// <summary>Detaches handlers and releases all active watches.</summary>
|
||||
|
|
@ -142,11 +146,21 @@ namespace GameCult.Networking
|
|||
SubscriptionKey key,
|
||||
bool active)
|
||||
{
|
||||
if (request.BodyIds is not { Length: > 0 }) return;
|
||||
DemandChanged?.Invoke(new CultNetDatabaseSubscriptionDemand(
|
||||
string.IsNullOrWhiteSpace(request.ConsumerRuntimeId) ? key.Id : request.ConsumerRuntimeId!,
|
||||
key.Id,
|
||||
request.BodyIds.Where(value => !string.IsNullOrWhiteSpace(value)).Distinct(StringComparer.Ordinal).ToArray(),
|
||||
(request.RecordKeys ?? Array.Empty<string>())
|
||||
.Where(value => !string.IsNullOrWhiteSpace(value))
|
||||
.Distinct(StringComparer.Ordinal)
|
||||
.ToArray(),
|
||||
(request.SchemaIds ?? Array.Empty<string>())
|
||||
.Where(value => !string.IsNullOrWhiteSpace(value))
|
||||
.Distinct(StringComparer.Ordinal)
|
||||
.ToArray(),
|
||||
(request.BodyIds ?? Array.Empty<string>())
|
||||
.Where(value => !string.IsNullOrWhiteSpace(value))
|
||||
.Distinct(StringComparer.Ordinal)
|
||||
.ToArray(),
|
||||
(request.SupportedBodyTransports ?? Array.Empty<string>())
|
||||
.Where(value => !string.IsNullOrWhiteSpace(value))
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
|
|
@ -284,7 +298,7 @@ namespace GameCult.Networking
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>One active or withdrawn consumer demand for logical hot bodies.</summary>
|
||||
/// <summary>One active or withdrawn exact state subscription and its optional hot-body route.</summary>
|
||||
public sealed class CultNetDatabaseSubscriptionDemand
|
||||
{
|
||||
public CultNetDatabaseSubscriptionDemand(
|
||||
|
|
@ -294,9 +308,32 @@ namespace GameCult.Networking
|
|||
IReadOnlyList<string> supportedBodyTransports,
|
||||
bool sameMachine,
|
||||
bool active)
|
||||
: this(
|
||||
consumerRuntimeId,
|
||||
subscriptionId,
|
||||
Array.Empty<string>(),
|
||||
Array.Empty<string>(),
|
||||
bodyIds,
|
||||
supportedBodyTransports,
|
||||
sameMachine,
|
||||
active)
|
||||
{
|
||||
}
|
||||
|
||||
public CultNetDatabaseSubscriptionDemand(
|
||||
string consumerRuntimeId,
|
||||
string subscriptionId,
|
||||
IReadOnlyList<string> recordKeys,
|
||||
IReadOnlyList<string> schemaIds,
|
||||
IReadOnlyList<string> bodyIds,
|
||||
IReadOnlyList<string> supportedBodyTransports,
|
||||
bool sameMachine,
|
||||
bool active)
|
||||
{
|
||||
ConsumerRuntimeId = consumerRuntimeId;
|
||||
SubscriptionId = subscriptionId;
|
||||
RecordKeys = recordKeys;
|
||||
SchemaIds = schemaIds;
|
||||
BodyIds = bodyIds;
|
||||
SupportedBodyTransports = supportedBodyTransports;
|
||||
SameMachine = sameMachine;
|
||||
|
|
@ -305,6 +342,8 @@ namespace GameCult.Networking
|
|||
|
||||
public string ConsumerRuntimeId { get; }
|
||||
public string SubscriptionId { get; }
|
||||
public IReadOnlyList<string> RecordKeys { get; }
|
||||
public IReadOnlyList<string> SchemaIds { get; }
|
||||
public IReadOnlyList<string> BodyIds { get; }
|
||||
public IReadOnlyList<string> SupportedBodyTransports { get; }
|
||||
public bool SameMachine { get; }
|
||||
|
|
|
|||
Loading…
Reference in a new issue