From 350a196ded323ea94e0da46234be90e7e071c7f6 Mon Sep 17 00:00:00 2001 From: Attila Kovacs Date: Sun, 5 Jan 2025 22:25:04 +0100 Subject: [PATCH] Only DB0 for clusters --- src/redisx-cluster.c | 6 ++++-- src/redisx.c | 10 ++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/redisx-cluster.c b/src/redisx-cluster.c index ea246ab..ac429e6 100644 --- a/src/redisx-cluster.c +++ b/src/redisx-cluster.c @@ -190,11 +190,13 @@ static RedisShard *rClusterDiscoverAsync(Redis *redis, int *n_shards) { } for(m = 0; m < s->n_servers; s++) { + Redis *r; RESP **node = (RESP **) desc[2]->value; - s->redis[m] = redisxInit((char *) node[0]->value); + r = s->redis[m] = redisxInit((char *) node[0]->value); redisxSetPort(s->redis[m], node[1]->n); - rCopyConfig(&((RedisPrivate *) redis->priv)->config, s->redis[m]); + rCopyConfig(&((RedisPrivate *) redis->priv)->config, r); + redisxSelectDB(r, 0); // Only DB 0 is allowed for clusters. } } } diff --git a/src/redisx.c b/src/redisx.c index c24039c..699ab94 100644 --- a/src/redisx.c +++ b/src/redisx.c @@ -443,13 +443,19 @@ int redisxSelectDB(Redis *redis, int idx) { prop_error(fn, rConfigLock(redis)); p = (RedisPrivate *) redis->priv; + + if(p->cluster && idx) { + rConfigUnlock(redis); + return x_error(X_SIZE_INVALID, EINVAL, fn, "cannot set non-zero db index %d for cluster", idx); + } + dbIdx = p->config.dbIndex; rConfigUnlock(redis); if(dbIdx == idx) return X_SUCCESS; - if(idx) redisxAddConnectHook(redis, rAffirmDB); - else redisxRemoveConnectHook(redis, rAffirmDB); + if(idx) redisxAddConnectHook(redis, rAffirmDB); // Use SELECT to switch to non-zero DB + else redisxRemoveConnectHook(redis, rAffirmDB); // Don't use SELECT for DB 0 (cluster safe) if(!redisxIsConnected(redis)) return X_SUCCESS;