Permalink
Browse files

Added asynchronous reconnect, fixed memory leak.

  • Loading branch information...
1 parent a91b7b1 commit 513982d8deed48475875bf52e3a79b742fb37732 @nicolasff committed Apr 12, 2011
Showing with 42 additions and 3 deletions.
  1. +3 −0 cmd.c
  2. +2 −0 formats/common.c
  3. +37 −3 pool.c
View
3 cmd.c
@@ -168,6 +168,7 @@ cmd_run(struct worker *w, struct http_client *client,
/* check that the client is able to run this command */
if(!acl_allow_command(cmd, w->s->cfg, client)) {
+ cmd_free(cmd);
return CMD_ACL_FAIL;
}
@@ -182,6 +183,7 @@ cmd_run(struct worker *w, struct http_client *client,
/* no args (e.g. INFO command) */
if(!slash) {
if(!ac) {
+ cmd_free(cmd);
return CMD_REDIS_UNAVAIL;
}
redisAsyncCommandArgv(ac, f_format, cmd, 1,
@@ -219,6 +221,7 @@ cmd_run(struct worker *w, struct http_client *client,
return CMD_SENT;
}
/* failed to find a suitable connection to Redis. */
+ cmd_free(cmd);
return CMD_REDIS_UNAVAIL;
}
View
@@ -44,6 +44,8 @@ format_send_error(struct cmd *cmd, short code, const char *msg) {
http_response_set_header(&resp, "Connection", "Close");
}
http_response_write(&resp, cmd->fd);
+
+ cmd_free(cmd);
}
void
View
40 pool.c
@@ -40,6 +40,41 @@ pool_on_connect(const redisAsyncContext *ac) {
}
}
+struct pool_reconnect {
+ struct event ev;
+ struct pool *p;
+
+ struct timeval tv;
+};
+
+static void
+pool_can_connect(int fd, short event, void *ptr) {
+ struct pool_reconnect *pr = ptr;
+ struct pool *p = pr->p;
+
+ (void)fd;
+ (void)event;
+
+ free(pr);
+
+ pool_connect(p, 1);
+}
+static void
+pool_schedule_reconnect(struct pool *p) {
+
+ struct pool_reconnect *pr = malloc(sizeof(struct pool_reconnect));
+ pr->p = p;
+
+ pr->tv.tv_sec = 0;
+ pr->tv.tv_usec = 100*1000; /* 0.1 sec*/
+
+ evtimer_set(&pr->ev, pool_can_connect, pr);
+ event_base_set(p->w->base, &pr->ev);
+ evtimer_add(&pr->ev, &pr->tv);
+}
+
+
+
static void
pool_on_disconnect(const redisAsyncContext *ac, int status) {
@@ -61,9 +96,8 @@ pool_on_disconnect(const redisAsyncContext *ac, int status) {
}
}
- /* reconnect */
- /* FIXME: schedule reconnect */
- pool_connect(p, 1);
+ /* schedule reconnect */
+ pool_schedule_reconnect(p);
}
/**

0 comments on commit 513982d

Please sign in to comment.