Skip to content

Commit a593fee

Browse files
Ability to cancel find operations (#48)
* Ability to cancel operations * Break
1 parent 605c6a4 commit a593fee

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/NServiceBus.Storage.MongoDB/SynchronizedStorage/StorageSession.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,31 @@ public async Task<BsonDocument> Find<T>(FilterDefinition<BsonDocument> filter)
6565

6666
using (var cancellationTokenSource = new CancellationTokenSource(transactionTimeout))
6767
{
68-
while (!cancellationTokenSource.IsCancellationRequested)
68+
var token = cancellationTokenSource.Token;
69+
while (!token.IsCancellationRequested)
6970
{
7071
try
7172
{
72-
var result = await sagaCollection.FindOneAndUpdateAsync(MongoSession, filter, update, FindOneAndUpdateOptions, CancellationToken.None)
73+
var result = await sagaCollection.FindOneAndUpdateAsync(MongoSession, filter, update, FindOneAndUpdateOptions, token)
7374
.ConfigureAwait(false);
7475
return result;
7576
}
77+
catch (OperationCanceledException)
78+
{
79+
break;
80+
}
7681
catch (MongoCommandException e) when (WriteConflictUnderTransaction(e))
7782
{
7883
await AbortTransaction().ConfigureAwait(false);
7984

80-
await Task.Delay(TimeSpan.FromMilliseconds(random.Next(5, 20)), CancellationToken.None).ConfigureAwait(false);
85+
try
86+
{
87+
await Task.Delay(TimeSpan.FromMilliseconds(random.Next(5, 20)), token).ConfigureAwait(false);
88+
}
89+
catch (OperationCanceledException)
90+
{
91+
break;
92+
}
8193

8294
StartTransaction();
8395
}

0 commit comments

Comments
 (0)