@@ -141,6 +141,7 @@ private async Task MaybeConfirmSelect(CancellationToken cancellationToken)
141
141
if ( _publisherConfirmationTrackingEnabled )
142
142
{
143
143
_confirmsTaskCompletionSources . Clear ( ) ;
144
+ MaybeUnblockPublishers ( ) ;
144
145
}
145
146
_nextPublishSeqNo = 1 ;
146
147
}
@@ -186,13 +187,15 @@ private void HandleAck(ulong deliveryTag, bool multiple)
186
187
{
187
188
pair . Value . SetResult ( true ) ;
188
189
_confirmsTaskCompletionSources . Remove ( pair . Key , out _ ) ;
190
+ MaybeUnblockPublishers ( ) ;
189
191
}
190
192
}
191
193
}
192
194
else
193
195
{
194
196
if ( _confirmsTaskCompletionSources . TryRemove ( deliveryTag , out TaskCompletionSource < bool > ? tcs ) )
195
197
{
198
+ MaybeUnblockPublishers ( ) ;
196
199
tcs . SetResult ( true ) ;
197
200
}
198
201
}
@@ -212,13 +215,15 @@ private void HandleNack(ulong deliveryTag, bool multiple, bool isReturn)
212
215
{
213
216
pair . Value . SetException ( new PublishException ( pair . Key , isReturn ) ) ;
214
217
_confirmsTaskCompletionSources . Remove ( pair . Key , out _ ) ;
218
+ MaybeUnblockPublishers ( ) ;
215
219
}
216
220
}
217
221
}
218
222
else
219
223
{
220
224
if ( _confirmsTaskCompletionSources . Remove ( deliveryTag , out TaskCompletionSource < bool > ? tcs ) )
221
225
{
226
+ MaybeUnblockPublishers ( ) ;
222
227
tcs . SetException ( new PublishException ( deliveryTag , isReturn ) ) ;
223
228
}
224
229
}
@@ -261,6 +266,7 @@ await _confirmSemaphore.WaitAsync(reason.CancellationToken)
261
266
}
262
267
263
268
_confirmsTaskCompletionSources . Clear ( ) ;
269
+ MaybeUnblockPublishers ( ) ;
264
270
}
265
271
}
266
272
finally
@@ -285,6 +291,7 @@ await _confirmSemaphore.WaitAsync(cancellationToken)
285
291
{
286
292
publisherConfirmationTcs = new TaskCompletionSource < bool > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
287
293
_confirmsTaskCompletionSources [ publishSequenceNumber ] = publisherConfirmationTcs ;
294
+ MaybeBlockPublishers ( ) ;
288
295
}
289
296
290
297
_nextPublishSeqNo ++ ;
@@ -311,6 +318,7 @@ private bool MaybeHandleExceptionWithEnabledPublisherConfirmations(PublisherConf
311
318
if ( _publisherConfirmationTrackingEnabled )
312
319
{
313
320
_confirmsTaskCompletionSources . TryRemove ( publisherConfirmationInfo . PublishSequenceNumber , out _ ) ;
321
+ MaybeUnblockPublishers ( ) ;
314
322
}
315
323
316
324
exceptionWasHandled = publisherConfirmationInfo . MaybeHandleException ( ex ) ;
@@ -334,5 +342,49 @@ await publisherConfirmationInfo.MaybeWaitForConfirmationAsync(cancellationToken)
334
342
}
335
343
}
336
344
}
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
+ }
337
389
}
338
390
}
0 commit comments