-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change adds the RetryPolicy type which represents the retry policy to use as an union type of the different supported retry policies. This is an initial change that only replaces RetryPolicy with IRetry and keeps the same current behavior. However, this change will enable us to store the retry policy with the DurablePromise to be used in the recovery path.
- Loading branch information
Showing
12 changed files
with
135 additions
and
93 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,24 +1,111 @@ | ||
export interface IRetry { | ||
next<T extends { attempt: number; timeout: number }>(ctx: T): { done: boolean; delay?: number }; | ||
iterator<T extends { attempt: number; timeout: number }>(ctx: T): IterableIterator<number>; | ||
export type RetryPolicy = Exponential | Linear | Never; | ||
|
||
export type Exponential = { | ||
kind: "exponential"; | ||
initialDelayMs: number; | ||
backoffFactor: number; | ||
maxAttempts: number; | ||
maxDelayMs: number; | ||
}; | ||
|
||
export type Linear = { | ||
kind: "linear"; | ||
delayMs: number; | ||
maxAttempts: number; | ||
}; | ||
|
||
export type Never = { | ||
kind: "never"; | ||
}; | ||
|
||
export function exponential( | ||
initialDelayMs: number = 100, | ||
backoffFactor: number = 2, | ||
maxAttempts: number = Infinity, | ||
maxDelayMs: number = 60000, | ||
): Exponential { | ||
return { | ||
kind: "exponential", | ||
initialDelayMs, | ||
backoffFactor, | ||
maxAttempts, | ||
maxDelayMs, | ||
}; | ||
} | ||
|
||
export class IterableRetry implements IRetry { | ||
next<T extends { attempt: number; timeout: number }>(ctx: T): { done: boolean; delay?: number } { | ||
throw new Error("Method not implemented"); | ||
} | ||
export function linear(delayMs: number = 1000, maxAttempts: number = Infinity): Linear { | ||
return { | ||
kind: "linear", | ||
delayMs, | ||
maxAttempts, | ||
}; | ||
} | ||
|
||
iterator<T extends { attempt: number; timeout: number }>(ctx: T): IterableIterator<number> { | ||
const self = this; // eslint-disable-line @typescript-eslint/no-this-alias | ||
export function never(): Never { | ||
return { kind: "never" }; | ||
} | ||
|
||
export function retryIterator<T extends { retryPolicy: RetryPolicy; attempt: number; timeout: number }>( | ||
ctx: T, | ||
): IterableIterator<number> { | ||
const { initialDelay, backoffFactor, maxAttempts, maxDelay } = retryDefaults(ctx.retryPolicy); | ||
|
||
const __next = (itCtx: { attempt: number; timeout: number }): { done: boolean; delay?: number } => { | ||
// attempt 0: 0ms delay | ||
// attampt n: {initial * factor^(attempt-1)}ms delay (or max delay) | ||
const delay = Math.min( | ||
Math.min(itCtx.attempt, 1) * initialDelay * Math.pow(backoffFactor, itCtx.attempt - 1), | ||
maxDelay, | ||
); | ||
|
||
if (Date.now() + delay >= itCtx.timeout || itCtx.attempt >= maxAttempts) { | ||
return { done: true }; | ||
} | ||
|
||
return { | ||
next() { | ||
const { done, delay } = self.next(ctx); | ||
return { done, value: delay || 0 }; | ||
}, | ||
[Symbol.iterator]() { | ||
return this; | ||
}, | ||
done: false, | ||
delay: delay, | ||
}; | ||
}; | ||
|
||
return { | ||
next() { | ||
const { done, delay } = __next(ctx); | ||
return { done, value: delay || 0 }; | ||
}, | ||
[Symbol.iterator]() { | ||
return this; | ||
}, | ||
}; | ||
} | ||
|
||
function retryDefaults(retryPolicy: RetryPolicy): { | ||
initialDelay: number; | ||
backoffFactor: number; | ||
maxAttempts: number; | ||
maxDelay: number; | ||
} { | ||
switch (retryPolicy.kind) { | ||
case "exponential": | ||
return { | ||
initialDelay: retryPolicy.initialDelayMs, | ||
backoffFactor: retryPolicy.backoffFactor, | ||
maxAttempts: retryPolicy.maxAttempts, | ||
maxDelay: retryPolicy.maxDelayMs, | ||
}; | ||
case "linear": | ||
return { | ||
initialDelay: retryPolicy.delayMs, | ||
backoffFactor: 1, | ||
maxAttempts: retryPolicy.maxAttempts, | ||
maxDelay: retryPolicy.delayMs, | ||
}; | ||
case "never": | ||
return { | ||
initialDelay: 0, | ||
backoffFactor: 0, | ||
maxAttempts: 1, | ||
maxDelay: 0, | ||
}; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.