refactor: 通道释放等待池

This commit is contained in:
Diego
2024-06-20 14:42:53 +08:00
parent 30a24ced1d
commit ac6659a293
9 changed files with 29 additions and 16 deletions

View File

@@ -116,7 +116,7 @@ public sealed class DatabaseLoggerProvider : ILoggerProvider, ISupportExternalSc
_processQueueTask?.Wait(1500);
}
catch (OperationCanceledException) { }
catch (AggregateException ex) when (ex.InnerExceptions.Count == 1 && ex.InnerExceptions[0] is TaskCanceledException) { }
catch (AggregateException ex) when (ex.InnerExceptions.Count == 1 && ex.InnerExceptions[0] is OperationCanceledException) { }
catch { }
// 清空数据库日志记录器

View File

@@ -148,8 +148,8 @@ public sealed class FileLoggerProvider : ILoggerProvider, ISupportExternalScope
// 设置 1.5秒的缓冲时间,避免还有日志消息没有完成写入文件中
_processQueueTask?.Wait(1500);
}
catch (TaskCanceledException) { }
catch (AggregateException ex) when (ex.InnerExceptions.Count == 1 && ex.InnerExceptions[0] is TaskCanceledException) { }
catch (OperationCanceledException) { }
catch (AggregateException ex) when (ex.InnerExceptions.Count == 1 && ex.InnerExceptions[0] is OperationCanceledException) { }
catch { }
// 清空文件日志记录器

View File

@@ -8,6 +8,8 @@
// QQ群605534569
//------------------------------------------------------------------------------
using TouchSocket.Core;
namespace ThingsGateway.Foundation;
/// <inheritdoc cref="TcpSessionClient"/>
@@ -24,7 +26,7 @@ public class TcpSessionClientChannel : TcpSessionClient, IClientChannel
/// <summary>
/// 等待池
/// </summary>
public WaitHandlePool<MessageBase> WaitHandlePool { get; } = new();
public WaitHandlePool<MessageBase> WaitHandlePool { get; private set; } = new();
/// <inheritdoc/>
public ConcurrentList<IProtocol> Collects { get; } = new();
@@ -49,7 +51,7 @@ public class TcpSessionClientChannel : TcpSessionClient, IClientChannel
/// <inheritdoc/>
public override string ToString()
{
return $"{IP}:{Port}";
return $"{IP}:{Port}:{Id}";
}
/// <inheritdoc/>
@@ -72,6 +74,12 @@ public class TcpSessionClientChannel : TcpSessionClient, IClientChannel
this.CloseAsync(msg).ConfigureAwait(false);
}
public override Task CloseAsync(string msg)
{
WaitHandlePool.SafeDispose();
return base.CloseAsync(msg);
}
public void Connect(int millisecondsTimeout = 3000, CancellationToken token = default)
{
throw new NotSupportedException();
@@ -81,6 +89,7 @@ public class TcpSessionClientChannel : TcpSessionClient, IClientChannel
protected override void Dispose(bool disposing)
{
if (DisposedValue) return;
WaitHandlePool.SafeDispose();
base.Dispose(disposing);
}

View File

@@ -116,12 +116,11 @@ public abstract class ProtocolBase : DisposableObject, IProtocol
/// <returns></returns>
protected Task ChannelStoped(IClientChannel channel)
{
if (channel == this.Channel)
try
{
//取消全部等待池
channel.WaitHandlePool.CancelAll();
}
else
catch
{
}
@@ -729,7 +728,16 @@ public abstract class ProtocolBase : DisposableObject, IProtocol
//只关闭,不释放
Channel.Close();
if (Channel is IClientChannel client)
{
client.WaitHandlePool.SafeDispose();
}
else if (Channel is TcpServiceChannel tcpServiceChannel)
{
tcpServiceChannel.Clients.ForEach(a =>
{
a.WaitHandlePool.SafeDispose();
});
}
}
catch (Exception ex)
{

View File

@@ -620,7 +620,7 @@ public abstract class DeviceHostedService : BackgroundService
}
}
}
catch (TaskCanceledException)
catch (OperationCanceledException)
{
}
catch (ObjectDisposedException)

View File

@@ -355,7 +355,7 @@ public class ManagementHostedService : BackgroundService
}
await Task.Delay(Options.Redundancy.SyncInterval, stoppingToken).ConfigureAwait(false);
}
catch (TaskCanceledException)
catch (OperationCanceledException)
{
}
catch (ObjectDisposedException)

View File

@@ -655,7 +655,7 @@ public class ChannelThread
await Task.Delay(1000, token).ConfigureAwait(false); // 默认延迟一段时间后再继续执行
}
}
catch (TaskCanceledException)
catch (OperationCanceledException)
{
if (stoppingToken.IsCancellationRequested)
driver.AfterStop(); // 执行驱动的释放操作

View File

@@ -351,10 +351,6 @@ public abstract class DriverBase : DisposableObject
// 正常返回None状态
return ThreadRunReturnTypeEnum.None;
}
catch (TaskCanceledException)
{
return ThreadRunReturnTypeEnum.Break;
}
catch (OperationCanceledException)
{
return ThreadRunReturnTypeEnum.Break;

View File

@@ -70,7 +70,7 @@ public class DoTask
return;
await DoWork(_cancelTokenSource.Token).ConfigureAwait(false);
}
catch (TaskCanceledException)
catch (OperationCanceledException)
{
}
catch (ObjectDisposedException)