Skip to content

Commit fd7a2ce

Browse files
committed
perf: 优化ws异常处理和资源释放方式
1 parent 4b12006 commit fd7a2ce

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

Cyaim.WebSocketServer/Cyaim.WebSocketServer/Infrastructure/Handlers/MvcHandler/MvcChannelHandler.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ public static async Task<MvcResponseScheme> MvcDistributeAsync(WebSocketRouteOpt
839839
{
840840
long requestTime = DateTime.Now.Ticks;
841841
string requestPath = request.Target.ToLower();
842-
842+
IServiceScope serviceScope = null;
843843
try
844844
{
845845
// 从键值对中获取对应的执行函数
@@ -849,20 +849,20 @@ public static async Task<MvcResponseScheme> MvcDistributeAsync(WebSocketRouteOpt
849849
{
850850
goto NotFound;
851851
}
852-
Type clss = webSocketOptions.WatchAssemblyContext.WatchEndPoint.FirstOrDefault(x => x.MethodPath == requestPath)?.Class;
853-
if (clss == null)
852+
Type targetClass = webSocketOptions.WatchAssemblyContext.WatchEndPoint.FirstOrDefault(x => x.MethodPath == requestPath)?.Class;
853+
if (targetClass == null)
854854
{
855855
//找不到访问目标
856856
goto NotFound;
857857
}
858858

859859
#region 注入Socket的HttpContext和WebSocket客户端
860-
webSocketOptions.WatchAssemblyContext.MaxConstructorParameters.TryGetValue(clss, out ConstructorParameter constructorParameter);
860+
webSocketOptions.WatchAssemblyContext.MaxConstructorParameters.TryGetValue(targetClass, out ConstructorParameter constructorParameter);
861861

862862
object[] instanceParmas = new object[constructorParameter.ParameterInfos.Length];
863863
// 从Scope DI容器提取目标类构造函数所需的对象
864864
var serviceScopeFactory = WebSocketRouteOption.ApplicationServices.GetService<IServiceScopeFactory>();
865-
var serviceScope = serviceScopeFactory.CreateScope();
865+
serviceScope = serviceScopeFactory.CreateScope();
866866
var scopeIocProvider = serviceScope.ServiceProvider;
867867
for (int i = 0; i < constructorParameter.ParameterInfos.Length; i++)
868868
{
@@ -885,11 +885,11 @@ public static async Task<MvcResponseScheme> MvcDistributeAsync(WebSocketRouteOpt
885885
}
886886
}
887887

888-
object inst = Activator.CreateInstance(clss, instanceParmas);
888+
object inst = Activator.CreateInstance(targetClass, instanceParmas);
889889

890890
// 使用注入器工厂注入 HttpContext 和 WebSocket(支持源代码生成和反射两种方式)
891891
var injectorFactory = webSocketOptions.InjectorFactory ?? new EndpointInjectorFactory(webSocketOptions);
892-
var injector = injectorFactory.GetOrCreateInjector(clss);
892+
var injector = injectorFactory.GetOrCreateInjector(targetClass);
893893
injector.Inject(inst, context, webSocket);
894894
#endregion
895895

@@ -1081,22 +1081,22 @@ public static async Task<MvcResponseScheme> MvcDistributeAsync(WebSocketRouteOpt
10811081
// await 会抛出 AggregateException,提取内部异常
10821082
if (ex is AggregateException aggEx && aggEx.InnerException != null)
10831083
{
1084-
throw aggEx.InnerException;
1084+
ex = aggEx.InnerException;
10851085
}
1086-
throw;
1086+
mvcResponse = await webSocketOptions.OnException(ex, request, mvcResponse, context, webSocketOptions, context.Request.Path, logger).ConfigureAwait(false);
10871087
}
10881088

10891089
// 检查任务状态(await 后任务已完成,但需要检查是否有异常或取消)
10901090
if (task.IsFaulted && task.Exception != null)
10911091
{
10921092
// 抛出内部异常(AggregateException 的第一个内部异常)
10931093
var innerException = task.Exception.InnerException ?? task.Exception;
1094-
throw innerException;
1094+
mvcResponse = await webSocketOptions.OnException(innerException, request, mvcResponse, context, webSocketOptions, context.Request.Path, logger).ConfigureAwait(false);
10951095
}
10961096

10971097
if (task.IsCanceled)
10981098
{
1099-
throw new TaskCanceledException(task);
1099+
mvcResponse = await webSocketOptions.OnException(new TaskCanceledException(task), request, mvcResponse, context, webSocketOptions, context.Request.Path, logger).ConfigureAwait(false);
11001100
}
11011101

11021102
// 检查是否是 Task<T> 类型,需要获取返回值
@@ -1113,9 +1113,6 @@ public static async Task<MvcResponseScheme> MvcDistributeAsync(WebSocketRouteOpt
11131113

11141114
#endregion
11151115

1116-
// Dispose ioc scope
1117-
serviceScope?.Dispose();
1118-
serviceScope = null;
11191116

11201117
mvcResponse.Id = request.Id;
11211118
mvcResponse.Target = request.Target;
@@ -1136,6 +1133,12 @@ public static async Task<MvcResponseScheme> MvcDistributeAsync(WebSocketRouteOpt
11361133

11371134
return customResp;
11381135
}
1136+
finally
1137+
{
1138+
// Dispose ioc scope
1139+
serviceScope?.Dispose();
1140+
serviceScope = null;
1141+
}
11391142

11401143
NotFound:
11411144
logger.LogInformation(string.Format(I18nText.WS_INTERACTIVE_TEXT_TEMPALTE, context.Connection.RemoteIpAddress, context.Connection.RemotePort, context.Connection.Id, I18nText.MvcDistributeAsync_EndPointNotFound + requestPath));

0 commit comments

Comments
 (0)