-
Notifications
You must be signed in to change notification settings - Fork 36
/
RetryErrorPolicy.cs
162 lines (140 loc) · 5.95 KB
/
RetryErrorPolicy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) 2020 Sergio Aquilini
// This code is licensed under MIT license (see LICENSE file for details)
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Silverback.Diagnostics;
using Silverback.Messaging.Broker.Behaviors;
using Silverback.Messaging.Messages;
using Silverback.Util;
namespace Silverback.Messaging.Inbound.ErrorHandling
{
/// <summary>
/// This policy retries to process the message that previously failed to be to processed. An optional
/// delay can be specified.
/// </summary>
/// TODO: Exponential backoff variant
public class RetryErrorPolicy : RetryableErrorPolicyBase
{
/// <summary>
/// Initializes a new instance of the <see cref="RetryErrorPolicy" /> class.
/// </summary>
/// <param name="initialDelay">
/// The optional delay to be applied to the first retry.
/// </param>
/// <param name="delayIncrement">
/// The optional increment to the delay to be applied at each retry.
/// </param>
public RetryErrorPolicy(TimeSpan? initialDelay = null, TimeSpan? delayIncrement = null)
{
InitialDelay = initialDelay ?? TimeSpan.Zero;
DelayIncrement = delayIncrement ?? TimeSpan.Zero;
if (InitialDelay < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(
nameof(initialDelay),
initialDelay,
"The specified initial delay must be greater than TimeSpan.Zero.");
}
if (DelayIncrement < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(
nameof(delayIncrement),
delayIncrement,
"The specified delay increment must be greater than TimeSpan.Zero.");
}
}
internal TimeSpan InitialDelay { get; }
internal TimeSpan DelayIncrement { get; }
/// <inheritdoc cref="ErrorPolicyBase.BuildCore" />
protected override ErrorPolicyImplementation BuildCore(IServiceProvider serviceProvider) =>
new RetryErrorPolicyImplementation(
InitialDelay,
DelayIncrement,
MaxFailedAttemptsCount,
ExcludedExceptions,
IncludedExceptions,
ApplyRule,
MessageToPublishFactory,
serviceProvider,
serviceProvider.GetRequiredService<IInboundLogger<RetryErrorPolicy>>());
private sealed class RetryErrorPolicyImplementation : ErrorPolicyImplementation
{
private readonly TimeSpan _initialDelay;
private readonly TimeSpan _delayIncrement;
private readonly IInboundLogger<RetryErrorPolicy> _logger;
public RetryErrorPolicyImplementation(
TimeSpan initialDelay,
TimeSpan delayIncrement,
int? maxFailedAttempts,
ICollection<Type> excludedExceptions,
ICollection<Type> includedExceptions,
Func<IRawInboundEnvelope, Exception, bool>? applyRule,
Func<IRawInboundEnvelope, Exception, object?>? messageToPublishFactory,
IServiceProvider serviceProvider,
IInboundLogger<RetryErrorPolicy> logger)
: base(
maxFailedAttempts,
excludedExceptions,
includedExceptions,
applyRule,
messageToPublishFactory,
serviceProvider,
logger)
{
_initialDelay = initialDelay;
_delayIncrement = delayIncrement;
_logger = logger;
}
protected override async Task<bool> ApplyPolicyAsync(
ConsumerPipelineContext context,
Exception exception)
{
Check.NotNull(context, nameof(context));
Check.NotNull(exception, nameof(exception));
if (!await TryRollbackAsync(context, exception).ConfigureAwait(false))
{
await context.Consumer.TriggerReconnectAsync().ConfigureAwait(false);
return true;
}
await ApplyDelayAsync(context).ConfigureAwait(false);
_logger.LogRetryProcessing(context.Envelope);
return true;
}
[SuppressMessage("", "CA1031", Justification = Justifications.ExceptionLogged)]
private async Task<bool> TryRollbackAsync(ConsumerPipelineContext context, Exception exception)
{
try
{
await context.TransactionManager.RollbackAsync(exception, stopConsuming: false)
.ConfigureAwait(false);
return true;
}
catch (Exception ex)
{
_logger.LogRollbackToRetryFailed(context.Envelope, ex);
return false;
}
}
private async Task ApplyDelayAsync(ConsumerPipelineContext context)
{
var delay = (int)_initialDelay.TotalMilliseconds +
(context.Envelope.Headers.GetValueOrDefault<int>(
DefaultMessageHeaders.FailedAttempts) *
(int)_delayIncrement.TotalMilliseconds);
if (delay <= 0)
return;
_logger.LogInboundTrace(
IntegrationLogEvents.RetryDelayed,
context.Envelope,
() => new object?[]
{
delay
});
await Task.Delay(delay).ConfigureAwait(false);
}
}
}
}