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

Improved logic for ObserveLinesWithRetry #159

Merged
merged 1 commit into from
Jul 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/KubeClient/ResourceClients/KubeResourceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reactive;
using System.Reactive.Linq;
using System.Text;
using System.Threading;
Expand All @@ -21,7 +22,6 @@ namespace KubeClient.ResourceClients
using KubeClient.Models.ContractResolvers;
using Models;
using Models.Converters;
using System.Reactive;

/// <summary>
/// The base class for Kubernetes resource API clients.
Expand Down Expand Up @@ -747,27 +747,29 @@ protected IObservable<string> ObserveLinesWithRetry(string operationDescription,
HttpRequest currentRequest = requestFactory();

return ObserveLines(requestFactory, operationDescription, bufferSize)
.RetryWhen(exceptions => Observable.Create((IObserver<Unit> retrySignal, CancellationToken subscriptionCancellation) =>
.RetryWhen(exceptions => Observable.Create((IObserver<Unit> retrySignal) =>
{
exceptions.Subscribe(
onNext: exception =>
return exceptions.Subscribe(
onNext: (Exception sourceError) =>
{
if (!subscriptionCancellation.IsCancellationRequested && shouldRetry(exception))
if (shouldRetry(sourceError))
retrySignal.OnNext(Unit.Default); // Retry (this will seamlessly continue the sequence).
else
retrySignal.OnError(exception); // Bubble up (this will terminate the sequence with an error).
retrySignal.OnError(sourceError); // Bubble up (this will terminate the sequence with an error).
},
onError: (Exception exceptionSourceError) =>
{
// Under normal circumstances this should not be called, and so we never retry from here.
retrySignal.OnError(exceptionSourceError); // Bubble up (this will terminate the sequence with an error).
},
onCompleted: () =>
{
if (!subscriptionCancellation.IsCancellationRequested && shouldRetry(null))
if (shouldRetry(null))
retrySignal.OnNext(Unit.Default); // Retry (this will seamlessly continue the sequence).
else
retrySignal.OnCompleted(); // Bubble up (this will terminate the sequence).
},
subscriptionCancellation // Automatically propagate termination of subscription.
}
);

return Task.CompletedTask;
}));
}

Expand Down