@@ -166,6 +166,7 @@ public IBasicConsumer DefaultConsumer
166
166
167
167
public bool IsOpen => CloseReason is null ;
168
168
169
+ // TODO add private bool for Confirm mode
169
170
public ulong NextPublishSeqNo { get ; private set ; }
170
171
171
172
public string CurrentQueue { get ; private set ; }
@@ -1239,8 +1240,24 @@ public void BasicPublish<TProperties>(string exchange, string routingKey, in TPr
1239
1240
}
1240
1241
}
1241
1242
1242
- var cmd = new BasicPublish ( exchange , routingKey , mandatory , default ) ;
1243
- ChannelSend ( in cmd , in basicProperties , body ) ;
1243
+ try
1244
+ {
1245
+ var cmd = new BasicPublish ( exchange , routingKey , mandatory , default ) ;
1246
+ ChannelSend ( in cmd , in basicProperties , body ) ;
1247
+ }
1248
+ catch
1249
+ {
1250
+ if ( NextPublishSeqNo > 0 )
1251
+ {
1252
+ lock ( _confirmLock )
1253
+ {
1254
+ NextPublishSeqNo -- ;
1255
+ _pendingDeliveryTags . RemoveLast ( ) ;
1256
+ }
1257
+ }
1258
+
1259
+ throw ;
1260
+ }
1244
1261
}
1245
1262
1246
1263
public void BasicPublish < TProperties > ( CachedString exchange , CachedString routingKey , in TProperties basicProperties , ReadOnlyMemory < byte > body , bool mandatory )
@@ -1254,8 +1271,24 @@ public void BasicPublish<TProperties>(CachedString exchange, CachedString routin
1254
1271
}
1255
1272
}
1256
1273
1257
- var cmd = new BasicPublishMemory ( exchange . Bytes , routingKey . Bytes , mandatory , default ) ;
1258
- ChannelSend ( in cmd , in basicProperties , body ) ;
1274
+ try
1275
+ {
1276
+ var cmd = new BasicPublishMemory ( exchange . Bytes , routingKey . Bytes , mandatory , default ) ;
1277
+ ChannelSend ( in cmd , in basicProperties , body ) ;
1278
+ }
1279
+ catch
1280
+ {
1281
+ if ( NextPublishSeqNo > 0 )
1282
+ {
1283
+ lock ( _confirmLock )
1284
+ {
1285
+ NextPublishSeqNo -- ;
1286
+ _pendingDeliveryTags . RemoveLast ( ) ;
1287
+ }
1288
+ }
1289
+
1290
+ throw ;
1291
+ }
1259
1292
}
1260
1293
1261
1294
public ValueTask BasicPublishAsync < TProperties > ( string exchange , string routingKey , in TProperties basicProperties , ReadOnlyMemory < byte > body , bool mandatory )
@@ -1269,8 +1302,24 @@ public ValueTask BasicPublishAsync<TProperties>(string exchange, string routingK
1269
1302
}
1270
1303
}
1271
1304
1272
- var cmd = new BasicPublish ( exchange , routingKey , mandatory , default ) ;
1273
- return ModelSendAsync ( in cmd , in basicProperties , body ) ;
1305
+ try
1306
+ {
1307
+ var cmd = new BasicPublish ( exchange , routingKey , mandatory , default ) ;
1308
+ return ModelSendAsync ( in cmd , in basicProperties , body ) ;
1309
+ }
1310
+ catch
1311
+ {
1312
+ if ( NextPublishSeqNo > 0 )
1313
+ {
1314
+ lock ( _confirmLock )
1315
+ {
1316
+ NextPublishSeqNo -- ;
1317
+ _pendingDeliveryTags . RemoveLast ( ) ;
1318
+ }
1319
+ }
1320
+
1321
+ throw ;
1322
+ }
1274
1323
}
1275
1324
1276
1325
public ValueTask BasicPublishAsync < TProperties > ( CachedString exchange , CachedString routingKey , in TProperties basicProperties , ReadOnlyMemory < byte > body , bool mandatory )
@@ -1284,8 +1333,24 @@ public ValueTask BasicPublishAsync<TProperties>(CachedString exchange, CachedStr
1284
1333
}
1285
1334
}
1286
1335
1287
- var cmd = new BasicPublishMemory ( exchange . Bytes , routingKey . Bytes , mandatory , default ) ;
1288
- return ModelSendAsync ( in cmd , in basicProperties , body ) ;
1336
+ try
1337
+ {
1338
+ var cmd = new BasicPublishMemory ( exchange . Bytes , routingKey . Bytes , mandatory , default ) ;
1339
+ return ModelSendAsync ( in cmd , in basicProperties , body ) ;
1340
+ }
1341
+ catch
1342
+ {
1343
+ if ( NextPublishSeqNo > 0 )
1344
+ {
1345
+ lock ( _confirmLock )
1346
+ {
1347
+ NextPublishSeqNo -- ;
1348
+ _pendingDeliveryTags . RemoveLast ( ) ;
1349
+ }
1350
+ }
1351
+
1352
+ throw ;
1353
+ }
1289
1354
}
1290
1355
1291
1356
public void UpdateSecret ( string newSecret , string reason )
@@ -1755,6 +1820,11 @@ await ModelSendAsync(method)
1755
1820
1756
1821
private List < TaskCompletionSource < bool > > _confirmsTaskCompletionSources ;
1757
1822
1823
+ public bool WaitForConfirms ( )
1824
+ {
1825
+ return WaitForConfirmsAsync ( ) . EnsureCompleted ( ) ;
1826
+ }
1827
+
1758
1828
public Task < bool > WaitForConfirmsAsync ( CancellationToken token = default )
1759
1829
{
1760
1830
if ( NextPublishSeqNo == 0UL )
@@ -1812,6 +1882,11 @@ await tokenRegistration.DisposeAsync()
1812
1882
}
1813
1883
}
1814
1884
1885
+ public void WaitForConfirmsOrDie ( )
1886
+ {
1887
+ WaitForConfirmsOrDieAsync ( ) . EnsureCompleted ( ) ;
1888
+ }
1889
+
1815
1890
public async Task WaitForConfirmsOrDieAsync ( CancellationToken token = default )
1816
1891
{
1817
1892
try
0 commit comments