From c1907bff9cb78ea733f6d6460cc8cf268669dd86 Mon Sep 17 00:00:00 2001 From: Elivo Date: Thu, 19 Mar 2026 17:18:52 +0800 Subject: [PATCH] =?UTF-8?q?API=20=E5=8F=8D=E5=B0=84=E6=94=AF=E6=8C=81=20Un?= =?UTF-8?q?parallel=20=E7=89=B9=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer/UnparallelAttribute.cs | 2 +- Apewer/Web/ApiApplication.cs | 7 ++++++ Apewer/Web/ApiFunction.cs | 7 ++++++ Apewer/Web/ApiProcessor.cs | 42 ++++++++++++++++++++++++++++++----- 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/Apewer/UnparallelAttribute.cs b/Apewer/UnparallelAttribute.cs index d5b39a2..5b2e89f 100644 --- a/Apewer/UnparallelAttribute.cs +++ b/Apewer/UnparallelAttribute.cs @@ -4,7 +4,7 @@ namespace Apewer { /// 表示方法不可并行。 - [AttributeUsage(AttributeTargets.Method)] + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] public sealed class UnparallelAttribute : Attribute { diff --git a/Apewer/Web/ApiApplication.cs b/Apewer/Web/ApiApplication.cs index 6d5b348..7850eb3 100644 --- a/Apewer/Web/ApiApplication.cs +++ b/Apewer/Web/ApiApplication.cs @@ -22,6 +22,7 @@ namespace Apewer.Web // invoke & enumerate bool _independent = false; bool _hidden = false; + bool _unparallel = false; // functions Dictionary _dict = new Dictionary(); @@ -52,6 +53,9 @@ namespace Apewer.Web /// public bool Hidden { get => _hidden; } + /// + public bool Unparallel { get => _unparallel; } + /// public ApiFunction[] Functions { get => _list.ToArray(); } @@ -98,6 +102,9 @@ namespace Apewer.Web // independent if (type.Contains(false)) _independent = true; + // unparallel + if (type.Contains(false)) _unparallel = true; + // Module var assemblyName = type.Assembly.GetName(); _module = TextUtility.Join("-", assemblyName.Name, assemblyName.Version.ToString()); diff --git a/Apewer/Web/ApiFunction.cs b/Apewer/Web/ApiFunction.cs index 885ca94..b615bf0 100644 --- a/Apewer/Web/ApiFunction.cs +++ b/Apewer/Web/ApiFunction.cs @@ -22,6 +22,7 @@ namespace Apewer.Web string _description = null; bool _hidden = false; + bool _unparallel = false; #endregion @@ -51,6 +52,9 @@ namespace Apewer.Web /// public bool Hidden { get => _hidden; } + /// + public bool Unparallel { get => _unparallel; } + #endregion /// @@ -118,6 +122,9 @@ namespace Apewer.Web // hidden if (method.Contains(false)) _hidden = true; + // unparallel + if (method.Contains(false)) _unparallel = true; + // 参数。 _parameters = parameters; } diff --git a/Apewer/Web/ApiProcessor.cs b/Apewer/Web/ApiProcessor.cs index dec3352..d174368 100644 --- a/Apewer/Web/ApiProcessor.cs +++ b/Apewer/Web/ApiProcessor.cs @@ -404,12 +404,25 @@ namespace Apewer.Web } else { + // 创建默认控制器。 var controller = null as ApiController; try { - controller = CreateController(@default, _context); - InvokeFunction(controller, application, null, options, request, response); + var unparallel = @default.Contains(false); + if (unparallel) + { + UnparallelLocker.InLock(GetUnparallelKey(@default), () => + { + controller = CreateController(@default, _context); + InvokeFunction(controller, application, null, options, request, response); + }); + } + else + { + controller = CreateController(@default, _context); + InvokeFunction(controller, application, null, options, request, response); + } } catch (Exception ex) { @@ -428,8 +441,27 @@ namespace Apewer.Web var controller = null as ApiController; try { - controller = CreateController(application.Type, _context); - InvokeFunction(controller, application, function, options, request, response); + if (application.Unparallel) + { + UnparallelLocker.InLock(GetUnparallelKey(application.Type), () => + { + controller = CreateController(application.Type, _context); + InvokeFunction(controller, application, function, options, request, response); + }); + } + else if (function != null && function.Unparallel) + { + UnparallelLocker.InLock(GetUnparallelKey(function.Method), () => + { + controller = CreateController(application.Type, _context); + InvokeFunction(controller, application, function, options, request, response); + }); + } + else + { + controller = CreateController(application.Type, _context); + InvokeFunction(controller, application, function, options, request, response); + } } catch (Exception ex) { @@ -831,7 +863,7 @@ namespace Apewer.Web static string GetUnparallelKey(Type type) { - return $"{type.DeclaringType.Assembly.ToString()}"; + return $"{type.Assembly.ToString()} | {type.FullName}"; } #endregion