Permalink
Browse files

updated pubsub with json stats, parse query args before accessing, ac…

…cept * for stats cb
  • Loading branch information...
1 parent b6ff617 commit 076e8dff72076247b54ff7979827c714b7960615 @mreiferson mreiferson committed Feb 1, 2011
Showing with 35 additions and 27 deletions.
  1. +23 −13 pubsub/pubsub.c
  2. +3 −4 pubsub_filtered/pubsub_filtered.c
  3. +1 −2 simplequeue/simplequeue.c
  4. +3 −3 simpletokyo/simpletokyo.c
  5. +5 −5 sortdb/sortdb.c
View
36 pubsub/pubsub.c
@@ -113,12 +113,9 @@ void
stats_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
{
struct evkeyvalq args;
- char *reset, *uri;
char buf[33];
-
- uri = evhttp_decode_uri(req->uri);
- evhttp_parse_query(uri, &args);
- free(uri);
+ const char *reset;
+ const char *format;
sprintf(buf, "%llu", totalConns);
evhttp_add_header(req->output_headers, "X-PUBSUB-TOTAL-CONNECTIONS", buf);
@@ -131,18 +128,31 @@ stats_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
sprintf(buf, "%llu", kickedClients);
evhttp_add_header(req->output_headers, "X-PUBSUB-KICKED-CLIENTS", buf);
- evbuffer_add_printf(evb, "Active connections: %llu\n", currentConns);
- evbuffer_add_printf(evb, "Total connections: %llu\n", totalConns);
- evbuffer_add_printf(evb, "Messages received: %llu\n", msgRecv);
- evbuffer_add_printf(evb, "Messages sent: %llu\n", msgSent);
- evbuffer_add_printf(evb, "Kicked clients: %llu\n", kickedClients);
+ evhttp_parse_query(req->uri, &args);
+ format = (char *)evhttp_find_header(&args, "format");
+
+ if ((format != NULL) && (strcmp(format, "json") == 0)) {
+ evbuffer_add_printf(evb, "{");
+ evbuffer_add_printf(evb, "\"current_connections\": %llu,", currentConns);
+ evbuffer_add_printf(evb, "\"total_connections\": %llu,", totalConns);
+ evbuffer_add_printf(evb, "\"messages_received\": %llu,", msgRecv);
+ evbuffer_add_printf(evb, "\"messages_sent\": %llu,", msgSent);
+ evbuffer_add_printf(evb, "\"kicked_clients\": %llu,", kickedClients);
+ evbuffer_add_printf(evb, "}\n");
+ } else {
+ evbuffer_add_printf(evb, "Active connections: %llu\n", currentConns);
+ evbuffer_add_printf(evb, "Total connections: %llu\n", totalConns);
+ evbuffer_add_printf(evb, "Messages received: %llu\n", msgRecv);
+ evbuffer_add_printf(evb, "Messages sent: %llu\n", msgSent);
+ evbuffer_add_printf(evb, "Kicked clients: %llu\n", kickedClients);
+ }
+
reset = (char *)evhttp_find_header(&args, "reset");
-
if (reset) {
msgRecv = 0;
msgSent = 0;
}
-
+
evhttp_send_reply(req, HTTP_OK, "OK", evb);
evhttp_clear_headers(&args);
}
@@ -299,7 +309,7 @@ main(int argc, char **argv)
simplehttp_init();
simplehttp_set_cb("/pub*", pub_cb, NULL);
simplehttp_set_cb("/sub*", sub_cb, NULL);
- simplehttp_set_cb("/stats", stats_cb, NULL);
+ simplehttp_set_cb("/stats*", stats_cb, NULL);
simplehttp_set_cb("/clients", clients_cb, NULL);
simplehttp_main(argc, argv);
View
7 pubsub_filtered/pubsub_filtered.c
@@ -345,7 +345,6 @@ clients_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
void
stats_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
{
- char *reset, *uri;
char buf[33];
struct evkeyvalq args;
const char *format;
@@ -361,6 +360,7 @@ stats_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
sprintf(buf, "%llu", kickedClients);
evhttp_add_header(req->output_headers, "X-PUBSUB-KICKED-CLIENTS", buf);
+ evhttp_parse_query(req->uri, &args);
format = (char *)evhttp_find_header(&args, "format");
if ((format != NULL) && (strcmp(format, "json") == 0)) {
@@ -381,9 +381,8 @@ stats_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
evbuffer_add_printf(evb, "Reconnects: %llu\n", number_reconnects);
}
- evhttp_clear_headers(&args);
-
evhttp_send_reply(req, HTTP_OK, "OK", evb);
+ evhttp_clear_headers(&args);
}
@@ -608,7 +607,7 @@ main(int argc, char **argv)
TAILQ_INIT(&clients);
simplehttp_init();
simplehttp_set_cb("/sub", sub_cb, NULL);
- simplehttp_set_cb("/stats", stats_cb, NULL);
+ simplehttp_set_cb("/stats*", stats_cb, NULL);
simplehttp_set_cb("/clients", clients_cb, NULL);
if (connect_to_source() == FAILURE) {
View
3 simplequeue/simplequeue.c
@@ -96,9 +96,8 @@ stats(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
}
}
- evhttp_clear_headers(&args);
-
evhttp_send_reply(req, HTTP_OK, "OK", evb);
+ evhttp_clear_headers(&args);
}
void
View
6 simpletokyo/simpletokyo.c
@@ -416,6 +416,7 @@ void stats_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
struct evkeyvalq args;
const char *format;
+ evhttp_parse_query(req->uri, &args);
format = (char *)evhttp_find_header(&args, "format");
if ((format != NULL) && (strcmp(format, "json") == 0)) {
@@ -442,9 +443,8 @@ void stats_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
evbuffer_add_printf(evb, "db opens: %llu\n", (long long unsigned int)db_opened);
}
- evhttp_clear_headers(&args);
-
evhttp_send_reply(req, HTTP_OK, "OK", evb);
+ evhttp_clear_headers(&args);
}
void exit_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx) {
@@ -500,7 +500,7 @@ main(int argc, char **argv)
simplehttp_set_cb("/vanish*", vanish_cb, NULL);
simplehttp_set_cb("/fwmatch*", fwmatch_cb, NULL);
simplehttp_set_cb("/incr*", incr_cb, NULL);
- simplehttp_set_cb("/stats", stats_cb, NULL);
+ simplehttp_set_cb("/stats*", stats_cb, NULL);
simplehttp_set_cb("/exit", exit_cb, NULL);
simplehttp_main(argc, argv);
View
10 sortdb/sortdb.c
@@ -191,8 +191,8 @@ void get_cb(struct evhttp_request *req, struct evbuffer *evb,void *ctx)
void stats_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
{
- long long unsigned int request_total;
- long long unsigned int average_request;
+ long long unsigned int request_total;
+ long long unsigned int average_request = 0;
int i, j, c, request_array_end;
struct evkeyvalq args;
const char *format;
@@ -212,6 +212,7 @@ void stats_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
}
}
+ evhttp_parse_query(req->uri, &args);
format = (char *)evhttp_find_header(&args, "format");
if ((format != NULL) && (strcmp(format, "json") == 0)) {
@@ -230,9 +231,8 @@ void stats_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
evbuffer_add_printf(evb, "Avg. request (usec): %llu\n", average_request);
}
- evhttp_clear_headers(&args);
-
evhttp_send_reply(req, HTTP_OK, "OK", evb);
+ evhttp_clear_headers(&args);
}
void reload_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
@@ -355,7 +355,7 @@ int main(int argc, char **argv)
simplehttp_init();
signal(SIGHUP, hup_handler);
simplehttp_set_cb("/get?*", get_cb, NULL);
- simplehttp_set_cb("/stats", stats_cb, NULL);
+ simplehttp_set_cb("/stats*", stats_cb, NULL);
simplehttp_set_cb("/reload", reload_cb, NULL);
simplehttp_set_cb("/exit", exit_cb, NULL);
simplehttp_main(argc, argv);

0 comments on commit 076e8df

Please sign in to comment.