-
Notifications
You must be signed in to change notification settings - Fork 324
/
WorkflowRetryPolicy.cs
104 lines (96 loc) · 4.78 KB
/
WorkflowRetryPolicy.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
// ------------------------------------------------------------------------
// Copyright 2023 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
using System;
using System.Threading;
using Microsoft.DurableTask;
namespace Dapr.Workflow
{
/// <summary>
/// A declarative retry policy that can be configured for activity or child workflow calls.
/// </summary>
public class WorkflowRetryPolicy
{
readonly RetryPolicy durableRetryPolicy;
/// <summary>
/// Initializes a new instance of the <see cref="WorkflowRetryPolicy"/> class.
/// </summary>
/// <param name="maxNumberOfAttempts">The maximum number of task invocation attempts. Must be 1 or greater.</param>
/// <param name="firstRetryInterval">The amount of time to delay between the first and second attempt.</param>
/// <param name="backoffCoefficient">
/// The exponential back-off coefficient used to determine the delay between subsequent retries. Must be 1.0 or greater.
/// </param>
/// <param name="maxRetryInterval">
/// The maximum time to delay between attempts, regardless of<paramref name="backoffCoefficient"/>.
/// </param>
/// <param name="retryTimeout">The overall timeout for retries.</param>
/// <remarks>
/// The value <see cref="Timeout.InfiniteTimeSpan"/> can be used to specify an unlimited timeout for
/// <paramref name="maxRetryInterval"/> or <paramref name="retryTimeout"/>.
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown if any of the following are true:
/// <list type="bullet">
/// <item>The value for <paramref name="maxNumberOfAttempts"/> is less than or equal to zero.</item>
/// <item>The value for <paramref name="firstRetryInterval"/> is less than or equal to <see cref="TimeSpan.Zero"/>.</item>
/// <item>The value for <paramref name="backoffCoefficient"/> is less than 1.0.</item>
/// <item>The value for <paramref name="maxRetryInterval"/> is less than <paramref name="firstRetryInterval"/>.</item>
/// <item>The value for <paramref name="retryTimeout"/> is less than <paramref name="firstRetryInterval"/>.</item>
/// </list>
/// </exception>
public WorkflowRetryPolicy(
int maxNumberOfAttempts,
TimeSpan firstRetryInterval,
double backoffCoefficient = 1.0,
TimeSpan? maxRetryInterval = null,
TimeSpan? retryTimeout = null)
{
this.durableRetryPolicy = new RetryPolicy(
maxNumberOfAttempts,
firstRetryInterval,
backoffCoefficient,
maxRetryInterval,
retryTimeout);
}
/// <summary>
/// Gets the max number of attempts for executing a given task.
/// </summary>
public int MaxNumberOfAttempts => this.durableRetryPolicy.MaxNumberOfAttempts;
/// <summary>
/// Gets the amount of time to delay between the first and second attempt.
/// </summary>
public TimeSpan FirstRetryInterval => this.durableRetryPolicy.FirstRetryInterval;
/// <summary>
/// Gets the exponential back-off coefficient used to determine the delay between subsequent retries.
/// </summary>
/// <value>
/// Defaults to 1.0 for no back-off.
/// </value>
public double BackoffCoefficient => this.durableRetryPolicy.BackoffCoefficient;
/// <summary>
/// Gets the maximum time to delay between attempts.
/// </summary>
/// <value>
/// Defaults to 1 hour.
/// </value>
public TimeSpan MaxRetryInterval => this.durableRetryPolicy.MaxRetryInterval;
/// <summary>
/// Gets the overall timeout for retries. No further attempts will be made at executing a task after this retry
/// timeout expires.
/// </summary>
/// <value>
/// Defaults to <see cref="Timeout.InfiniteTimeSpan"/>.
/// </value>
public TimeSpan RetryTimeout => this.durableRetryPolicy.RetryTimeout;
internal RetryPolicy GetDurableRetryPolicy() => this.durableRetryPolicy;
}
}