Skip to content

Commit 5ddbe3c

Browse files
committed
feat: 增加锁控制并发
1 parent 84764e3 commit 5ddbe3c

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/BootstrapBlazor/Services/TcpSocket/TcpSocketClientBase.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ public abstract class TcpSocketClientBase(SocketClientOptions options) : ITcpSoc
6565
private CancellationTokenSource? _receiveCancellationTokenSource;
6666
private CancellationTokenSource? _autoConnectTokenSource;
6767

68+
#if NET9_0_OR_GREATER
69+
private readonly Lock _lock = new();
70+
#else
71+
private readonly object _lock = new();
72+
#endif
73+
6874
/// <summary>
6975
/// <inheritdoc/>
7076
/// </summary>
@@ -78,6 +84,15 @@ public async ValueTask<bool> ConnectAsync(IPEndPoint endPoint, CancellationToken
7884
return true;
7985
}
8086

87+
lock (_lock)
88+
{
89+
if (IsConnected)
90+
{
91+
return true;
92+
}
93+
}
94+
95+
var reconnect = true;
8196
var ret = false;
8297
SocketClientProvider = ServiceProvider?.GetRequiredService<ISocketClientProvider>()
8398
?? throw new InvalidOperationException("SocketClientProvider is not registered in the service provider.");

0 commit comments

Comments
 (0)