-
Notifications
You must be signed in to change notification settings - Fork 599
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
feat(util-waiter): add waiter utilities package #1736
Changes from 1 commit
bbfc196
4f79dbb
2bf531f
a620107
013953c
05acce5
cd39e22
7702314
6b74244
5f622b5
40a6460
1fc129f
2b52619
88ac270
4fbdb04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
import { sleep } from "./utils/sleep"; | ||
import { WaiterOptions, WaiterResult, WaiterState } from "./waiter"; | ||
|
||
function exponentialBackoff(floor: number, ciel: number, attempt: number): number { | ||
return Math.min(ciel, floor * 2 ** attempt); | ||
} | ||
/** | ||
* Reference: https://github.com/awslabs/smithy/pull/656 | ||
* The theoretical limit to the attempt is max delay cannot be > Number.MAX_VALUE, but it's unlikely because of | ||
* `maxWaitTime` | ||
*/ | ||
const exponentialBackoff = (floor: number, ciel: number, attempt: number) => | ||
Math.floor(Math.min(ciel, randomInRange(floor, floor * 2 ** (attempt - 1)))); | ||
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 think this will overflow your number type for large values of attempt. To address this, you can compute the maximum attempt ceiling that doesn't exceed your ceiling. Pseudo code:
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. This 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. As I mentioned in the comment, this is not possible because it is in fact limited by 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 dont think Javascript has number overflow: https://www.algotech.solutions/blog/javascript/handle-number-overflow-javascript/ 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. @alexforsyth It's possible to overflow the JS Number: > Number.MAX_VALUE * 2
> Infinity |
||
const randomInRange = (min: number, max: number) => min + Math.random() * (max - min); | ||
|
||
/** | ||
* Function that runs indefinite polling as part of waiters. | ||
|
@@ -13,22 +18,19 @@ function exponentialBackoff(floor: number, ciel: number, attempt: number): numbe | |
* @param stateChecker function that checks the acceptor states on each poll. | ||
*/ | ||
export const runPolling = async <T, S>( | ||
params: WaiterOptions, | ||
{ minDelay, maxDelay }: WaiterOptions, | ||
client: T, | ||
input: S, | ||
acceptorChecks: (client: T, input: S) => Promise<WaiterResult> | ||
): Promise<WaiterResult> => { | ||
let currentAttempt = 1; | ||
let currentDelay = params.minDelay; | ||
|
||
while (true) { | ||
await sleep(currentDelay); | ||
await sleep(exponentialBackoff(minDelay, maxDelay, currentAttempt)); | ||
AllanZhengYP marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const { state } = await acceptorChecks(client, input); | ||
if (state === WaiterState.SUCCESS || state === WaiterState.FAILURE) { | ||
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. Probably better to say |
||
return { state }; | ||
} | ||
|
||
currentDelay = exponentialBackoff(params.minDelay, params.maxDelay, currentAttempt); | ||
currentAttempt += 1; | ||
} | ||
}; |
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.
I think it's better to link to the waiters spec rather than the PR to update the spec. Maybe link here since this link will be relevant when we release the next version of Smithy that includes jitter in waiters: https://awslabs.github.io/smithy/1.0/spec/waiters.html#waiter-retries