Skip to content

Commit af7db20

Browse files
committed
Reuse a single redis connection
1 parent d71769d commit af7db20

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

redis/hiredis.c

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ typedef struct {
5656
hiredis_refdb_backend *backend;
5757
} hiredis_refdb_iterator;
5858

59+
static redisContext *sharedConnection = NULL;
60+
5961
/* Odb methods */
6062

6163
int hiredis_odb_backend__read_header(size_t *len_p, git_otype *type_p, git_odb_backend *_backend, const git_oid *oid)
@@ -495,22 +497,26 @@ int git_odb_backend_hiredis(git_odb_backend **backend_out, const char* prefix, c
495497
if (backend == NULL)
496498
return GITERR_NOMEMORY;
497499

498-
backend->db = redisConnect(host, port);
499-
if (backend->db->err) {
500-
free(backend);
501-
giterr_set_str(GITERR_REFERENCE, "Redis odb storage couldn't connect to redis server");
502-
return GIT_ERROR;
503-
}
504-
505-
if(password != NULL) {
506-
reply = redisCommand(backend->db, "AUTH %s", password);
507-
if (reply->type == REDIS_REPLY_ERROR) {
508-
giterr_set_str(GITERR_REFERENCE, "Redis odb storage authentication with redis server failed");
500+
if (sharedConnection == NULL) {
501+
sharedConnection = redisConnect(host, port);
502+
if (sharedConnection->err) {
503+
free(backend);
504+
giterr_set_str(GITERR_REFERENCE, "Redis odb storage couldn't connect to redis server");
509505
return GIT_ERROR;
510506
}
511-
freeReplyObject(reply);
507+
508+
if(password != NULL) {
509+
reply = redisCommand(sharedConnection, "AUTH %s", password);
510+
if (reply->type == REDIS_REPLY_ERROR) {
511+
giterr_set_str(GITERR_REFERENCE, "Redis odb storage authentication with redis server failed");
512+
return GIT_ERROR;
513+
}
514+
freeReplyObject(reply);
515+
}
512516
}
513517

518+
backend->db = sharedConnection;
519+
514520
backend->prefix = prefix;
515521
backend->repo_path = path;
516522

@@ -540,22 +546,26 @@ int git_refdb_backend_hiredis(git_refdb_backend **backend_out, const char* prefi
540546
if (backend == NULL)
541547
return GITERR_NOMEMORY;
542548

543-
backend-> db = redisConnect(host, port);
544-
if (backend->db->err) {
545-
free(backend);
546-
giterr_set_str(GITERR_REFERENCE, "Redis refdb storage couldn't connect to redis server");
547-
return GIT_ERROR;
548-
}
549-
550-
if(password != NULL) {
551-
reply = redisCommand(backend->db, "AUTH %s", password);
552-
if (reply->type == REDIS_REPLY_ERROR) {
553-
giterr_set_str(GITERR_REFERENCE, "Redis refdb storage authentication with redis server failed");
549+
if (sharedConnection == NULL) {
550+
sharedConnection = redisConnect(host, port);
551+
if (sharedConnection->err) {
552+
free(backend);
553+
giterr_set_str(GITERR_REFERENCE, "Redis refdb storage couldn't connect to redis server");
554554
return GIT_ERROR;
555555
}
556-
freeReplyObject(reply);
556+
557+
if(password != NULL) {
558+
reply = redisCommand(sharedConnection, "AUTH %s", password);
559+
if (reply->type == REDIS_REPLY_ERROR) {
560+
giterr_set_str(GITERR_REFERENCE, "Redis refdb storage authentication with redis server failed");
561+
return GIT_ERROR;
562+
}
563+
freeReplyObject(reply);
564+
}
557565
}
558566

567+
backend->db = sharedConnection;
568+
559569
backend->prefix = prefix;
560570
backend->repo_path = path;
561571

0 commit comments

Comments
 (0)