| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using Azure.Core; |
| | 6 | |
|
| | 7 | | namespace Azure.Messaging.ServiceBus |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// The set of options that can be specified to influence how |
| | 11 | | /// retry attempts are made, and a failure is eligible to be retried. |
| | 12 | | /// </summary> |
| | 13 | | /// |
| | 14 | | public class ServiceBusRetryOptions |
| | 15 | | { |
| | 16 | | /// <summary>The maximum number of retry attempts before considering the associated operation to have failed.</s |
| 308 | 17 | | private int _maxRetries = 3; |
| | 18 | |
|
| | 19 | | /// <summary>The delay or back-off factor to apply between retry attempts.</summary> |
| 308 | 20 | | private TimeSpan _delay = TimeSpan.FromSeconds(0.8); |
| | 21 | |
|
| | 22 | | /// <summary>The maximum delay to allow between retry attempts.</summary> |
| 308 | 23 | | private TimeSpan _maxDelay = TimeSpan.FromMinutes(1); |
| | 24 | |
|
| | 25 | | /// <summary>The maximum duration to wait for an operation, per attempt.</summary> |
| 308 | 26 | | private TimeSpan _tryTimeout = TimeSpan.FromMinutes(1); |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// The approach to use for calculating retry delays. |
| | 30 | | /// </summary> |
| | 31 | | /// |
| 458 | 32 | | public ServiceBusRetryMode Mode { get; set; } = ServiceBusRetryMode.Exponential; |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// The maximum number of retry attempts before considering the associated operation |
| | 36 | | /// to have failed. |
| | 37 | | /// </summary> |
| | 38 | | /// |
| | 39 | | public int MaxRetries |
| | 40 | | { |
| 142 | 41 | | get => _maxRetries; |
| | 42 | |
|
| | 43 | | set |
| | 44 | | { |
| 72 | 45 | | Argument.AssertInRange(value, 0, 100, nameof(MaxRetries)); |
| 72 | 46 | | _maxRetries = value; |
| 72 | 47 | | } |
| | 48 | | } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// The delay between retry attempts for a fixed approach or the delay |
| | 52 | | /// on which to base calculations for a backoff-based approach. |
| | 53 | | /// </summary> |
| | 54 | | /// |
| | 55 | | public TimeSpan Delay |
| | 56 | | { |
| 126 | 57 | | get => _delay; |
| | 58 | |
|
| | 59 | | set |
| | 60 | | { |
| 72 | 61 | | Argument.AssertInRange(value, TimeSpan.FromMilliseconds(1), TimeSpan.FromMinutes(5), nameof(Delay)); |
| 72 | 62 | | _delay = value; |
| 72 | 63 | | } |
| | 64 | | } |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// The maximum permissible delay between retry attempts. |
| | 68 | | /// </summary> |
| | 69 | | /// |
| | 70 | | public TimeSpan MaxDelay |
| | 71 | | { |
| 108 | 72 | | get => _maxDelay; |
| | 73 | |
|
| | 74 | | set |
| | 75 | | { |
| 72 | 76 | | Argument.AssertNotNegative(value, nameof(MaxDelay)); |
| 72 | 77 | | _maxDelay = value; |
| 72 | 78 | | } |
| | 79 | | } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// The maximum duration to wait for completion of a single attempt, whether the initial |
| | 83 | | /// attempt or a retry. |
| | 84 | | /// </summary> |
| | 85 | | /// |
| | 86 | | public TimeSpan TryTimeout |
| | 87 | | { |
| 96 | 88 | | get => _tryTimeout; |
| | 89 | |
|
| | 90 | | set |
| | 91 | | { |
| 60 | 92 | | if (value < TimeSpan.Zero) |
| | 93 | | { |
| | 94 | | #pragma warning disable CA2208 // Instantiate argument exceptions. Using property name is more intuitive than Value. |
| 0 | 95 | | throw new ArgumentException(Resources.TimeoutMustBePositive, nameof(TryTimeout)); |
| | 96 | | #pragma warning restore CA2208 // Instantiate argument exceptions correctly |
| | 97 | | } |
| | 98 | |
|
| 60 | 99 | | Argument.AssertInRange(value, TimeSpan.Zero, TimeSpan.FromHours(1), nameof(TryTimeout)); |
| 60 | 100 | | _tryTimeout = value; |
| 60 | 101 | | } |
| | 102 | | } |
| | 103 | |
|
| | 104 | | /// <summary> |
| | 105 | | /// A custom retry policy to be used in place of the individual option values. |
| | 106 | | /// </summary> |
| | 107 | | /// |
| | 108 | | /// <remarks> |
| | 109 | | /// When populated, this custom policy will take precedence over the individual retry |
| | 110 | | /// options provided. |
| | 111 | | /// </remarks> |
| | 112 | | /// |
| 208 | 113 | | public ServiceBusRetryPolicy CustomRetryPolicy { get; set; } |
| | 114 | | } |
| | 115 | | } |