-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[ServiceBus] add delay between infinite retry cycles #20316
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
Merged
jeremymeng
merged 7 commits into
Azure:main
from
jeremymeng:sb/delay-between-reconnecting-attemp
Feb 12, 2022
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
786dec4
[ServiceBus] add delay between infinite retry cycles
jeremymeng a044202
Add a test and update changelog
jeremymeng 57d6df2
Address CR feedback
jeremymeng 55e4c0d
Update sdk/servicebus/service-bus/CHANGELOG.md
jeremymeng 73f4689
Merge remote-tracking branch 'upstream/main' into sb/delay-between-re…
jeremymeng b1b2cab
Merge remote-tracking branch 'upstream/main' into sb/delay-between-re…
jeremymeng e48a827
Revert max delay limit in Fixed retry mode
jeremymeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,9 +15,12 @@ import { | |
| import { DispositionStatusOptions } from "../core/managementClient"; | ||
| import { ConnectionContext } from "../connectionContext"; | ||
| import { | ||
| Constants, | ||
| ErrorNameConditionMapper, | ||
| delay, | ||
| retry, | ||
| RetryConfig, | ||
| RetryMode, | ||
| RetryOperationType, | ||
| RetryOptions, | ||
| } from "@azure/core-amqp"; | ||
|
|
@@ -279,6 +282,28 @@ export interface RetryForeverArgs<T> { | |
| logPrefix: string; | ||
| } | ||
|
|
||
| /** | ||
| * Calculates delay between retries, in milliseconds. | ||
| * @internal | ||
| */ | ||
| function calculateDelay( | ||
| attemptCount: number, | ||
| retryDelayInMs: number, | ||
| maxRetryDelayInMs: number, | ||
| mode: RetryMode | ||
| ): number { | ||
| if (mode === RetryMode.Exponential) { | ||
| const boundedRandDelta = | ||
| retryDelayInMs * 0.8 + | ||
| Math.floor(Math.random() * (retryDelayInMs * 1.2 - retryDelayInMs * 0.8)); | ||
|
|
||
| const incrementDelta = boundedRandDelta * (Math.pow(2, attemptCount) - 1); | ||
| return Math.min(incrementDelta, maxRetryDelayInMs); | ||
| } | ||
|
|
||
| return retryDelayInMs; | ||
| } | ||
|
|
||
| /** | ||
| * Retry infinitely until success, reporting in between retry attempts. | ||
| * | ||
|
|
@@ -293,6 +318,22 @@ export async function retryForever<T>( | |
| retryFn: typeof retry = retry | ||
| ): Promise<T> { | ||
| let numRetryCycles = 0; | ||
| const config = args.retryConfig; | ||
|
Member
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. I duplicated the code from core-amqp's retry as they are used internally and not really worth to expose to reuse. |
||
| if (!config.retryOptions) { | ||
| config.retryOptions = {}; | ||
| } | ||
| if (config.retryOptions.retryDelayInMs == undefined || config.retryOptions.retryDelayInMs < 0) { | ||
| config.retryOptions.retryDelayInMs = Constants.defaultDelayBetweenOperationRetriesInMs; | ||
| } | ||
| if ( | ||
| config.retryOptions.maxRetryDelayInMs == undefined || | ||
| config.retryOptions.maxRetryDelayInMs < 0 | ||
| ) { | ||
| config.retryOptions.maxRetryDelayInMs = Constants.defaultMaxDelayForExponentialRetryInMs; | ||
| } | ||
| if (config.retryOptions.mode == undefined) { | ||
| config.retryOptions.mode = RetryMode.Fixed; | ||
| } | ||
|
|
||
| // The retries are broken up into cycles, giving the user some control over how often | ||
| // we actually attempt to retry. | ||
|
|
@@ -325,6 +366,20 @@ export async function retryForever<T>( | |
| args.retryConfig | ||
| ); | ||
|
|
||
| const delayInMs = calculateDelay( | ||
| numRetryCycles, | ||
| config.retryOptions.retryDelayInMs, | ||
| config.retryOptions.maxRetryDelayInMs, | ||
| config.retryOptions.mode | ||
| ); | ||
| logger.verbose( | ||
| "[%s] Sleeping for %d milliseconds for '%s'.", | ||
| config.connectionId, | ||
| delayInMs, | ||
| config.operationType | ||
| ); | ||
| await delay<void>(delayInMs, config.abortSignal, "Retry cycle has been cancelled by the user."); | ||
|
Contributor
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. changelog? |
||
|
|
||
| continue; | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: no need to tag it as internal if it is not exported.