From 67782350a9da278457c3280c300ebec65bdc6326 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Thu, 9 Jan 2025 10:56:08 +0530 Subject: [PATCH] feat: add default retry strategy for redis (#10880) Fixes: FRMW-2861 --- .changeset/afraid-experts-walk.md | 5 +++++ .../common/__tests__/define-config.spec.ts | 18 +++++++++++++++++ .../core/utils/src/common/define-config.ts | 20 ++++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 .changeset/afraid-experts-walk.md diff --git a/.changeset/afraid-experts-walk.md b/.changeset/afraid-experts-walk.md new file mode 100644 index 0000000000..f734cb09fb --- /dev/null +++ b/.changeset/afraid-experts-walk.md @@ -0,0 +1,5 @@ +--- +"@medusajs/utils": patch +--- + +feat: add default retry strategy for redis diff --git a/packages/core/utils/src/common/__tests__/define-config.spec.ts b/packages/core/utils/src/common/__tests__/define-config.spec.ts index 9919e3e6b9..f49263b606 100644 --- a/packages/core/utils/src/common/__tests__/define-config.spec.ts +++ b/packages/core/utils/src/common/__tests__/define-config.spec.ts @@ -142,6 +142,9 @@ describe("defineConfig", function () { }, "storeCors": "http://localhost:8000", }, + "redisOptions": { + "retryStrategy": [Function], + }, }, } `) @@ -298,6 +301,9 @@ describe("defineConfig", function () { }, "storeCors": "http://localhost:8000", }, + "redisOptions": { + "retryStrategy": [Function], + }, }, } `) @@ -462,6 +468,9 @@ describe("defineConfig", function () { }, "storeCors": "http://localhost:8000", }, + "redisOptions": { + "retryStrategy": [Function], + }, }, } `) @@ -627,6 +636,9 @@ describe("defineConfig", function () { }, "storeCors": "http://localhost:8000", }, + "redisOptions": { + "retryStrategy": [Function], + }, }, } `) @@ -780,6 +792,9 @@ describe("defineConfig", function () { }, "storeCors": "http://localhost:8000", }, + "redisOptions": { + "retryStrategy": [Function], + }, }, } `) @@ -933,6 +948,9 @@ describe("defineConfig", function () { }, "storeCors": "http://localhost:8000", }, + "redisOptions": { + "retryStrategy": [Function], + }, }, } `) diff --git a/packages/core/utils/src/common/define-config.ts b/packages/core/utils/src/common/define-config.ts index 25fe76a992..fda1b8ec51 100644 --- a/packages/core/utils/src/common/define-config.ts +++ b/packages/core/utils/src/common/define-config.ts @@ -74,7 +74,8 @@ type Config = Partial< * to override configuration as needed. */ export function defineConfig(config: Config = {}): ConfigModule { - const { http, ...restOfProjectConfig } = config.projectConfig || {} + const { http, redisOptions, ...restOfProjectConfig } = + config.projectConfig || {} /** * The defaults to use for the project config. They are shallow merged @@ -93,6 +94,23 @@ export function defineConfig(config: Config = {}): ConfigModule { }, ...http, }, + redisOptions: { + retryStrategy(retries) { + /** + * Exponentially increase delay with every retry + * attempt. Max to 4s + */ + const delay = Math.min(Math.pow(2, retries) * 50, 4000) + + /** + * Add a random jitter to not choke the server when multiple + * clients are retrying at the same time + */ + const jitter = Math.floor(Math.random() * 200) + return delay + jitter + }, + ...redisOptions, + }, ...restOfProjectConfig, }