diff --git a/src/networking.c b/src/networking.c index cfe23c6c..cbbad541 100644 --- a/src/networking.c +++ b/src/networking.c @@ -75,6 +75,8 @@ void linkClient(client *c) { * this way removing the client in unlinkClient() will not require * a linear scan, but just a constant time operation. */ c->client_list_node = listLast(server.clients); + uint64_t id = htonu64(c->id); + raxInsert(server.clients_index,(unsigned char*)&id,sizeof(id),c,NULL); } client *createClient(int fd) { @@ -720,6 +722,8 @@ void unlinkClient(client *c) { if (c->fd != -1) { /* Remove from the list of active clients. */ if (c->client_list_node) { + uint64_t id = htonu64(c->id); + raxRemove(server.clients_index,(unsigned char*)&id,sizeof(id),NULL); listDelNode(server.clients,c->client_list_node); c->client_list_node = NULL; } diff --git a/src/server.c b/src/server.c index 3ac1278a..5f779d8d 100644 --- a/src/server.c +++ b/src/server.c @@ -1890,6 +1890,7 @@ void initServer(void) { server.pid = getpid(); server.current_client = NULL; server.clients = listCreate(); + server.clients_index = raxNew(); server.clients_to_close = listCreate(); server.slaves = listCreate(); server.monitors = listCreate(); diff --git a/src/server.h b/src/server.h index a130d5b1..feccc7aa 100644 --- a/src/server.h +++ b/src/server.h @@ -952,6 +952,7 @@ struct redisServer { list *clients_pending_write; /* There is to write or install handler. */ list *slaves, *monitors; /* List of slaves and MONITORs */ client *current_client; /* Current client, only used on crash report */ + rax *clients_index; /* Active clients dictionary by client ID. */ int clients_paused; /* True if clients are currently paused */ mstime_t clients_pause_end_time; /* Time when we undo clients_paused */ char neterr[ANET_ERR_LEN]; /* Error buffer for anet.c */