Skip to content

Commit

Permalink
fix: Dispose cloned request messages in ExponentialRetryPolicy
Browse files Browse the repository at this point in the history
  • Loading branch information
nozzlegear committed May 16, 2024
1 parent 43b9d1f commit 922a9b7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ public async Task Run_ShouldReturnResult()
result.Result.Should().Be(expectedValue);
}

[Fact]
public async Task Run_ShouldDisposeClonedRequestMessages()
{
_cloneableRequestMessage.When(x => x.Dispose())
.Throw<TestException>();

var policy = SetupPolicy();
var act = () => policy.Run(_cloneableRequestMessage, _executeRequest, CancellationToken.None);

await act.Should().ThrowAsync<TestException>();
_cloneableRequestMessage.Received(1).Dispose();
}

[Fact]
public async Task Run_ShouldThrowWhenRequestIsNotRetriableAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ ITaskScheduler taskScheduler
}

public async Task<RequestResult<T>> Run<T>(
CloneableRequestMessage requestMessage,
CloneableRequestMessage baseRequestMessage,
ExecuteRequestAsync<T> executeRequestAsync,
CancellationToken cancellationToken,
int? graphqlQueryCost = null)
{
var currentTry = 1;
var useMaximumDelayBetweenRetries = false;
var clonedRequestMessage = requestMessage;
var combinedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

if (_options.MaximumDelayBeforeRequestCancellation is not null)
Expand All @@ -53,6 +52,8 @@ public async Task<RequestResult<T>> Run<T>(
{
combinedCancellationToken.Token.ThrowIfCancellationRequested();

using var clonedRequestMessage = await baseRequestMessage.CloneAsync();

try
{
var value = await executeRequestAsync.Invoke(clonedRequestMessage);
Expand Down Expand Up @@ -101,7 +102,6 @@ public async Task<RequestResult<T>> Run<T>(

// Delay and then try again
await _taskScheduler.DelayAsync(nextDelay, combinedCancellationToken.Token);
clonedRequestMessage = await requestMessage.CloneAsync();
}
}
}

0 comments on commit 922a9b7

Please sign in to comment.