Skip to content

Commit e7304ad

Browse files
committed
Extract default connect strategy into helper function
1 parent 38eb603 commit e7304ad

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

packages/client/lib/client/socket.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,6 @@ export default class RedisSocket extends EventEmitter {
8282
}
8383

8484
#createReconnectStrategy(options?: RedisSocketOptions): ReconnectStrategyFunction {
85-
const defaultStrategy = (retries: number) => {
86-
// Generate a random jitter between 0 – 200 ms:
87-
const jitter = Math.floor(Math.random() * 200);
88-
// Delay is an exponential back off, (times^2) * 50 ms, with a maximum value of 2000 ms:
89-
const delay = Math.min(Math.pow(2, retries) * 50, 2000);
90-
91-
return delay + jitter;
92-
}
93-
9485
const strategy = options?.reconnectStrategy;
9586
if (strategy === false || typeof strategy === 'number') {
9687
return () => strategy;
@@ -106,12 +97,12 @@ export default class RedisSocket extends EventEmitter {
10697
return retryIn;
10798
} catch (err) {
10899
this.emit('error', err);
109-
return defaultStrategy(retries);
100+
return this.defaultReconnectStrategy(retries);
110101
}
111102
};
112103
}
113104

114-
return defaultStrategy;
105+
return this.defaultReconnectStrategy;
115106
}
116107

117108
#createSocketFactory(options?: RedisSocketOptions) {
@@ -342,4 +333,13 @@ export default class RedisSocket extends EventEmitter {
342333
this.#isSocketUnrefed = true;
343334
this.#socket?.unref();
344335
}
336+
337+
defaultReconnectStrategy(retries: number) {
338+
// Generate a random jitter between 0 – 200 ms:
339+
const jitter = Math.floor(Math.random() * 200);
340+
// Delay is an exponential back off, (times^2) * 50 ms, with a maximum value of 2000 ms:
341+
const delay = Math.min(Math.pow(2, retries) * 50, 2000);
342+
343+
return delay + jitter;
344+
}
345345
}

0 commit comments

Comments
 (0)