Skip to content

Commit

Permalink
Extract default connect strategy into helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
qsymmachus committed Apr 29, 2024
1 parent 38eb603 commit e7304ad
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions packages/client/lib/client/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,6 @@ export default class RedisSocket extends EventEmitter {
}

#createReconnectStrategy(options?: RedisSocketOptions): ReconnectStrategyFunction {
const defaultStrategy = (retries: number) => {
// Generate a random jitter between 0 – 200 ms:
const jitter = Math.floor(Math.random() * 200);
// Delay is an exponential back off, (times^2) * 50 ms, with a maximum value of 2000 ms:
const delay = Math.min(Math.pow(2, retries) * 50, 2000);

return delay + jitter;
}

const strategy = options?.reconnectStrategy;
if (strategy === false || typeof strategy === 'number') {
return () => strategy;
Expand All @@ -106,12 +97,12 @@ export default class RedisSocket extends EventEmitter {
return retryIn;
} catch (err) {
this.emit('error', err);
return defaultStrategy(retries);
return this.defaultReconnectStrategy(retries);
}
};
}

return defaultStrategy;
return this.defaultReconnectStrategy;
}

#createSocketFactory(options?: RedisSocketOptions) {
Expand Down Expand Up @@ -342,4 +333,13 @@ export default class RedisSocket extends EventEmitter {
this.#isSocketUnrefed = true;
this.#socket?.unref();
}

defaultReconnectStrategy(retries: number) {
// Generate a random jitter between 0 – 200 ms:
const jitter = Math.floor(Math.random() * 200);
// Delay is an exponential back off, (times^2) * 50 ms, with a maximum value of 2000 ms:
const delay = Math.min(Math.pow(2, retries) * 50, 2000);

return delay + jitter;
}
}

0 comments on commit e7304ad

Please sign in to comment.