Skip to content

Commit 91fa10b

Browse files
committed
Merge pull request #255 from idning/ping_quit
ping & quit supported
2 parents c19c9f2 + 4175419 commit 91fa10b

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

src/nc_message.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ _msg_get(void)
229229
msg->result = MSG_PARSE_OK;
230230

231231
msg->fragment = NULL;
232+
msg->reply = NULL;
232233
msg->pre_coalesce = NULL;
233234
msg->post_coalesce = NULL;
234235

@@ -262,6 +263,7 @@ _msg_get(void)
262263
msg->request = 0;
263264
msg->quit = 0;
264265
msg->noreply = 0;
266+
msg->noforward = 0;
265267
msg->done = 0;
266268
msg->fdone = 0;
267269
msg->swallow = 0;
@@ -291,6 +293,7 @@ msg_get(struct conn *conn, bool request, bool redis)
291293
msg->parser = redis_parse_rsp;
292294
}
293295
msg->fragment = redis_fragment;
296+
msg->reply = redis_reply;
294297
msg->pre_coalesce = redis_pre_coalesce;
295298
msg->post_coalesce = redis_post_coalesce;
296299
} else {

src/nc_message.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
typedef void (*msg_parse_t)(struct msg *);
2424
typedef rstatus_t (*msg_fragment_t)(struct msg *, uint32_t, struct msg_tqh *);
2525
typedef void (*msg_coalesce_t)(struct msg *r);
26+
typedef rstatus_t (*msg_reply_t)(struct msg *r);
2627

2728
typedef enum msg_parse_result {
2829
MSG_PARSE_OK, /* parsing ok */
@@ -157,6 +158,8 @@ typedef enum msg_parse_result {
157158
ACTION( REQ_REDIS_ZSCAN) \
158159
ACTION( REQ_REDIS_EVAL ) /* redis requests - eval */ \
159160
ACTION( REQ_REDIS_EVALSHA ) \
161+
ACTION( REQ_REDIS_PING ) /* redis requests - ping/quit */ \
162+
ACTION( REQ_REDIS_QUIT) \
160163
ACTION( RSP_REDIS_STATUS ) /* redis response */ \
161164
ACTION( RSP_REDIS_ERROR ) \
162165
ACTION( RSP_REDIS_INTEGER ) \
@@ -199,6 +202,7 @@ struct msg {
199202
msg_parse_result_t result; /* message parsing result */
200203

201204
msg_fragment_t fragment; /* message fragment */
205+
msg_reply_t reply; /* gen message reply (example: ping) */
202206
msg_coalesce_t pre_coalesce; /* message pre-coalesce */
203207
msg_coalesce_t post_coalesce; /* message post-coalesce */
204208

@@ -228,6 +232,7 @@ struct msg {
228232
unsigned request:1; /* request? or response? */
229233
unsigned quit:1; /* quit request? */
230234
unsigned noreply:1; /* noreply? */
235+
unsigned noforward:1; /* not need forward (example: ping) */
231236
unsigned done:1; /* done? */
232237
unsigned fdone:1; /* all fragments are done? */
233238
unsigned swallow:1; /* swallow response? */

src/nc_request.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,27 @@ req_recv_done(struct context *ctx, struct conn *conn, struct msg *msg,
586586
return;
587587
}
588588

589+
if (msg->noforward) {
590+
status = req_make_reply(ctx, conn, msg);
591+
if (status != NC_OK) {
592+
conn->err = errno;
593+
return;
594+
}
595+
596+
status = msg->reply(msg);
597+
if (status != NC_OK) {
598+
conn->err = errno;
599+
return;
600+
}
601+
602+
status = event_add_out(ctx->evb, conn);
603+
if (status != NC_OK) {
604+
conn->err = errno;
605+
}
606+
607+
return;
608+
}
609+
589610
/* do fragment */
590611
pool = conn->owner;
591612
TAILQ_INIT(&frag_msgq);

src/proto/nc_proto.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,6 @@ void redis_parse_rsp(struct msg *r);
149149
void redis_pre_coalesce(struct msg *r);
150150
void redis_post_coalesce(struct msg *r);
151151
rstatus_t redis_fragment(struct msg *r, uint32_t ncontinuum, struct msg_tqh *frag_msgq);
152+
rstatus_t redis_reply(struct msg *r);
152153

153154
#endif

src/proto/nc_redis.c

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@
2121
#include <nc_core.h>
2222
#include <nc_proto.h>
2323

24+
/*
25+
* Return true, if the redis command take no key, otherwise
26+
* return false
27+
*/
28+
static bool
29+
redis_argz(struct msg *r)
30+
{
31+
switch (r->type) {
32+
case MSG_REQ_REDIS_PING:
33+
case MSG_REQ_REDIS_QUIT:
34+
return true;
35+
36+
default:
37+
break;
38+
}
39+
40+
return false;
41+
}
42+
2443
/*
2544
* Return true, if the redis command accepts no arguments, otherwise
2645
* return false
@@ -594,6 +613,18 @@ redis_parse_req(struct msg *r)
594613
break;
595614
}
596615

616+
if (str4icmp(m, 'p', 'i', 'n', 'g')) {
617+
r->type = MSG_REQ_REDIS_PING;
618+
r->noforward = 1;
619+
break;
620+
}
621+
622+
if (str4icmp(m, 'q', 'u', 'i', 't')) {
623+
r->type = MSG_REQ_REDIS_QUIT;
624+
r->quit = 1;
625+
break;
626+
}
627+
597628
break;
598629

599630
case 5:
@@ -1018,7 +1049,9 @@ redis_parse_req(struct msg *r)
10181049
case SW_REQ_TYPE_LF:
10191050
switch (ch) {
10201051
case LF:
1021-
if (redis_argeval(r)) {
1052+
if (redis_argz(r)) {
1053+
goto done;
1054+
} else if (redis_argeval(r)) {
10221055
state = SW_ARG1_LEN;
10231056
} else {
10241057
state = SW_KEY_LEN;
@@ -2417,6 +2450,23 @@ redis_fragment(struct msg *r, uint32_t ncontinuum, struct msg_tqh *frag_msgq)
24172450
}
24182451
}
24192452

2453+
rstatus_t
2454+
redis_reply(struct msg *r)
2455+
{
2456+
struct msg *response = r->peer;
2457+
2458+
ASSERT(response != NULL);
2459+
2460+
switch (r->type) {
2461+
case MSG_REQ_REDIS_PING:
2462+
return msg_append(response, (uint8_t *)"+PONG\r\n", 7);
2463+
2464+
default:
2465+
NOT_REACHED();
2466+
return NC_ERROR;
2467+
}
2468+
}
2469+
24202470
void
24212471
redis_post_coalesce_mset(struct msg *request)
24222472
{

0 commit comments

Comments
 (0)