Make CultMesh identity and route recovery explicit
This commit is contained in:
parent
b542f4b644
commit
21163d83fa
24 changed files with 179 additions and 94 deletions
|
|
@ -383,18 +383,7 @@ namespace GameCult.Mesh
|
|||
var expectedOperation = operation.Trim();
|
||||
var expectedResponseSchema = responseSchema.Trim();
|
||||
var messageId = idempotencyKey.Trim();
|
||||
var session = await ConnectAsync(target, CultMeshProtocols.Documents, cancellationToken).ConfigureAwait(false);
|
||||
var completion = new TaskCompletionSource<CultNetOperationResponseMessage>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
using var subscription = session.OnCultNet<CultNetOperationResponseMessage>(response =>
|
||||
{
|
||||
if (string.Equals(response.MessageId, messageId, StringComparison.Ordinal))
|
||||
completion.TrySetResult(response);
|
||||
});
|
||||
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
timeout.CancelAfter(_operationResponseTimeout);
|
||||
using var cancellation = timeout.Token.Register(() => completion.TrySetCanceled(timeout.Token));
|
||||
|
||||
session.SendCultNet(new CultNetOperationRequestMessage
|
||||
var message = new CultNetOperationRequestMessage
|
||||
{
|
||||
MessageId = messageId,
|
||||
ServiceId = expectedService,
|
||||
|
|
@ -403,14 +392,46 @@ namespace GameCult.Mesh
|
|||
PayloadEncoding = "messagepack-base64",
|
||||
Payload = Convert.ToBase64String(MessagePackSerializer.Serialize(request, CultNetSchemaMessageSerialization.Options)),
|
||||
SourceRuntimeId = sourceRuntimeId.Trim()
|
||||
});
|
||||
|
||||
};
|
||||
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
timeout.CancelAfter(_operationResponseTimeout);
|
||||
CultNetOperationResponseMessage envelope;
|
||||
try
|
||||
{
|
||||
envelope = await completion.Task.ConfigureAwait(false);
|
||||
while (true)
|
||||
{
|
||||
var session = await ConnectOnlineAsync(target, timeout.Token).ConfigureAwait(false);
|
||||
var completion = new TaskCompletionSource<CultNetOperationResponseMessage>(
|
||||
TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var pathChanged = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var generation = session.PhysicalGeneration;
|
||||
using var responseSubscription = session.OnCultNet<CultNetOperationResponseMessage>(response =>
|
||||
{
|
||||
if (string.Equals(response.MessageId, messageId, StringComparison.Ordinal))
|
||||
completion.TrySetResult(response);
|
||||
});
|
||||
using var stateSubscription = session.WatchState().Subscribe(state =>
|
||||
{
|
||||
if (state.Status != CultMeshSessionStatus.Online || session.PhysicalGeneration != generation)
|
||||
pathChanged.TrySetResult(true);
|
||||
});
|
||||
using var cancellation = timeout.Token.Register(() => completion.TrySetCanceled(timeout.Token));
|
||||
if (session.State.Status != CultMeshSessionStatus.Online ||
|
||||
session.PhysicalGeneration != generation)
|
||||
continue;
|
||||
|
||||
session.SendCultNet(message);
|
||||
var finished = await Task.WhenAny(completion.Task, pathChanged.Task).ConfigureAwait(false);
|
||||
if (finished == completion.Task)
|
||||
{
|
||||
envelope = await completion.Task.ConfigureAwait(false);
|
||||
break;
|
||||
}
|
||||
// The operation has a durable idempotency key. A route loss before a
|
||||
// correlated response replays the same request after replacement finality.
|
||||
}
|
||||
}
|
||||
catch (TaskCanceledException) when (!cancellationToken.IsCancellationRequested)
|
||||
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
throw new TimeoutException(
|
||||
$"CultMesh operation '{expectedService}/{expectedOperation}' did not answer idempotency key '{messageId}' " +
|
||||
|
|
@ -462,6 +483,32 @@ namespace GameCult.Mesh
|
|||
return new CultMeshOperationResult<TResponse>(envelope, value);
|
||||
}
|
||||
|
||||
private async Task<CultMeshSession> ConnectOnlineAsync(
|
||||
CultMeshSessionTarget target,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var session = await ConnectAsync(target, CultMeshProtocols.Documents, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (session.State.Status == CultMeshSessionStatus.Online)
|
||||
return session;
|
||||
|
||||
var changed = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
using var subscription = session.WatchState().Subscribe(state =>
|
||||
{
|
||||
if (state.Status == CultMeshSessionStatus.Online || state.Status == CultMeshSessionStatus.Offline)
|
||||
changed.TrySetResult(true);
|
||||
});
|
||||
using var cancellation = cancellationToken.Register(() => changed.TrySetCanceled(cancellationToken));
|
||||
if (session.State.Status != CultMeshSessionStatus.Reconnecting)
|
||||
continue;
|
||||
await changed.Task.ConfigureAwait(false);
|
||||
if (session.State.Status == CultMeshSessionStatus.Online)
|
||||
return session;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Gets the number of currently leased shared document subscriptions.</summary>
|
||||
public int ActiveDocumentResourceCount => _documents.Count;
|
||||
|
||||
|
|
|
|||
|
|
@ -34,50 +34,42 @@ import provider internals.
|
|||
Install the renderer-neutral `GameCult.Eve.Surface` package in the provider.
|
||||
Do not install EveUnity or the browser lowerer there. A provider defines typed
|
||||
state, one typed operation transaction, and one Eve surface. CultNet envelope
|
||||
encoding is framework work:
|
||||
encoding is framework work. This complete method builds the surface document;
|
||||
it has no hidden transport or state variables:
|
||||
|
||||
```csharp
|
||||
using GameCult.Eve.Surface;
|
||||
using GameCult.Mesh;
|
||||
using GameCult.Networking;
|
||||
|
||||
using var operations = new CultNetOperationServer(schemaServer, "sample.counter-provider")
|
||||
.Register<IncrementRequest, IncrementReceipt>(
|
||||
"sample.counter",
|
||||
static EveSurfaceDocument CreateCounterSurface()
|
||||
{
|
||||
var route = new CultMeshRouteHint(CultMeshLocalityKind.Network, "cultmesh");
|
||||
var count = new CultMeshStateBindingDescriptor(
|
||||
"value",
|
||||
"sample.counter.count",
|
||||
"sample.counter_state:counter:main",
|
||||
"sample.counter_state.v1",
|
||||
route);
|
||||
var increment = CultMesh.OperationBinding(
|
||||
"sample.counter.increment",
|
||||
"Increment",
|
||||
"sample.increment.v1",
|
||||
"gamecult.eve.command_receipt.v1",
|
||||
async command => await IncrementExactlyOnceAsync(
|
||||
command.IdempotencyKey,
|
||||
command.Value));
|
||||
route);
|
||||
|
||||
var route = new CultMeshRouteHint(CultMeshLocalityKind.Network, "cultmesh");
|
||||
var count = new CultMeshStateBindingDescriptor(
|
||||
"value",
|
||||
"sample.counter.count",
|
||||
"sample.counter_state:counter:main",
|
||||
"sample.counter_state.v1",
|
||||
route);
|
||||
var increment = CultMesh.OperationBinding(
|
||||
"sample.counter.increment",
|
||||
"Increment",
|
||||
"sample.increment.v1",
|
||||
route);
|
||||
|
||||
var surface = EveSurface.Create("sample.counter")
|
||||
.Provider("sample.counter-provider", "sample.daemon")
|
||||
.Title("CultMesh browser counter")
|
||||
.RootColumn("counter.root", root => root
|
||||
.Metric("counter.value", "Canonical count", "0", count)
|
||||
.Button("counter.increment", "Increment", increment))
|
||||
.Build();
|
||||
return EveSurface.Create("sample.counter")
|
||||
.Provider("sample.counter-ui", "sample.daemon")
|
||||
.Title("CultMesh browser counter")
|
||||
.RootColumn("counter.root", root => root
|
||||
.Metric("counter.value", "Canonical count", "0", count)
|
||||
.Button("counter.increment", "Increment", increment))
|
||||
.Build();
|
||||
}
|
||||
```
|
||||
|
||||
`schemaServer` is a transport-neutral CultNet server port. WebSocket, RUDP, and
|
||||
test transports attach the same dispatcher. The handler owns validation,
|
||||
idempotency, state mutation, and its durable receipt. `CultNetOperationServer`
|
||||
owns only route/schema checks and typed envelope serialization. The complete
|
||||
provider transaction is in
|
||||
The operation handler owns validation, idempotency, state mutation, and its
|
||||
durable receipt. `CultNetOperationServer` owns only route/schema checks and
|
||||
typed envelope serialization. The complete, compiling provider transaction—including
|
||||
the schema server, typed request/receipt types, and exactly-once mutation—is in
|
||||
[`samples/eve-browser-network/Program.cs`](../../../../samples/eve-browser-network/Program.cs);
|
||||
it contains no hand-written operation base64 or MessagePack dispatch switch.
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue