Compare commits

...
4 Commits
Author SHA1 Message Date
xrain 116cbcdc7f update to 9 2025-10-14 02:55:29 +08:00
xrain 4e1b7d4845 update to 9 2025-10-14 02:20:37 +08:00
xrain 71acb35df9 cancle json refrence 2025-08-13 17:28:24 +08:00
xrain 67cc7346dc use concurrentDic 2025-05-17 21:17:43 +08:00
5 changed files with 56 additions and 62 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ jobs:
- name: Setup .NET Core - name: Setup .NET Core
uses: actions/setup-dotnet@v1 uses: actions/setup-dotnet@v1
with: with:
dotnet-version: "8.0.200" dotnet-version: "9.0.305"
- name: Publish - name: Publish
run: | run: |
version=`git describe --tags` version=`git describe --tags`
+2 -2
View File
@@ -24,10 +24,10 @@ public static class SimApiUtil
/// </summary> /// </summary>
public static JsonSerializerOptions JsonOption => new() public static JsonSerializerOptions JsonOption => new()
{ {
ReferenceHandler = ReferenceHandler.Preserve, // ReferenceHandler = ReferenceHandler.Preserve,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All), Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull // DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
}; };
/// <summary> /// <summary>
+1 -1
View File
@@ -14,8 +14,8 @@
<SynchReleaseVersion>false</SynchReleaseVersion> <SynchReleaseVersion>false</SynchReleaseVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>5.0.2</PackageVersion> <PackageVersion>5.0.2</PackageVersion>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
-2
View File
@@ -3,7 +3,6 @@ using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization;
using Hangfire; using Hangfire;
using Hangfire.Console; using Hangfire.Console;
using Hangfire.Redis.StackExchange; using Hangfire.Redis.StackExchange;
@@ -251,7 +250,6 @@ public static class SimApiExtensions
.AddJsonOptions(opt => .AddJsonOptions(opt =>
{ {
opt.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; opt.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
opt.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
}); });
} }
+52 -56
View File
@@ -1,20 +1,19 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text.Json; using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MQTTnet; using MQTTnet;
using MQTTnet.Packets;
using SimApi.Communications; using SimApi.Communications;
using SimApi.Helpers; using SimApi.Helpers;
using System.Collections.Concurrent;
namespace SimApi; namespace SimApi;
public partial class Synapse public partial class Synapse
{ {
private Dictionary<string, TaskCompletionSource<string>> ResponseCompletionSources { get; } = new(); private ConcurrentDictionary<string, TaskCompletionSource<string>> ResponseCompletionSources { get; } = new();
private string EventClientTopicPrefix => $"{Options.SysName}/{Options.AppName}/rpc/client/{Options.AppId}/"; private string EventClientTopicPrefix => $"{Options.SysName}/{Options.AppName}/rpc/client/{Options.AppId}/";
@@ -27,7 +26,7 @@ public partial class Synapse
var messageId = e.ApplicationMessage.Topic.Replace(EventClientTopicPrefix, string.Empty); var messageId = e.ApplicationMessage.Topic.Replace(EventClientTopicPrefix, string.Empty);
if (!ResponseCompletionSources.TryGetValue(messageId, out var tcs)) return Task.CompletedTask; if (!ResponseCompletionSources.TryGetValue(messageId, out var tcs)) return Task.CompletedTask;
tcs.SetResult(reqBody); tcs.SetResult(reqBody);
ResponseCompletionSources.Remove(messageId); ResponseCompletionSources.Remove(messageId, out _);
return Task.CompletedTask; return Task.CompletedTask;
}; };
SubRpcClientTopic(); SubRpcClientTopic();
@@ -43,64 +42,61 @@ 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)
{ {
// 检查并发情况下的线程安全问题,使用锁保证字典操作的原子性 // 移除锁语句
lock (ResponseCompletionSources) string paramJson;
if (param is string strParam)
{ {
string paramJson; paramJson = strParam;
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.TryAdd(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))
{ {
paramJson = strParam; response = tcs.Task.Result;
logger.LogDebug(
"Synapse RPC Client Response: ({BasicPropertiesCorrelationId}) {BasicPropertiesType}@{BasicPropertiesReplyTo} -> {OptionsAppName}\n{S}",
messageId, action, app, Options.AppName, response);
} }
else else
{ {
paramJson = JsonSerializer.Serialize(param, SimApiUtil.JsonOption); response = JsonSerializer.Serialize(new SimApiBaseResponse(502, "timeout"), 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;
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;
} }
catch
{
response = JsonSerializer.Serialize(new SimApiBaseResponse(500, "Synapse RPC Client Error"),
SimApiUtil.JsonOption);
}
return response;
} }
} }