summaryrefslogtreecommitdiff
path: root/src/networking.c
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2017-12-05 15:59:56 +0100
committerantirez <antirez@gmail.com>2017-12-05 16:02:03 +0100
commit62a4b817c6e83eedf96a451f45dd943099258fd0 (patch)
tree2d3467ba1e00da56e4922b09fde289253470101d /src/networking.c
parent03cfc8bf3ad3a172bd5c7f5a02ad7757c0c3f96d (diff)
downloadredis-62a4b817c6e83eedf96a451f45dd943099258fd0.tar.gz
add linkClient(): adds the client and caches the list node.
We have this operation in two places: when caching the master and when linking a new client after the client creation. By having an API for this we avoid incurring in errors when modifying one of the two places forgetting the other. The function is also a good place where to document why we cache the linked list node. Related to #4497 and #4210.
Diffstat (limited to 'src/networking.c')
-rw-r--r--src/networking.c18
1 files changed, 12 insertions, 6 deletions
diff --git a/src/networking.c b/src/networking.c
index b1235ed41..3f880fe06 100644
--- a/src/networking.c
+++ b/src/networking.c
@@ -67,6 +67,16 @@ int listMatchObjects(void *a, void *b) {
return equalStringObjects(a,b);
}
+/* This function links the client to the global linked list of clients.
+ * unlinkClient() does the opposite, among other things. */
+void linkClient(client *c) {
+ listAddNodeTail(server.clients,c);
+ /* Note that we remember the linked list node where the client is stored,
+ * 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);
+}
+
client *createClient(int fd) {
client *c = zmalloc(sizeof(client));
@@ -134,14 +144,10 @@ client *createClient(int fd) {
c->pubsub_channels = dictCreate(&objectKeyPointerValueDictType,NULL);
c->pubsub_patterns = listCreate();
c->peerid = NULL;
+ c->client_list_node = NULL;
listSetFreeMethod(c->pubsub_patterns,decrRefCountVoid);
listSetMatchMethod(c->pubsub_patterns,listMatchObjects);
- if (fd != -1) {
- listAddNodeTail(server.clients,c);
- c->client_list_node = listLast(server.clients);
- } else {
- c->client_list_node = NULL;
- }
+ if (fd != -1) linkClient(c);
initClientMultiState(c);
return c;
}