NATS
This commit is contained in:
@@ -118,15 +118,18 @@ public sealed class MessageDispatcher : IMessageDispatcher
|
|||||||
"Dispatching '{Type}' remotely on {Subject} seq={Seq} for user {UserId}",
|
"Dispatching '{Type}' remotely on {Subject} seq={Seq} for user {UserId}",
|
||||||
message.Type, subject, message.Seq, connection.UserId);
|
message.Type, subject, message.Seq, connection.UserId);
|
||||||
|
|
||||||
var reply = await _nats.RequestAsync<RemoteDispatchRequest, RemoteDispatchResponse>(
|
var requestBytes = JsonSerializer.SerializeToUtf8Bytes(
|
||||||
subject,
|
|
||||||
new RemoteDispatchRequest(
|
new RemoteDispatchRequest(
|
||||||
message.Type,
|
message.Type,
|
||||||
message.Seq,
|
message.Seq,
|
||||||
message.Payload.ToArray(),
|
message.Payload.ToArray(),
|
||||||
connection.UserId,
|
connection.UserId,
|
||||||
connection.DisplayName,
|
connection.DisplayName,
|
||||||
connection.SessionToken),
|
connection.SessionToken));
|
||||||
|
|
||||||
|
var reply = await _nats.RequestAsync<byte[], byte[]>(
|
||||||
|
subject,
|
||||||
|
requestBytes,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
@@ -138,8 +141,8 @@ public sealed class MessageDispatcher : IMessageDispatcher
|
|||||||
},
|
},
|
||||||
timeoutCts.Token);
|
timeoutCts.Token);
|
||||||
|
|
||||||
var remote = reply.Data;
|
var replyBytes = reply.Data;
|
||||||
if (remote is null)
|
if (replyBytes is null || replyBytes.Length == 0)
|
||||||
{
|
{
|
||||||
_logger.LogWarning(
|
_logger.LogWarning(
|
||||||
"Remote dispatch for '{Type}' returned an empty reply",
|
"Remote dispatch for '{Type}' returned an empty reply",
|
||||||
@@ -147,6 +150,15 @@ public sealed class MessageDispatcher : IMessageDispatcher
|
|||||||
return ServerMessage.Error(message.Seq, "INTERNAL_ERROR", "Internal service returned an empty reply.");
|
return ServerMessage.Error(message.Seq, "INTERNAL_ERROR", "Internal service returned an empty reply.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var remote = JsonSerializer.Deserialize<RemoteDispatchResponse>(replyBytes);
|
||||||
|
if (remote is null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(
|
||||||
|
"Remote dispatch for '{Type}' returned an unreadable reply",
|
||||||
|
message.Type);
|
||||||
|
return ServerMessage.Error(message.Seq, "INTERNAL_ERROR", "Internal service returned an invalid reply.");
|
||||||
|
}
|
||||||
|
|
||||||
if (!remote.Handled)
|
if (!remote.Handled)
|
||||||
{
|
{
|
||||||
return ServerMessage.Error(
|
return ServerMessage.Error(
|
||||||
|
|||||||
@@ -107,10 +107,10 @@ public sealed class NatsMessageRpcResponder : IHostedService
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
await foreach (var msg in _nats
|
await foreach (var msg in _nats
|
||||||
.SubscribeAsync<RemoteDispatchRequest>(subject, queueGroup, cancellationToken: cancellationToken)
|
.SubscribeAsync<byte[]>(subject, queueGroup, cancellationToken: cancellationToken)
|
||||||
.WithCancellation(cancellationToken))
|
.WithCancellation(cancellationToken))
|
||||||
{
|
{
|
||||||
if (msg.Data is null)
|
if (msg.Data is null || msg.Data.Length == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(msg.ReplyTo))
|
if (string.IsNullOrWhiteSpace(msg.ReplyTo))
|
||||||
@@ -121,8 +121,18 @@ public sealed class NatsMessageRpcResponder : IHostedService
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var response = await DispatchAsync(handlerType, msg.Data, cancellationToken);
|
var request = JsonSerializer.Deserialize<RemoteDispatchRequest>(msg.Data);
|
||||||
await _nats.PublishAsync(msg.ReplyTo, response, cancellationToken: cancellationToken);
|
if (request is null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(
|
||||||
|
"Received unreadable RPC payload for '{Type}'",
|
||||||
|
messageType);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var response = await DispatchAsync(handlerType, request, cancellationToken);
|
||||||
|
var responseBytes = JsonSerializer.SerializeToUtf8Bytes(response);
|
||||||
|
await _nats.PublishAsync(msg.ReplyTo, responseBytes, cancellationToken: cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
|
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
|
||||||
|
|||||||
Reference in New Issue
Block a user