use Task insteadof while improve performance

This commit is contained in:
2024-07-26 05:20:27 +08:00
parent f4bc6dc080
commit 82f7e0fc00
+24 -18
View File
@@ -12,7 +12,7 @@ namespace SimApi;
public partial class Synapse public partial class Synapse
{ {
private Dictionary<string, string> ResponseCache { get; } = new(); private Dictionary<string, TaskCompletionSource<string>> ResponseCompletionSources { get; } = new();
private void RunRpcClient() private void RunRpcClient()
{ {
@@ -24,8 +24,9 @@ public partial class Synapse
if (!e.ApplicationMessage.Topic.StartsWith(rcTopic)) return Task.CompletedTask; if (!e.ApplicationMessage.Topic.StartsWith(rcTopic)) return Task.CompletedTask;
var reqBody = e.ApplicationMessage.ConvertPayloadToString(); var reqBody = e.ApplicationMessage.ConvertPayloadToString();
var messageId = e.ApplicationMessage.Topic.Replace(rcTopic, string.Empty); var messageId = e.ApplicationMessage.Topic.Replace(rcTopic, string.Empty);
ResponseCache.Add(messageId, reqBody); if (!ResponseCompletionSources.TryGetValue(messageId, out var tcs)) return Task.CompletedTask;
logger.LogDebug("Synapse RPC Client Message: ({BasicPropertiesCorrelationId}) => {S}", messageId, reqBody); tcs.SetResult(reqBody);
ResponseCompletionSources.Remove(messageId);
return Task.CompletedTask; return Task.CompletedTask;
}; };
Client.SubscribeAsync(rcSubOpts).Wait(); Client.SubscribeAsync(rcSubOpts).Wait();
@@ -34,9 +35,10 @@ public partial class Synapse
private string FireRpc(string app, string action, object param) private string FireRpc(string app, string action, object param)
{ {
var paramJson = JsonSerializer.Serialize(param, SimApiUtil.JsonOption); var paramJson = JsonSerializer.Serialize(param, SimApiUtil.JsonOption);
string response;
var topic = $"{Options.SysName}/{app}/rpc/server/{action}"; var topic = $"{Options.SysName}/{app}/rpc/server/{action}";
var messageId = Guid.NewGuid().ToString(); var messageId = Guid.NewGuid().ToString();
var tcs = new TaskCompletionSource<string>();
ResponseCompletionSources.Add(messageId, tcs);
var message = new MqttApplicationMessageBuilder() var message = new MqttApplicationMessageBuilder()
.WithTopic(topic) .WithTopic(topic)
.WithPayload(paramJson) .WithPayload(paramJson)
@@ -47,24 +49,28 @@ public partial class Synapse
if (!Client!.IsConnected) return null; if (!Client!.IsConnected) return null;
Client!.PublishAsync(message, CancellationToken.None).Wait(); Client!.PublishAsync(message, CancellationToken.None).Wait();
logger.LogDebug( logger.LogDebug(
"Synapse RPC Client Request: ({PropsMessageId}) {OptionsAppName} -> {Action}@{App}\n{ParamJson}", "Synapse RPC Client Request: ({PropsMessageId}) {OptionsAppName} -> {Action}@{App}\n{ParamJson}", messageId,
messageId, Options.AppName, action, app, paramJson); Options.AppName, action, app, paramJson);
var ts = SimApiUtil.TimestampNow;
while (true)
{
if (SimApiUtil.TimestampNow - ts > Options.RpcTimeout)
{
response = JsonSerializer.Serialize(new SimApiBaseResponse(502, "timeout"), SimApiUtil.JsonOption);
break;
}
if (!ResponseCache.TryGetValue(messageId, out var value)) continue; string response;
response = value; try
ResponseCache.Remove(messageId); {
if (tcs.Task.Wait(Options.RpcTimeout * 1000))
{
response = tcs.Task.Result;
logger.LogDebug( logger.LogDebug(
"Synapse RPC Client Response: ({BasicPropertiesCorrelationId}) {BasicPropertiesType}@{BasicPropertiesReplyTo} -> {OptionsAppName}\n{S}", "Synapse RPC Client Response: ({BasicPropertiesCorrelationId}) {BasicPropertiesType}@{BasicPropertiesReplyTo} -> {OptionsAppName}\n{S}",
messageId, action, app, Options.AppName, response); messageId, action, app, Options.AppName, response);
break; }
else
{
response = JsonSerializer.Serialize(new SimApiBaseResponse(502, "timeout"), SimApiUtil.JsonOption);
}
}
catch
{
response = JsonSerializer.Serialize(new SimApiBaseResponse(500, "Synapse RPC Client Error"),
SimApiUtil.JsonOption);
} }
return response; return response;