Skip to content

Commit 2be24ee

Browse files
authored
fix: retry if eval fails (#160)
1 parent be6f0ee commit 2be24ee

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/lib.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,27 @@ class RedisStore implements Store {
103103
this.windowMs = options.windowMs;
104104
}
105105

106+
async runCommandWithRetry(key: string) {
107+
const evalCommand = async () =>
108+
this.sendCommand(
109+
"EVALSHA",
110+
await this.loadedScriptSha1,
111+
"1",
112+
this.prefixKey(key),
113+
this.resetExpiryOnChange ? "1" : "0",
114+
this.windowMs.toString()
115+
);
116+
117+
try {
118+
const result = await evalCommand();
119+
return result;
120+
} catch {
121+
// TODO: distinguish different error types
122+
this.loadedScriptSha1 = this.loadScript();
123+
return evalCommand();
124+
}
125+
}
126+
106127
/**
107128
* Method to increment a client's hit counter.
108129
*
@@ -111,14 +132,7 @@ class RedisStore implements Store {
111132
* @returns {IncrementResponse} - The number of hits and reset time for that client
112133
*/
113134
async increment(key: string): Promise<IncrementResponse> {
114-
const results = await this.sendCommand(
115-
"EVALSHA",
116-
await this.loadedScriptSha1,
117-
"1",
118-
this.prefixKey(key),
119-
this.resetExpiryOnChange ? "1" : "0",
120-
this.windowMs.toString()
121-
);
135+
const results = await this.runCommandWithRetry(key);
122136

123137
if (!Array.isArray(results)) {
124138
throw new TypeError("Expected result to be array of values");

0 commit comments

Comments
 (0)