|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +/** |
| 5 | + * A declarative retry policy that can be configured for activity or sub-orchestration calls. |
| 6 | + * |
| 7 | + * @remarks |
| 8 | + * Retry policies control how many times a task is retried and the delay between retries. |
| 9 | + * The delay between retries increases exponentially based on the backoffCoefficient. |
| 10 | + * |
| 11 | + * @example |
| 12 | + * ```typescript |
| 13 | + * const retryPolicy = new RetryPolicy({ |
| 14 | + * maxNumberOfAttempts: 5, |
| 15 | + * firstRetryIntervalInMilliseconds: 1000, |
| 16 | + * backoffCoefficient: 2.0, |
| 17 | + * maxRetryIntervalInMilliseconds: 30000, |
| 18 | + * retryTimeoutInMilliseconds: 300000 |
| 19 | + * }); |
| 20 | + * ``` |
| 21 | + */ |
| 22 | +export class RetryPolicy { |
| 23 | + private readonly _maxNumberOfAttempts: number; |
| 24 | + private readonly _firstRetryIntervalInMilliseconds: number; |
| 25 | + private readonly _backoffCoefficient: number; |
| 26 | + private readonly _maxRetryIntervalInMilliseconds: number; |
| 27 | + private readonly _retryTimeoutInMilliseconds: number; |
| 28 | + |
| 29 | + /** |
| 30 | + * Creates a new RetryPolicy instance. |
| 31 | + * |
| 32 | + * @param options - The retry policy options |
| 33 | + * @throws Error if any of the validation constraints are violated |
| 34 | + */ |
| 35 | + constructor(options: RetryPolicyOptions) { |
| 36 | + const { |
| 37 | + maxNumberOfAttempts, |
| 38 | + firstRetryIntervalInMilliseconds, |
| 39 | + backoffCoefficient = 1.0, |
| 40 | + maxRetryIntervalInMilliseconds, |
| 41 | + retryTimeoutInMilliseconds, |
| 42 | + } = options; |
| 43 | + |
| 44 | + // Validation aligned with .NET SDK |
| 45 | + if (maxNumberOfAttempts <= 0) { |
| 46 | + throw new Error("maxNumberOfAttempts must be greater than zero"); |
| 47 | + } |
| 48 | + |
| 49 | + if (firstRetryIntervalInMilliseconds <= 0) { |
| 50 | + throw new Error("firstRetryIntervalInMilliseconds must be greater than zero"); |
| 51 | + } |
| 52 | + |
| 53 | + if (backoffCoefficient < 1.0) { |
| 54 | + throw new Error("backoffCoefficient must be greater than or equal to 1.0"); |
| 55 | + } |
| 56 | + |
| 57 | + if ( |
| 58 | + maxRetryIntervalInMilliseconds !== undefined && |
| 59 | + maxRetryIntervalInMilliseconds !== -1 && |
| 60 | + maxRetryIntervalInMilliseconds < firstRetryIntervalInMilliseconds |
| 61 | + ) { |
| 62 | + throw new Error("maxRetryIntervalInMilliseconds must be greater than or equal to firstRetryIntervalInMilliseconds"); |
| 63 | + } |
| 64 | + |
| 65 | + if ( |
| 66 | + retryTimeoutInMilliseconds !== undefined && |
| 67 | + retryTimeoutInMilliseconds !== -1 && |
| 68 | + retryTimeoutInMilliseconds < firstRetryIntervalInMilliseconds |
| 69 | + ) { |
| 70 | + throw new Error("retryTimeoutInMilliseconds must be greater than or equal to firstRetryIntervalInMilliseconds"); |
| 71 | + } |
| 72 | + |
| 73 | + this._maxNumberOfAttempts = maxNumberOfAttempts; |
| 74 | + this._firstRetryIntervalInMilliseconds = firstRetryIntervalInMilliseconds; |
| 75 | + this._backoffCoefficient = backoffCoefficient; |
| 76 | + // Default to 1 hour (3600000ms) if not specified, -1 means infinite |
| 77 | + this._maxRetryIntervalInMilliseconds = maxRetryIntervalInMilliseconds ?? 3600000; |
| 78 | + // Default to -1 (infinite) if not specified |
| 79 | + this._retryTimeoutInMilliseconds = retryTimeoutInMilliseconds ?? -1; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Gets the max number of attempts for executing a given task. |
| 84 | + */ |
| 85 | + get maxNumberOfAttempts(): number { |
| 86 | + return this._maxNumberOfAttempts; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Gets the amount of time in milliseconds to delay between the first and second attempt. |
| 91 | + */ |
| 92 | + get firstRetryIntervalInMilliseconds(): number { |
| 93 | + return this._firstRetryIntervalInMilliseconds; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Gets the exponential back-off coefficient used to determine the delay between subsequent retries. |
| 98 | + * @remarks Defaults to 1.0 for no back-off. |
| 99 | + */ |
| 100 | + get backoffCoefficient(): number { |
| 101 | + return this._backoffCoefficient; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Gets the maximum time in milliseconds to delay between attempts. |
| 106 | + * @remarks Defaults to 1 hour (3600000ms). Use -1 for infinite. |
| 107 | + */ |
| 108 | + get maxRetryIntervalInMilliseconds(): number { |
| 109 | + return this._maxRetryIntervalInMilliseconds; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Gets the overall timeout for retries in milliseconds. |
| 114 | + * No further attempts will be made after this timeout expires. |
| 115 | + * @remarks Defaults to -1 (infinite). |
| 116 | + */ |
| 117 | + get retryTimeoutInMilliseconds(): number { |
| 118 | + return this._retryTimeoutInMilliseconds; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Options for creating a RetryPolicy. |
| 124 | + */ |
| 125 | +export interface RetryPolicyOptions { |
| 126 | + /** |
| 127 | + * The maximum number of task invocation attempts. Must be 1 or greater. |
| 128 | + */ |
| 129 | + maxNumberOfAttempts: number; |
| 130 | + |
| 131 | + /** |
| 132 | + * The amount of time in milliseconds to delay between the first and second attempt. |
| 133 | + * Must be greater than 0. |
| 134 | + */ |
| 135 | + firstRetryIntervalInMilliseconds: number; |
| 136 | + |
| 137 | + /** |
| 138 | + * The exponential back-off coefficient used to determine the delay between subsequent retries. |
| 139 | + * Must be 1.0 or greater. |
| 140 | + * @default 1.0 |
| 141 | + */ |
| 142 | + backoffCoefficient?: number; |
| 143 | + |
| 144 | + /** |
| 145 | + * The maximum time in milliseconds to delay between attempts. |
| 146 | + * Must be greater than or equal to firstRetryIntervalInMilliseconds. |
| 147 | + * Use -1 for infinite (no maximum). |
| 148 | + * @default 3600000 (1 hour) |
| 149 | + */ |
| 150 | + maxRetryIntervalInMilliseconds?: number; |
| 151 | + |
| 152 | + /** |
| 153 | + * The overall timeout for retries in milliseconds. |
| 154 | + * No further attempts will be made after this timeout expires. |
| 155 | + * Use -1 for infinite (no timeout). |
| 156 | + * @default -1 (infinite) |
| 157 | + */ |
| 158 | + retryTimeoutInMilliseconds?: number; |
| 159 | +} |
0 commit comments