Skip to content

Commit

Permalink
Allow to connect using a unix socket from hiredis
Browse files Browse the repository at this point in the history
  • Loading branch information
pietern committed Nov 3, 2010
1 parent 0ccb2c8 commit f25a526
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
7 changes: 7 additions & 0 deletions async.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ redisAsyncContext *redisAsyncConnect(const char *ip, int port) {
return ac;
}

redisAsyncContext *redisAsyncConnectUnix(const char *path) {
redisContext *c = redisConnectUnixNonBlock(path);
redisAsyncContext *ac = redisAsyncInitialize(c);
__redisAsyncCopyError(ac);
return ac;
}

int redisAsyncSetReplyObjectFunctions(redisAsyncContext *ac, redisReplyObjectFunctions *fn) {
redisContext *c = &(ac->c);
return redisSetReplyObjectFunctions(c,fn);
Expand Down
16 changes: 16 additions & 0 deletions hiredis.c
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,22 @@ redisContext *redisConnectNonBlock(const char *ip, int port) {
return c;
}

redisContext *redisConnectUnix(const char *path) {
redisContext *c = redisContextInit();
c->flags |= REDIS_BLOCK;
c->flags |= REDIS_CONNECTED;
redisContextConnectUnix(c,path);
return c;
}

redisContext *redisConnectUnixNonBlock(const char *path) {
redisContext *c = redisContextInit();
c->flags &= ~REDIS_BLOCK;
c->flags |= REDIS_CONNECTED;
redisContextConnectUnix(c,path);
return c;
}

/* Set the replyObjectFunctions to use. Returns REDIS_ERR when the reader
* was already initialized and the function set could not be re-set.
* Return REDIS_OK when they could be set. */
Expand Down
2 changes: 2 additions & 0 deletions hiredis.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ int redisFormatCommandArgv(char **target, int argc, const char **argv, const siz

redisContext *redisConnect(const char *ip, int port);
redisContext *redisConnectNonBlock(const char *ip, int port);
redisContext *redisConnectUnix(const char *path);
redisContext *redisConnectUnixNonBlock(const char *path);
int redisSetReplyObjectFunctions(redisContext *c, redisReplyObjectFunctions *fn);
void redisFree(redisContext *c);
int redisBufferRead(redisContext *c);
Expand Down
29 changes: 22 additions & 7 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ static long long usec(void) {
return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
}

static int use_unix = 0;
static redisContext *blocking_context = NULL;
static void __connect(redisContext **target) {
*target = blocking_context = redisConnect((char*)"127.0.0.1", 6379);
*target = blocking_context = (use_unix ?
redisConnectUnix("/tmp/redis.sock") : redisConnect((char*)"127.0.0.1", 6379));
if (blocking_context->err) {
printf("Connection error: %s\n", blocking_context->errstr);
exit(1);
Expand Down Expand Up @@ -77,10 +79,22 @@ static void test_blocking_connection() {
__connect(&c);
test("Returns I/O error when the connection is lost: ");
reply = redisCommand(c,"QUIT");
test_cond(strcasecmp(reply->str,"OK") == 0 &&
redisCommand(c,"PING") == NULL &&
c->err == REDIS_ERR_EOF &&
strcmp(c->errstr,"Server closed the connection") == 0);
test_cond(strcasecmp(reply->str,"OK") == 0 && redisCommand(c,"PING") == NULL);

/* Two conditions may happen, depending on the type of connection.
* When connected via TCP, the socket will not yet be aware of the closed
* connection and the write(2) call will succeed, but the read(2) will
* result in an EOF. When connected via Unix sockets, the socket will be
* immediately aware that it was closed and fail on the write(2) call. */
if (use_unix) {
fprintf(stderr,"Error: %s\n", c->errstr);
assert(c->err == REDIS_ERR_IO &&
strcmp(c->errstr,"Broken pipe") == 0);
} else {
fprintf(stderr,"Error: %s\n", c->errstr);
assert(c->err == REDIS_ERR_EOF &&
strcmp(c->errstr,"Server closed the connection") == 0);
}
freeReplyObject(reply);
redisFree(c);

Expand Down Expand Up @@ -210,15 +224,15 @@ static void test_throughput() {

replies = malloc(sizeof(redisReply*)*1000);
t1 = usec();
for (i = 0; i < 1000; i++) replies[i] = redisCommand(c,"PING");
for (i = 0; i < 1000; i++) assert((replies[i] = redisCommand(c,"PING")) != NULL);
t2 = usec();
for (i = 0; i < 1000; i++) freeReplyObject(replies[i]);
free(replies);
printf("\t(1000x PING: %.2fs)\n", (t2-t1)/1000000.0);

replies = malloc(sizeof(redisReply*)*1000);
t1 = usec();
for (i = 0; i < 1000; i++) replies[i] = redisCommand(c,"LRANGE mylist 0 499");
for (i = 0; i < 1000; i++) assert((replies[i] = redisCommand(c,"LRANGE mylist 0 499")) != NULL);
t2 = usec();
for (i = 0; i < 1000; i++) freeReplyObject(replies[i]);
free(replies);
Expand Down Expand Up @@ -337,6 +351,7 @@ static void cleanup() {
// }

int main(void) {
signal(SIGPIPE, SIG_IGN);
test_format_commands();
test_blocking_connection();
test_reply_reader();
Expand Down

0 comments on commit f25a526

Please sign in to comment.