Skip to content

Commit

Permalink
Only DB0 for clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
attipaci committed Jan 5, 2025
1 parent 8d00cd0 commit 350a196
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/redisx-cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/redisx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 350a196

Please sign in to comment.