@@ -50,9 +50,7 @@ internal sealed class BinaryConnection : IConnection
50
50
private DateTime _openedAtUtc ;
51
51
private readonly object _openLock = new object ( ) ;
52
52
private Task _openTask ;
53
- private readonly SemaphoreSlim _receiveLock ;
54
53
private CompressorType ? _sendCompressorType ;
55
- private readonly SemaphoreSlim _sendLock ;
56
54
private readonly ConnectionSettings _settings ;
57
55
private readonly InterlockedInt32 _state ;
58
56
private Stream _stream ;
@@ -77,8 +75,6 @@ public BinaryConnection(
77
75
Ensure . IsNotNull ( eventSubscriber , nameof ( eventSubscriber ) ) ;
78
76
79
77
_connectionId = new ConnectionId ( serverId , settings . ConnectionIdLocalValueProvider ( ) ) ;
80
- _receiveLock = new SemaphoreSlim ( 1 ) ;
81
- _sendLock = new SemaphoreSlim ( 1 ) ;
82
78
_state = new InterlockedInt32 ( State . Initial ) ;
83
79
84
80
_compressorSource = new CompressorSource ( settings . Compressors ) ;
@@ -171,9 +167,6 @@ private void Dispose(bool disposing)
171
167
_eventLogger . LogAndPublish ( new ConnectionClosingEvent ( _connectionId , EventContext . OperationId ) ) ;
172
168
173
169
var stopwatch = Stopwatch . StartNew ( ) ;
174
- _receiveLock . Dispose ( ) ;
175
- _sendLock . Dispose ( ) ;
176
-
177
170
if ( _stream != null )
178
171
{
179
172
try
@@ -459,57 +452,31 @@ private int GetResponseTo(IByteBuffer message)
459
452
460
453
private void SendBuffer ( OperationContext operationContext , IByteBuffer buffer )
461
454
{
462
- _sendLock . Wait ( operationContext . RemainingTimeout , operationContext . CancellationToken ) ;
463
455
try
464
456
{
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 ;
481
459
}
482
- finally
460
+ catch ( Exception ex )
483
461
{
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 ; }
485
465
}
486
466
}
487
467
488
468
private async Task SendBufferAsync ( OperationContext operationContext , IByteBuffer buffer )
489
469
{
490
- await _sendLock . WaitAsync ( operationContext . RemainingTimeout , operationContext . CancellationToken ) . ConfigureAwait ( false ) ;
491
470
try
492
471
{
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 ;
509
474
}
510
- finally
475
+ catch ( Exception ex )
511
476
{
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 ; }
513
480
}
514
481
}
515
482
0 commit comments