@@ -1004,26 +1004,13 @@ public async ValueTask BasicPublishAsync<TProperties>(CachedString exchange, Cac
10041004 CancellationToken cancellationToken = default )
10051005 where TProperties : IReadOnlyBasicProperties , IAmqpHeader
10061006 {
1007- TaskCompletionSource < bool > ? publisherConfirmationTcs = null ;
1008- ulong publishSequenceNumber = 0 ;
1007+ PublisherConfirmationInfo ? publisherConfirmationInfo = null ;
10091008 try
10101009 {
1011- if ( _publisherConfirmationsEnabled )
1012- {
1013- await _confirmSemaphore . WaitAsync ( cancellationToken )
1010+ publisherConfirmationInfo =
1011+ await MaybeStartPublisherConfirmationTracking ( cancellationToken )
10141012 . ConfigureAwait ( false ) ;
10151013
1016- publishSequenceNumber = _nextPublishSeqNo ;
1017-
1018- if ( _publisherConfirmationTrackingEnabled )
1019- {
1020- publisherConfirmationTcs = new ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
1021- _confirmsTaskCompletionSources [ publishSequenceNumber ] = publisherConfirmationTcs ;
1022- }
1023-
1024- _nextPublishSeqNo ++ ;
1025- }
1026-
10271014 await EnforceFlowControlAsync ( cancellationToken )
10281015 . ConfigureAwait ( false ) ;
10291016
@@ -1033,6 +1020,12 @@ await EnforceFlowControlAsync(cancellationToken)
10331020 ? RabbitMQActivitySource . Send ( routingKey . Value , exchange . Value , body . Length )
10341021 : default ;
10351022
1023+ ulong publishSequenceNumber = 0 ;
1024+ if ( publisherConfirmationInfo is not null )
1025+ {
1026+ publishSequenceNumber = publisherConfirmationInfo . PublishSequenceNumber ;
1027+ }
1028+
10361029 BasicProperties ? props = PopulateBasicPropertiesHeaders ( basicProperties , sendActivity , publishSequenceNumber ) ;
10371030 if ( props is null )
10381031 {
@@ -1047,35 +1040,16 @@ await ModelSendAsync(in cmd, in props, body, cancellationToken)
10471040 }
10481041 catch ( Exception ex )
10491042 {
1050- if ( _publisherConfirmationsEnabled )
1051- {
1052- _nextPublishSeqNo -- ;
1053- if ( _publisherConfirmationTrackingEnabled )
1054- {
1055- _confirmsTaskCompletionSources . TryRemove ( publishSequenceNumber , out _ ) ;
1056- }
1057- }
1058-
1059- if ( publisherConfirmationTcs is not null )
1060- {
1061- publisherConfirmationTcs . SetException ( ex ) ;
1062- }
1063- else
1043+ bool exceptionWasHandled =
1044+ MaybeHandleExceptionWithEnabledPublisherConfirmations ( publisherConfirmationInfo , ex ) ;
1045+ if ( ! exceptionWasHandled )
10641046 {
10651047 throw ;
10661048 }
10671049 }
10681050 finally
10691051 {
1070- if ( _publisherConfirmationsEnabled )
1071- {
1072- _confirmSemaphore . Release ( ) ;
1073- }
1074- }
1075-
1076- if ( publisherConfirmationTcs is not null )
1077- {
1078- await publisherConfirmationTcs . Task . WaitAsync ( cancellationToken )
1052+ await MaybeEndPublisherConfirmationTracking ( publisherConfirmationInfo , cancellationToken )
10791053 . ConfigureAwait ( false ) ;
10801054 }
10811055 }
0 commit comments