@@ -101,7 +101,7 @@ await base.OpenAsync()
101
101
/// <exception cref="InvalidOperationException"></exception>
102
102
/// <exception cref="NotSupportedException"></exception>
103
103
/// <exception cref="PublisherException"></exception>
104
- public async Task < PublishResult > PublishAsync ( IMessage message , CancellationToken cancellationToken = default )
104
+ public Task < PublishResult > PublishAsync ( IMessage message , CancellationToken cancellationToken = default )
105
105
{
106
106
ThrowIfClosed ( ) ;
107
107
@@ -119,17 +119,17 @@ public async Task<PublishResult> PublishAsync(IMessage message, CancellationToke
119
119
stopwatch . Start ( ) ;
120
120
}
121
121
122
+ TaskCompletionSource < PublishResult > publishResultTcs =
123
+ Utils . CreateTaskCompletionSource < PublishResult > ( ) ;
124
+
122
125
try
123
126
{
124
- TaskCompletionSource < PublishOutcome > messagePublishedTcs =
125
- Utils . CreateTaskCompletionSource < PublishOutcome > ( ) ;
126
-
127
127
Message nativeMessage = ( ( AmqpMessage ) message ) . NativeMessage ;
128
128
129
129
void OutcomeCallback ( ILink sender , Message inMessage , Outcome outcome , object state )
130
130
{
131
131
// Note: sometimes `message` is null 🤔
132
- System . Diagnostics . Debug . Assert ( Object . ReferenceEquals ( this , state ) ) ;
132
+ Debug . Assert ( Object . ReferenceEquals ( this , state ) ) ;
133
133
134
134
if ( false == Object . ReferenceEquals ( _senderLink , sender ) )
135
135
{
@@ -167,7 +167,15 @@ void OutcomeCallback(ILink sender, Message inMessage, Outcome outcome, object st
167
167
}
168
168
}
169
169
170
- messagePublishedTcs . SetResult ( publishOutcome ) ;
170
+ // TODO cancellation token
171
+ if ( _metricsReporter is not null && stopwatch is not null )
172
+ {
173
+ stopwatch . Stop ( ) ;
174
+ _metricsReporter . Published ( stopwatch . Elapsed ) ;
175
+ }
176
+
177
+ var publishResult = new PublishResult ( message , publishOutcome ) ;
178
+ publishResultTcs . SetResult ( publishResult ) ;
171
179
}
172
180
173
181
/*
@@ -176,25 +184,16 @@ void OutcomeCallback(ILink sender, Message inMessage, Outcome outcome, object st
176
184
*/
177
185
_senderLink . Send ( nativeMessage , OutcomeCallback , this ) ;
178
186
179
- // TODO cancellation token
180
- // PublishOutcome publishOutcome = await messagePublishedTcs.Task.WaitAsync(TimeSpan.FromSeconds(5), cancellationToken)
181
- PublishOutcome publishOutcome = await messagePublishedTcs . Task . WaitAsync ( TimeSpan . FromSeconds ( 5 ) )
182
- . ConfigureAwait ( false ) ;
183
-
184
- if ( _metricsReporter is not null && stopwatch is not null )
185
- {
186
- stopwatch . Stop ( ) ;
187
- _metricsReporter . Published ( stopwatch . Elapsed ) ;
188
- }
189
-
190
- return new PublishResult ( message , publishOutcome ) ;
187
+ return publishResultTcs . Task ;
191
188
}
192
189
catch ( AmqpException ex )
193
190
{
194
191
stopwatch ? . Stop ( ) ;
195
192
_metricsReporter ? . PublishDisposition ( IMetricsReporter . PublishDispositionValue . REJECTED ) ;
196
193
var publishOutcome = new PublishOutcome ( OutcomeState . Rejected , Utils . ConvertError ( ex . Error ) ) ;
197
- return new PublishResult ( message , publishOutcome ) ;
194
+ var publishResult = new PublishResult ( message , publishOutcome ) ;
195
+ publishResultTcs . SetResult ( publishResult ) ;
196
+ return publishResultTcs . Task ;
198
197
}
199
198
catch ( Exception e )
200
199
{
0 commit comments