Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify cts in polling loop #459

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 5 additions & 7 deletions source/Halibut/Transport/PollingClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class PollingClient : IPollingClient

readonly Func<RetryPolicy> createRetryPolicy;
readonly AsyncHalibutFeature asyncHalibutFeature;
RequestCancellationTokens? requestCancellationTokens;

public PollingClient(Uri subscription, ISecureClient secureClient, Func<RequestMessage, ResponseMessage> handleIncomingRequest, ILog log, CancellationToken cancellationToken, Func<RetryPolicy> createRetryPolicy, AsyncHalibutFeature asyncHalibutFeature)
{
Expand Down Expand Up @@ -64,8 +63,7 @@ public void Start()
}
else
{
requestCancellationTokens = new RequestCancellationTokens(workingCancellationTokenSource.Token, workingCancellationTokenSource.Token);
pollingClientLoopTask = Task.Run(async () => await ExecutePollingLoopAsyncCatchingExceptions(requestCancellationTokens));
pollingClientLoopTask = Task.Run(async () => await ExecutePollingLoopAsyncCatchingExceptions(workingCancellationTokenSource.Token));
}
}

Expand All @@ -74,7 +72,6 @@ public void Dispose()
working = false;
Try.CatchingError(workingCancellationTokenSource.Cancel, _ => { });
Try.CatchingError(workingCancellationTokenSource.Dispose, _ => { });
Try.CatchingError(() => requestCancellationTokens?.Dispose(), _ => { });
}

void ExecutePollingLoop(object ignored)
Expand Down Expand Up @@ -123,20 +120,21 @@ void ExecutePollingLoop(object ignored)
/// Runs ExecutePollingLoopAsync but catches any exception that falls out of it, log here
/// rather than let it be unobserved. We are not expecting an exception but just in case.
/// </summary>
async Task ExecutePollingLoopAsyncCatchingExceptions(RequestCancellationTokens requestCancellationTokens)
async Task ExecutePollingLoopAsyncCatchingExceptions(CancellationToken cancellationToken)
{
try
{
await ExecutePollingLoopAsync(requestCancellationTokens);
await ExecutePollingLoopAsync(cancellationToken);
}
catch (Exception e)
{
log.Write(EventType.Diagnostic, $"PollingClient stopped with an exception: {e}");
}
}

async Task ExecutePollingLoopAsync(RequestCancellationTokens requestCancellationTokens)
async Task ExecutePollingLoopAsync(CancellationToken cancellationToken)
{
using var requestCancellationTokens = new RequestCancellationTokens(cancellationToken, cancellationToken);
var retry = createRetryPolicy();
var sleepFor = TimeSpan.Zero;
while (!cancellationToken.IsCancellationRequested)
Expand Down