Skip to content

Commit 45a42da

Browse files
committed
Remove locks
1 parent 9fa0215 commit 45a42da

File tree

1 file changed

+12
-45
lines changed

1 file changed

+12
-45
lines changed

src/MongoDB.Driver/Core/Connections/BinaryConnection.cs

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ internal sealed class BinaryConnection : IConnection
5050
private DateTime _openedAtUtc;
5151
private readonly object _openLock = new object();
5252
private Task _openTask;
53-
private readonly SemaphoreSlim _receiveLock;
5453
private CompressorType? _sendCompressorType;
55-
private readonly SemaphoreSlim _sendLock;
5654
private readonly ConnectionSettings _settings;
5755
private readonly InterlockedInt32 _state;
5856
private Stream _stream;
@@ -77,8 +75,6 @@ public BinaryConnection(
7775
Ensure.IsNotNull(eventSubscriber, nameof(eventSubscriber));
7876

7977
_connectionId = new ConnectionId(serverId, settings.ConnectionIdLocalValueProvider());
80-
_receiveLock = new SemaphoreSlim(1);
81-
_sendLock = new SemaphoreSlim(1);
8278
_state = new InterlockedInt32(State.Initial);
8379

8480
_compressorSource = new CompressorSource(settings.Compressors);
@@ -171,9 +167,6 @@ private void Dispose(bool disposing)
171167
_eventLogger.LogAndPublish(new ConnectionClosingEvent(_connectionId, EventContext.OperationId));
172168

173169
var stopwatch = Stopwatch.StartNew();
174-
_receiveLock.Dispose();
175-
_sendLock.Dispose();
176-
177170
if (_stream != null)
178171
{
179172
try
@@ -459,57 +452,31 @@ private int GetResponseTo(IByteBuffer message)
459452

460453
private void SendBuffer(OperationContext operationContext, IByteBuffer buffer)
461454
{
462-
_sendLock.Wait(operationContext.RemainingTimeout, operationContext.CancellationToken);
463455
try
464456
{
465-
if (_state.Value == State.Failed)
466-
{
467-
throw new MongoConnectionClosedException(_connectionId);
468-
}
469-
470-
try
471-
{
472-
_stream.WriteBytes(operationContext, buffer, 0, buffer.Length);
473-
_lastUsedAtUtc = DateTime.UtcNow;
474-
}
475-
catch (Exception ex)
476-
{
477-
var wrappedException = WrapExceptionIfRequired(ex, "sending a message to the server");
478-
ConnectionFailed(wrappedException ?? ex);
479-
if (wrappedException == null) { throw; } else { throw wrappedException; }
480-
}
457+
_stream.WriteBytes(operationContext, buffer, 0, buffer.Length);
458+
_lastUsedAtUtc = DateTime.UtcNow;
481459
}
482-
finally
460+
catch (Exception ex)
483461
{
484-
_sendLock.Release();
462+
var wrappedException = WrapExceptionIfRequired(ex, "sending a message to the server");
463+
ConnectionFailed(wrappedException ?? ex);
464+
if (wrappedException == null) { throw; } else { throw wrappedException; }
485465
}
486466
}
487467

488468
private async Task SendBufferAsync(OperationContext operationContext, IByteBuffer buffer)
489469
{
490-
await _sendLock.WaitAsync(operationContext.RemainingTimeout, operationContext.CancellationToken).ConfigureAwait(false);
491470
try
492471
{
493-
if (_state.Value == State.Failed)
494-
{
495-
throw new MongoConnectionClosedException(_connectionId);
496-
}
497-
498-
try
499-
{
500-
await _stream.WriteBytesAsync(operationContext, buffer, 0, buffer.Length).ConfigureAwait(false);
501-
_lastUsedAtUtc = DateTime.UtcNow;
502-
}
503-
catch (Exception ex)
504-
{
505-
var wrappedException = WrapExceptionIfRequired(ex, "sending a message to the server");
506-
ConnectionFailed(wrappedException ?? ex);
507-
if (wrappedException == null) { throw; } else { throw wrappedException; }
508-
}
472+
await _stream.WriteBytesAsync(operationContext, buffer, 0, buffer.Length).ConfigureAwait(false);
473+
_lastUsedAtUtc = DateTime.UtcNow;
509474
}
510-
finally
475+
catch (Exception ex)
511476
{
512-
_sendLock.Release();
477+
var wrappedException = WrapExceptionIfRequired(ex, "sending a message to the server");
478+
ConnectionFailed(wrappedException ?? ex);
479+
if (wrappedException == null) { throw; } else { throw wrappedException; }
513480
}
514481
}
515482

0 commit comments

Comments
 (0)