use lock
This commit is contained in:
+52
-48
@@ -43,60 +43,64 @@ public partial class Synapse
|
|||||||
private string? FireRpc(string app, string action, object? param, Dictionary<string, string>? headers = null,
|
private string? FireRpc(string app, string action, object? param, Dictionary<string, string>? headers = null,
|
||||||
int? timeout = null)
|
int? timeout = null)
|
||||||
{
|
{
|
||||||
string paramJson;
|
// 检查并发情况下的线程安全问题,使用锁保证字典操作的原子性
|
||||||
if (param is string strParam)
|
lock (ResponseCompletionSources)
|
||||||
{
|
{
|
||||||
paramJson = strParam;
|
string paramJson;
|
||||||
}
|
if (param is string strParam)
|
||||||
else
|
|
||||||
{
|
|
||||||
paramJson = JsonSerializer.Serialize(param, SimApiUtil.JsonOption);
|
|
||||||
}
|
|
||||||
|
|
||||||
var topic = $"{Options.SysName}/{app}/rpc/server/{action}";
|
|
||||||
var messageId = Guid.NewGuid().ToString();
|
|
||||||
var tcs = new TaskCompletionSource<string>();
|
|
||||||
ResponseCompletionSources.Add(messageId, tcs);
|
|
||||||
var messageBuilder = new MqttApplicationMessageBuilder()
|
|
||||||
.WithTopic(topic)
|
|
||||||
.WithPayload(paramJson)
|
|
||||||
.WithResponseTopic($"{Options.AppName},{Options.AppId},{messageId}")
|
|
||||||
.WithContentType("application/json")
|
|
||||||
.WithRetainFlag(false);
|
|
||||||
foreach (var h in headers ?? new Dictionary<string, string>())
|
|
||||||
{
|
|
||||||
messageBuilder.WithUserProperty(h.Key, h.Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
var message = messageBuilder.Build();
|
|
||||||
if (!Client!.IsConnected) return null;
|
|
||||||
Client.PublishAsync(message, CancellationToken.None).Wait();
|
|
||||||
logger.LogDebug(
|
|
||||||
"Synapse RPC Client Request: ({PropsMessageId}) {OptionsAppName} -> {Action}@{App}\n{ParamJson}\nHeaders: {Headers}",
|
|
||||||
messageId, Options.AppName, action, app, paramJson, JsonSerializer.Serialize(headers));
|
|
||||||
|
|
||||||
string response;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
timeout ??= Options.RpcTimeout;
|
|
||||||
if (tcs.Task.Wait(timeout.Value * 1000))
|
|
||||||
{
|
{
|
||||||
response = tcs.Task.Result;
|
paramJson = strParam;
|
||||||
logger.LogDebug(
|
|
||||||
"Synapse RPC Client Response: ({BasicPropertiesCorrelationId}) {BasicPropertiesType}@{BasicPropertiesReplyTo} -> {OptionsAppName}\n{S}",
|
|
||||||
messageId, action, app, Options.AppName, response);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
response = JsonSerializer.Serialize(new SimApiBaseResponse(502, "timeout"), SimApiUtil.JsonOption);
|
paramJson = JsonSerializer.Serialize(param, SimApiUtil.JsonOption);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
response = JsonSerializer.Serialize(new SimApiBaseResponse(500, "Synapse RPC Client Error"),
|
|
||||||
SimApiUtil.JsonOption);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response;
|
var topic = $"{Options.SysName}/{app}/rpc/server/{action}";
|
||||||
|
var messageId = Guid.NewGuid().ToString();
|
||||||
|
var tcs = new TaskCompletionSource<string>();
|
||||||
|
ResponseCompletionSources.Add(messageId, tcs);
|
||||||
|
var messageBuilder = new MqttApplicationMessageBuilder()
|
||||||
|
.WithTopic(topic)
|
||||||
|
.WithPayload(paramJson)
|
||||||
|
.WithResponseTopic($"{Options.AppName},{Options.AppId},{messageId}")
|
||||||
|
.WithContentType("application/json")
|
||||||
|
.WithRetainFlag(false);
|
||||||
|
foreach (var h in headers ?? new Dictionary<string, string>())
|
||||||
|
{
|
||||||
|
messageBuilder.WithUserProperty(h.Key, h.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
var message = messageBuilder.Build();
|
||||||
|
if (!Client!.IsConnected) return null;
|
||||||
|
Client.PublishAsync(message, CancellationToken.None).Wait();
|
||||||
|
logger.LogDebug(
|
||||||
|
"Synapse RPC Client Request: ({PropsMessageId}) {OptionsAppName} -> {Action}@{App}\n{ParamJson}\nHeaders: {Headers}",
|
||||||
|
messageId, Options.AppName, action, app, paramJson, JsonSerializer.Serialize(headers));
|
||||||
|
|
||||||
|
string response;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
timeout ??= Options.RpcTimeout;
|
||||||
|
if (tcs.Task.Wait(timeout.Value * 1000))
|
||||||
|
{
|
||||||
|
response = tcs.Task.Result;
|
||||||
|
logger.LogDebug(
|
||||||
|
"Synapse RPC Client Response: ({BasicPropertiesCorrelationId}) {BasicPropertiesType}@{BasicPropertiesReplyTo} -> {OptionsAppName}\n{S}",
|
||||||
|
messageId, action, app, Options.AppName, response);
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user