@@ -141,6 +141,7 @@ private async Task MaybeConfirmSelect(CancellationToken cancellationToken)
141141 if ( _publisherConfirmationTrackingEnabled )
142142 {
143143 _confirmsTaskCompletionSources . Clear ( ) ;
144+ MaybeUnblockPublishers ( ) ;
144145 }
145146 _nextPublishSeqNo = 1 ;
146147 }
@@ -186,13 +187,15 @@ private void HandleAck(ulong deliveryTag, bool multiple)
186187 {
187188 pair . Value . SetResult ( true ) ;
188189 _confirmsTaskCompletionSources . Remove ( pair . Key , out _ ) ;
190+ MaybeUnblockPublishers ( ) ;
189191 }
190192 }
191193 }
192194 else
193195 {
194196 if ( _confirmsTaskCompletionSources . TryRemove ( deliveryTag , out TaskCompletionSource < bool > ? tcs ) )
195197 {
198+ MaybeUnblockPublishers ( ) ;
196199 tcs . SetResult ( true ) ;
197200 }
198201 }
@@ -212,13 +215,15 @@ private void HandleNack(ulong deliveryTag, bool multiple, bool isReturn)
212215 {
213216 pair . Value . SetException ( new PublishException ( pair . Key , isReturn ) ) ;
214217 _confirmsTaskCompletionSources . Remove ( pair . Key , out _ ) ;
218+ MaybeUnblockPublishers ( ) ;
215219 }
216220 }
217221 }
218222 else
219223 {
220224 if ( _confirmsTaskCompletionSources . Remove ( deliveryTag , out TaskCompletionSource < bool > ? tcs ) )
221225 {
226+ MaybeUnblockPublishers ( ) ;
222227 tcs . SetException ( new PublishException ( deliveryTag , isReturn ) ) ;
223228 }
224229 }
@@ -261,6 +266,7 @@ await _confirmSemaphore.WaitAsync(reason.CancellationToken)
261266 }
262267
263268 _confirmsTaskCompletionSources . Clear ( ) ;
269+ MaybeUnblockPublishers ( ) ;
264270 }
265271 }
266272 finally
@@ -285,6 +291,7 @@ await _confirmSemaphore.WaitAsync(cancellationToken)
285291 {
286292 publisherConfirmationTcs = new TaskCompletionSource < bool > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
287293 _confirmsTaskCompletionSources [ publishSequenceNumber ] = publisherConfirmationTcs ;
294+ MaybeBlockPublishers ( ) ;
288295 }
289296
290297 _nextPublishSeqNo ++ ;
@@ -311,6 +318,7 @@ private bool MaybeHandleExceptionWithEnabledPublisherConfirmations(PublisherConf
311318 if ( _publisherConfirmationTrackingEnabled )
312319 {
313320 _confirmsTaskCompletionSources . TryRemove ( publisherConfirmationInfo . PublishSequenceNumber , out _ ) ;
321+ MaybeUnblockPublishers ( ) ;
314322 }
315323
316324 exceptionWasHandled = publisherConfirmationInfo . MaybeHandleException ( ex ) ;
@@ -334,5 +342,49 @@ await publisherConfirmationInfo.MaybeWaitForConfirmationAsync(cancellationToken)
334342 }
335343 }
336344 }
345+
346+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
347+ private ValueTask MaybeEnforceOutstandingPublisherConfirmationsAsync ( CancellationToken cancellationToken )
348+ {
349+ if ( _publisherConfirmationTrackingEnabled )
350+ {
351+ if ( _maxOutstandingPublisherConfirmsReached . IsSet )
352+ {
353+ return default ;
354+ }
355+ else
356+ {
357+ return _maxOutstandingPublisherConfirmsReached . WaitAsync ( cancellationToken ) ;
358+ }
359+ }
360+
361+ return default ;
362+ }
363+
364+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
365+ private void MaybeBlockPublishers ( )
366+ {
367+ if ( _publisherConfirmationTrackingEnabled )
368+ {
369+ if ( _maxOutstandingPublisherConfirmations is not null
370+ && _confirmsTaskCompletionSources . Count >= _maxOutstandingPublisherConfirmations )
371+ {
372+ _maxOutstandingPublisherConfirmsReached . Reset ( ) ;
373+ }
374+ }
375+ }
376+
377+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
378+ private void MaybeUnblockPublishers ( )
379+ {
380+ if ( _publisherConfirmationTrackingEnabled )
381+ {
382+ if ( _maxOutstandingPublisherConfirmations is not null
383+ && _confirmsTaskCompletionSources . Count < _maxOutstandingPublisherConfirmations )
384+ {
385+ _maxOutstandingPublisherConfirmsReached . Set ( ) ;
386+ }
387+ }
388+ }
337389 }
338390}
0 commit comments