Skip to content

Commit 6778235

Browse files
authored
feat: add default retry strategy for redis (#10880)
Fixes: FRMW-2861
1 parent bd73716 commit 6778235

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

.changeset/afraid-experts-walk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/utils": patch
3+
---
4+
5+
feat: add default retry strategy for redis

packages/core/utils/src/common/__tests__/define-config.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ describe("defineConfig", function () {
142142
},
143143
"storeCors": "http://localhost:8000",
144144
},
145+
"redisOptions": {
146+
"retryStrategy": [Function],
147+
},
145148
},
146149
}
147150
`)
@@ -298,6 +301,9 @@ describe("defineConfig", function () {
298301
},
299302
"storeCors": "http://localhost:8000",
300303
},
304+
"redisOptions": {
305+
"retryStrategy": [Function],
306+
},
301307
},
302308
}
303309
`)
@@ -462,6 +468,9 @@ describe("defineConfig", function () {
462468
},
463469
"storeCors": "http://localhost:8000",
464470
},
471+
"redisOptions": {
472+
"retryStrategy": [Function],
473+
},
465474
},
466475
}
467476
`)
@@ -627,6 +636,9 @@ describe("defineConfig", function () {
627636
},
628637
"storeCors": "http://localhost:8000",
629638
},
639+
"redisOptions": {
640+
"retryStrategy": [Function],
641+
},
630642
},
631643
}
632644
`)
@@ -780,6 +792,9 @@ describe("defineConfig", function () {
780792
},
781793
"storeCors": "http://localhost:8000",
782794
},
795+
"redisOptions": {
796+
"retryStrategy": [Function],
797+
},
783798
},
784799
}
785800
`)
@@ -933,6 +948,9 @@ describe("defineConfig", function () {
933948
},
934949
"storeCors": "http://localhost:8000",
935950
},
951+
"redisOptions": {
952+
"retryStrategy": [Function],
953+
},
936954
},
937955
}
938956
`)

packages/core/utils/src/common/define-config.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ type Config = Partial<
7474
* to override configuration as needed.
7575
*/
7676
export function defineConfig(config: Config = {}): ConfigModule {
77-
const { http, ...restOfProjectConfig } = config.projectConfig || {}
77+
const { http, redisOptions, ...restOfProjectConfig } =
78+
config.projectConfig || {}
7879

7980
/**
8081
* The defaults to use for the project config. They are shallow merged
@@ -93,6 +94,23 @@ export function defineConfig(config: Config = {}): ConfigModule {
9394
},
9495
...http,
9596
},
97+
redisOptions: {
98+
retryStrategy(retries) {
99+
/**
100+
* Exponentially increase delay with every retry
101+
* attempt. Max to 4s
102+
*/
103+
const delay = Math.min(Math.pow(2, retries) * 50, 4000)
104+
105+
/**
106+
* Add a random jitter to not choke the server when multiple
107+
* clients are retrying at the same time
108+
*/
109+
const jitter = Math.floor(Math.random() * 200)
110+
return delay + jitter
111+
},
112+
...redisOptions,
113+
},
96114
...restOfProjectConfig,
97115
}
98116

0 commit comments

Comments
 (0)