You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-12-15 23:55:38 +03:00
fix(cluster): prevent infinite loop (#3078)
getRandomNode could end up in an infinite loop if this.masters is empty and this.replicas is empty. fixes: #3075
This commit is contained in:
committed by
GitHub
parent
e2702b63f2
commit
6eed1ee7ad
@@ -5,7 +5,6 @@ import RedisClusterSlots from './cluster-slots';
|
||||
|
||||
describe('RedisClusterSlots', () => {
|
||||
describe('initialization', () => {
|
||||
|
||||
describe('clientSideCache validation', () => {
|
||||
const mockEmit = ((_event: string | symbol, ..._args: any[]): boolean => true) as EventEmitter['emit'];
|
||||
const clientSideCacheConfig = { ttl: 0, maxEntries: 0 };
|
||||
@@ -45,4 +44,14 @@ describe('RedisClusterSlots', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getRandomNode', ()=> {
|
||||
it('should not enter infinite loop when no nodes', () => {
|
||||
const slots = new RedisClusterSlots({
|
||||
rootNodes: []
|
||||
}, () => true)
|
||||
slots.getRandomNode()
|
||||
slots.getRandomNode()
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -462,6 +462,7 @@ export default class RedisClusterSlots<
|
||||
}
|
||||
|
||||
*#iterateAllNodes() {
|
||||
if(this.masters.length + this.replicas.length === 0) return
|
||||
let i = Math.floor(Math.random() * (this.masters.length + this.replicas.length));
|
||||
if (i < this.masters.length) {
|
||||
do {
|
||||
|
||||
Reference in New Issue
Block a user