Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ignoring SELECT from client #431

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/nc_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ static struct command conf_commands[] = {
conf_set_num,
offsetof(struct conf_pool, redis_db) },

{ string("redis_ignore_select"),
conf_set_bool,
offsetof(struct conf_pool, redis_ignore_select) },

{ string("preconnect"),
conf_set_bool,
offsetof(struct conf_pool, preconnect) },
Expand Down Expand Up @@ -200,6 +204,7 @@ conf_pool_init(struct conf_pool *cp, struct string *name)
cp->redis = CONF_UNSET_NUM;
cp->tcpkeepalive = CONF_UNSET_NUM;
cp->redis_db = CONF_UNSET_NUM;
cp->redis_ignore_select = CONF_UNSET_NUM;
cp->preconnect = CONF_UNSET_NUM;
cp->auto_eject_hosts = CONF_UNSET_NUM;
cp->server_connections = CONF_UNSET_NUM;
Expand Down Expand Up @@ -292,6 +297,7 @@ conf_pool_each_transform(void *elem, void *data)
sp->timeout = cp->timeout;
sp->backlog = cp->backlog;
sp->redis_db = cp->redis_db;
sp->redis_ignore_select = cp->redis_ignore_select ? 1 : 0;

sp->redis_auth = cp->redis_auth;
sp->require_auth = cp->redis_auth.len > 0 ? 1 : 0;
Expand Down Expand Up @@ -1245,6 +1251,10 @@ conf_validate_pool(struct conf *cf, struct conf_pool *cp)
if (cp->redis_db == CONF_UNSET_NUM) {
cp->redis_db = CONF_DEFAULT_REDIS_DB;
}

if (cp->redis_ignore_select == CONF_UNSET_NUM) {
cp->redis_ignore_select = CONF_DEFAULT_REDIS_IGNORE_SELECT;
}

if (cp->preconnect == CONF_UNSET_NUM) {
cp->preconnect = CONF_DEFAULT_PRECONNECT;
Expand Down
2 changes: 2 additions & 0 deletions src/nc_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#define CONF_DEFAULT_CLIENT_CONNECTIONS 0
#define CONF_DEFAULT_REDIS false
#define CONF_DEFAULT_REDIS_DB 0
#define CONF_DEFAULT_REDIS_IGNORE_SELECT false
#define CONF_DEFAULT_PRECONNECT false
#define CONF_DEFAULT_AUTO_EJECT_HOSTS false
#define CONF_DEFAULT_SERVER_RETRY_TIMEOUT 30 * 1000 /* in msec */
Expand Down Expand Up @@ -88,6 +89,7 @@ struct conf_pool {
int redis; /* redis: */
struct string redis_auth; /* redis_auth: redis auth password (matches requirepass on redis) */
int redis_db; /* redis_db: redis db */
int redis_ignore_select; /* redis_ignore_select: whether to ignore selects and just reply ok */
int preconnect; /* preconnect: */
int auto_eject_hosts; /* auto_eject_hosts: */
int server_connections; /* server_connections: */
Expand Down
1 change: 1 addition & 0 deletions src/nc_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ struct server_pool {
unsigned preconnect:1; /* preconnect? */
unsigned redis:1; /* redis? */
unsigned tcpkeepalive:1; /* tcpkeepalive? */
unsigned redis_ignore_select:1;/* redis ignore select? */
};

void server_ref(struct conn *conn, void *owner);
Expand Down
17 changes: 15 additions & 2 deletions src/proto/nc_redis.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ static bool
redis_arg0(struct msg *r)
{
switch (r->type) {
case MSG_REQ_REDIS_SELECT:

case MSG_REQ_REDIS_EXISTS:
case MSG_REQ_REDIS_PERSIST:
case MSG_REQ_REDIS_PTTL:
Expand Down Expand Up @@ -105,7 +107,7 @@ redis_arg0(struct msg *r)
static bool
redis_arg1(struct msg *r)
{
switch (r->type) {
switch (r->type) {
case MSG_REQ_REDIS_EXPIRE:
case MSG_REQ_REDIS_EXPIREAT:
case MSG_REQ_REDIS_PEXPIRE:
Expand Down Expand Up @@ -878,6 +880,12 @@ redis_parse_req(struct msg *r)
r->type = MSG_REQ_REDIS_ZSCORE;
break;
}

if (str6icmp(m, 's', 'e', 'l', 'e', 'c', 't')) {
r->type = MSG_REQ_REDIS_SELECT;
r->noforward = 1;
break;
}

break;

Expand Down Expand Up @@ -2682,6 +2690,7 @@ redis_reply(struct msg *r)
{
struct conn *c_conn;
struct msg *response = r->peer;
struct server_pool *pool;

ASSERT(response != NULL && response->owner != NULL);

Expand All @@ -2697,7 +2706,11 @@ redis_reply(struct msg *r)
switch (r->type) {
case MSG_REQ_REDIS_PING:
return msg_append(response, rsp_pong.data, rsp_pong.len);

case MSG_REQ_REDIS_SELECT:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternately or additionally, it may be safe to allow forwarding this entirely if the db number is equivalent to the one in the config file for redis_db (pool->redis_db)

pool = (struct server_pool *)c_conn->owner;
if (pool->redis_ignore_select) {
return msg_append(response, rsp_ok.data, rsp_ok.len);
}
default:
NOT_REACHED();
return NC_ERROR;
Expand Down