-
-
Notifications
You must be signed in to change notification settings - Fork 302
Add sync way to retry in InlineSendingAgent #1614
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ namespace Wolverine.Transports.Sending; | |
| public class InlineSendingAgent : ISendingAgent, IDisposable | ||
| { | ||
| private readonly IMessageTracker _messageLogger; | ||
| private readonly RetryBlock<Envelope> _sending; | ||
| private readonly IRetryBlock<Envelope> _sending; | ||
| private readonly DurabilitySettings _settings; | ||
|
|
||
| public InlineSendingAgent(ILogger logger, ISender sender, Endpoint endpoint, IMessageTracker messageLogger, | ||
|
|
@@ -20,23 +20,26 @@ public InlineSendingAgent(ILogger logger, ISender sender, Endpoint endpoint, IMe | |
| _settings = settings; | ||
| Endpoint = endpoint; | ||
|
|
||
| if (endpoint.TelemetryEnabled) | ||
| if (settings.UseSyncRetryBlock) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use optional property to switch to different behavior so no breaking change is added |
||
| { | ||
| _sending = new RetryBlock<Envelope>(sendWithTracing, logger, _settings.Cancellation); | ||
| _sending = new RetryBlockSync<Envelope>(RetryHandlerResolver(endpoint), logger, _settings.Cancellation); | ||
| } | ||
| else | ||
| { | ||
| _sending = new RetryBlock<Envelope>(sendWithOutTracing, logger, _settings.Cancellation); | ||
| _sending = new RetryBlock<Envelope>(RetryHandlerResolver(endpoint), logger, _settings.Cancellation); | ||
| } | ||
| } | ||
|
|
||
| private Func<Envelope, CancellationToken, Task> RetryHandlerResolver(Endpoint endpoint) => endpoint.TelemetryEnabled ? sendWithTracing : sendWithOutTracing; | ||
|
|
||
| public ISender Sender { get; } | ||
|
|
||
| private async Task sendWithTracing(Envelope e, CancellationToken cancellationToken) | ||
| { | ||
| using var activity = WolverineTracing.StartSending(e); | ||
| try | ||
| { | ||
| //TODO: What about cancellationToken?? | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cancellationToken is not passed to Sender.SendAsync - I think it should be
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't make sense before when it was truly asynchronous |
||
| await Sender.SendAsync(e); | ||
| _messageLogger.Sent(e); | ||
| } | ||
|
|
@@ -58,7 +61,7 @@ private async Task sendWithOutTracing(Envelope e, CancellationToken cancellation | |
|
|
||
| public void Dispose() | ||
| { | ||
| _sending.Dispose(); | ||
| (_sending as IDisposable)?.Dispose(); | ||
| } | ||
|
|
||
| public Uri Destination => Sender.Destination; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| namespace Wolverine.Util.Dataflow; | ||
|
|
||
| /// <summary> | ||
| /// Abstract a way we can retry on <typeparamref name="T"/> message | ||
| /// </summary> | ||
| /// <typeparam name="T"></typeparam> | ||
| internal interface IRetryBlock<T> | ||
| { | ||
| /// <summary> | ||
| /// Send <typeparamref name="T"/> message in async manner | ||
| /// </summary> | ||
| /// <param name="message"></param> | ||
| /// <returns></returns> | ||
| Task PostAsync(T message); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| using JasperFx.Core; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Wolverine.Util.Dataflow; | ||
|
|
||
| /// <summary> | ||
| /// Synchrous version of RetryBlock - this version is retrying on the same thread context so execution os sending message is waiting for information that message was sent to the broker or there was a problem | ||
| /// </summary> | ||
| /// <typeparam name="T"></typeparam> | ||
| public class RetryBlockSync<T>(Func<T, CancellationToken, Task> handler, ILogger logger, CancellationToken cancellationToken) : IRetryBlock<T> | ||
| { | ||
| private readonly CancellationToken _cancellationToken = cancellationToken; | ||
| private readonly Func<T, CancellationToken, Task> _handler = handler; | ||
| private readonly ILogger _logger = logger; | ||
| public TimeSpan[] Pauses { get; set; } = [0.Milliseconds(), 50.Milliseconds(), 100.Milliseconds(), 250.Milliseconds()]; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe move this Pauses to DurabilitySettings so retry logic could be configurable? |
||
|
|
||
| public async Task PostAsync(T message) | ||
| { | ||
| if (_cancellationToken.IsCancellationRequested) return; | ||
|
|
||
| for (var attempt = 1; attempt <= Pauses.Length; attempt++) | ||
| { | ||
| var delay = Pauses[attempt-1]; | ||
| try | ||
| { | ||
| if (attempt > 1 && delay.TotalMilliseconds > 0) | ||
| { | ||
| await Task.Delay(delay, _cancellationToken).ConfigureAwait(false); | ||
| } | ||
|
|
||
| await _handler(message, _cancellationToken).ConfigureAwait(false); | ||
| _logger.LogDebug("Completed {Item}", message); | ||
| } | ||
| catch (OperationCanceledException) | ||
| { | ||
| _logger.LogWarning("Operation canceled for message {Message} on attempt {Attempts}", message, attempt); | ||
| throw; | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| if (attempt < Pauses.Length) | ||
| { | ||
| _logger.LogInformation(e, "Retrying message {Message} after {Attempts} attempts", message, attempt); | ||
| } | ||
| else | ||
| { | ||
| _logger.LogError(e, "Error proccessing message {Message} on attempt {Attempts}", message, attempt); | ||
| throw; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Option to drive sending agent