lith пре 4 година
родитељ
комит
907998147e

+ 11 - 4
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Api/LocalApi/ApiNodeMng.cs

@@ -1,25 +1,30 @@
 using Sers.Core.Module.Rpc;
 using System.Collections.Generic;
+using System.Collections.Immutable;
 using System.Linq;
 using System.Runtime.CompilerServices;
 using Vit.Extensions;
 
 namespace Sers.Core.Module.Api.LocalApi
 {
+    /// <summary>
+    ///  https://www.xin3721.com/ArticlecSharp/net17449.html
+    /// </summary>
     public class ApiNodeMng
     {
+        //SortedDictionary
 
         /// <summary>
         /// 映射  /{httpMethod}{route} -> LocalApiNode
         /// 例如 "/POST/api/value"
         /// </summary>
-        protected readonly SortedDictionary<string, IApiNode> apiNodeMapWithMethod = new SortedDictionary<string, IApiNode>();
+        protected ImmutableSortedDictionary<string, IApiNode> apiNodeMapWithMethod =  ImmutableSortedDictionary<string, IApiNode>.Empty;
 
         /// <summary>
         /// 映射  {route} -> LocalApiNode
         /// 例如 "/api/value"
         /// </summary>
-        protected readonly SortedDictionary<string, IApiNode> apiNodeMapWithoutMethod = new SortedDictionary<string, IApiNode>();
+        protected ImmutableSortedDictionary<string, IApiNode> apiNodeMapWithoutMethod = ImmutableSortedDictionary<string, IApiNode>.Empty;
         
 
         public IEnumerable<IApiNode> apiNodes => apiNodeMapWithMethod.Select((kv) => kv.Value).Concat(apiNodeMapWithoutMethod.Select((kv) => kv.Value));
@@ -43,11 +48,13 @@ namespace Sers.Core.Module.Api.LocalApi
 
             if (string.IsNullOrEmpty(method))
             {
-                apiNodeMapWithoutMethod[route] = apiNode;
+                //apiNodeMapWithoutMethod[route] = apiNode;
+                apiNodeMapWithoutMethod = apiNodeMapWithoutMethod.SetItem(route, apiNode);            
             }
             else
             {
-                apiNodeMapWithMethod[method + route] = apiNode;
+                //apiNodeMapWithMethod[method + route] = apiNode;
+                apiNodeMapWithMethod = apiNodeMapWithMethod.SetItem(method + route, apiNode);         
             }            
         }
 

+ 4 - 4
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Api/LocalApi/Event/LocalApiEvent.cs

@@ -1,19 +1,19 @@
 using Sers.Core.Module.Message;
 using Sers.Core.Module.Rpc;
 using System;
-using System.Collections.Generic;
 using System.Runtime.CompilerServices;
 using Vit.Core.Module.Log;
+using Vit.Extensions.IEnumerable;
 
 namespace Sers.Core.Module.Api.LocalApi.Event
 {
     public class LocalApiEvent : IDisposable
     {         
 
-        private List<IDisposable> events_OnDispose;
+        private IDisposable[] events_OnDispose;
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        internal LocalApiEvent(List<IDisposable> events_OnDispose)
+        internal LocalApiEvent(IDisposable[] events_OnDispose)
         {
             this.events_OnDispose = events_OnDispose;
         }
@@ -26,7 +26,7 @@ namespace Sers.Core.Module.Api.LocalApi.Event
         }
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public virtual void Dispose()
+        public void Dispose()
         {
             if (events_OnDispose == null) return;
 

+ 6 - 4
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Api/LocalApi/Event/LocalApiEventMng.cs

@@ -47,7 +47,7 @@ namespace Sers.Core.Module.Api.LocalApi.Event
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public void AddEvent_ApiScope( params IApiScopeEvent[] apiScopeEvents)
         {
-            if (null == apiScopeEvents) return;
+            if (null == apiScopeEvents || apiScopeEvents.Length==0) return;
 
             if (null == apiScopeEventList)
             {
@@ -61,8 +61,10 @@ namespace Sers.Core.Module.Api.LocalApi.Event
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public LocalApiEvent CreateApiEvent()
         {
-            List<IDisposable> events_OnDispose =
-                apiScopeEventList?.Select(apiScope =>
+            if (apiScopeEventList == null) return null;
+
+            var events_OnDispose =
+                apiScopeEventList.Select(apiScope =>
                 {
                     try
                     {
@@ -74,7 +76,7 @@ namespace Sers.Core.Module.Api.LocalApi.Event
                     }
                     return null;
                 })
-                .ToList();
+                .ToArray();
 
             return new LocalApiEvent(events_OnDispose);
         }

+ 3 - 4
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Api/LocalApi/LocalApiService.cs

@@ -97,7 +97,7 @@ namespace Sers.Core.Module.Api.LocalApi
         /// <param name="apiRequest"></param>
         /// <returns></returns>
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        internal ApiMessage CallLocalApi(ApiMessage apiRequest)
+        public ApiMessage CallLocalApi(ApiMessage apiRequest)
         {           
             using (var rpcContext = new RpcContext())
             using (var localApiEvent = LocalApiEventMng.Instance.CreateApiEvent())
@@ -292,15 +292,14 @@ namespace Sers.Core.Module.Api.LocalApi
 
             [MethodImpl(MethodImplOptions.AggressiveInlining)]
             void Processor(RequestInfo requestInfo)
-            {
-                ApiMessage apiReply;
+            {             
 
                 try
                 {
                     CommunicationManageServer.CurConn = requestInfo.conn;
 
                     //处理请求
-                    apiReply = callLocalApi(requestInfo.apiRequest);
+                    ApiMessage apiReply = callLocalApi(requestInfo.apiRequest);
 
                     //调用请求回调
                     requestInfo.callback(requestInfo.sender, apiReply);

+ 1 - 1
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Message/ApiMessage.cs

@@ -20,7 +20,7 @@ namespace Sers.Core.Module.Message
                 var files = base.Files;
                 if (files == null)
                 {
-                    base.Files= files = new List<ArraySegment<byte>>(2) { ArraySegmentByteExtensions.Null, ArraySegmentByteExtensions.Null};
+                    base.Files = files = new List<ArraySegment<byte>>(2) { ArraySegmentByteExtensions.Null, ArraySegmentByteExtensions.Null };
                 }
                 return files;
             }

+ 1 - 0
dotnet/Library/Sers/Sers.Core/Sers.Core/Sers.Core.csproj

@@ -28,6 +28,7 @@
   <ItemGroup>
     <PackageReference Include="Disruptor" Version="3.6.2" />
     <PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.0.0" />
+    <PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
   </ItemGroup>  
  
   

+ 15 - 3
dotnet/Library/Sers/Sers.Core/Test/Sers.Core.Module.LocalApi.Qps/LocalApi/Extensions/LocalApiMngExtensions.cs

@@ -13,7 +13,19 @@ namespace Sers.Core.Module.LocalApi.MsTest.LocalApi.Extensions
 
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public static void CallLocalApi(this LocalApiService data,string route, Object arg,Action<ApiMessage> onSuc)
+        public static ApiMessage CallLocalApi(this LocalApiService data, string route, Object arg)
+        {
+            var apiRequestMessage = new ApiMessage();
+            //apiRequestMessage.InitAsApiRequestMessage(route, arg);
+
+            return data.CallLocalApi(apiRequestMessage);
+
+        }
+
+
+
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public static void CallLocalApiAsync(this LocalApiService data,string route, Object arg,Action<ApiMessage> onSuc)
         {
             var apiRequestMessage = new ApiMessage().InitAsApiRequestMessage(route, arg);                   
 
@@ -27,9 +39,9 @@ namespace Sers.Core.Module.LocalApi.MsTest.LocalApi.Extensions
 
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public static void CallLocalApi<ReturnType>(this LocalApiService data, string route, Object arg,Action<ReturnType> onSuc)
+        public static void CallLocalApiAsync<ReturnType>(this LocalApiService data, string route, Object arg,Action<ReturnType> onSuc)
         {
-            data.CallLocalApi(route, arg, replyMessage =>
+            data.CallLocalApiAsync(route, arg, replyMessage =>
               {
                   var returnBytes = replyMessage.value_OriData;
                   var returnValue = returnBytes.DeserializeFromArraySegmentByte<ReturnType>();

+ 33 - 4
dotnet/Library/Sers/Sers.Core/Test/Sers.Core.Module.LocalApi.Qps/LocalApi/LocalApiTest.cs

@@ -2,7 +2,7 @@
 using Statistics;
 using Sers.Core.Module.Api.LocalApi;
 using Sers.Core.Module.LocalApi.MsTest.LocalApi.Extensions;
-
+using System.Threading.Tasks;
 
 namespace Sers.Core.Module.LocalApi.MsTest.LocalApi
 {
@@ -27,8 +27,37 @@ namespace Sers.Core.Module.LocalApi.MsTest.LocalApi
         }
 
 
-
         public static void StartThread()
+        {
+            QpsData qpsInfo = new QpsData(statisticsQps);
+            Task.Run(() =>
+            {
+
+                while (true)
+                {
+                    try
+                    {
+                        for (var t = 0; t < 10000; t++)
+                        {
+                            //string route = "/Test/api/GetDeviceGuidList";
+                            //string arg = "asfsdf";
+                            //object argValue = new { arg };
+
+                            var apiReplyMessage = localApiService.CallLocalApi("/a", null);                    
+                        }
+
+                        qpsInfo.RequestCount++;
+                    }
+                    catch (Exception ex)
+                    {
+                    }
+                }
+
+            });
+
+        }
+
+        public static void StartThread_Async()
         {
             QpsData qpsInfo = new QpsData(statisticsQps);
 
@@ -40,7 +69,7 @@ namespace Sers.Core.Module.LocalApi.MsTest.LocalApi
             {
 
                 t++;
-                if (t >= 1000)
+                if (t >= 10000)
                 {
                     t = 0;
                     qpsInfo.RequestCount++;
@@ -50,7 +79,7 @@ namespace Sers.Core.Module.LocalApi.MsTest.LocalApi
                 //string arg = "asfsdf";
                 //object argValue = new { arg };
 
-                localApiService.CallLocalApi<string>("/a", null, callApi);
+                localApiService.CallLocalApiAsync<string>("/a", null, callApi);
             };
 
             callApi(null);

+ 2 - 1
dotnet/Library/Vit/Vit.Core/Vit.Core/Extensions/byte/ArraySegmentByteExtensions.cs

@@ -7,7 +7,8 @@ namespace Vit.Extensions
 {
     public static partial class ArraySegmentByteExtensions
     {
-        public static readonly ArraySegment<byte> Null = new ArraySegment<byte>(new byte[0],0,0);
+        //public static readonly ArraySegment<byte> Null = new ArraySegment<byte>(new byte[0], 0, 0);
+        public static readonly ArraySegment<byte> Null = default;
 
         //public static ArraySegment<T> Null<T>()
         //{

+ 3 - 0
dotnet/todo.txt

@@ -1,6 +1,9 @@
 CUR:
 
 
+ImmutableSortedDictionary 替代Api字典
+
+
 RequestAdaptor.cs  static void PackageReqRepFrame(long reqKey, Vit.Core.Util.Pipelines.ByteData oriMsg, out Vit.Core.Util.Pipelines.ByteData reqRepFrame) 优化减少创建ByteData对象
 
 对象缓存是否弃用