-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING: Improve retry mechanism (#311)
- Implemented retry mechanism with exponential backoff algorithm - (**BREAKING**) Removed `RedisConnectionOptions.retryInterval` - Added `RedisConnectionOptions.backoff`
- Loading branch information
Showing
7 changed files
with
144 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export interface Backoff { | ||
/** | ||
* Returns the next backoff interval in milliseconds | ||
*/ | ||
(attempts: number): number; | ||
} | ||
|
||
export interface ExponentialBackoffOptions { | ||
/** | ||
* @default 2 | ||
*/ | ||
multiplier: number; | ||
/** | ||
* The maximum backoff interval in milliseconds | ||
* @default 5000 | ||
*/ | ||
maxInterval: number; | ||
/** | ||
* The minimum backoff interval in milliseconds | ||
* @default 500 | ||
*/ | ||
minInterval: number; | ||
} | ||
|
||
export function exponentialBackoff({ | ||
multiplier = 2, | ||
maxInterval = 5000, | ||
minInterval = 500, | ||
}: Partial<ExponentialBackoffOptions> = {}): Backoff { | ||
return (attempts) => | ||
Math.min(maxInterval, minInterval * (multiplier ** (attempts - 1))); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { assertEquals } from "../vendor/https/deno.land/std/testing/asserts.ts"; | ||
import { describe, it } from "../vendor/https/deno.land/std/testing/bdd.ts"; | ||
|
||
import { exponentialBackoff } from "../backoff.ts"; | ||
|
||
describe("backoff", { | ||
permissions: "none", | ||
}, () => { | ||
describe("exponentialBackoff", () => { | ||
it("should return exponentially increasing backoff intervals", () => { | ||
const backoff = exponentialBackoff({ | ||
multiplier: 2, | ||
maxInterval: 5000, | ||
minInterval: 1000, | ||
}); | ||
|
||
assertEquals(backoff(1), 1000); | ||
assertEquals(backoff(2), 2000); | ||
assertEquals(backoff(3), 4000); | ||
assertEquals(backoff(4), 5000); | ||
assertEquals(backoff(5), 5000); | ||
}); | ||
}); | ||
}); |
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