4 void *dupClientReplyValue(void *o
) {
5 incrRefCount((robj
*)o
);
9 int listMatchObjects(void *a
, void *b
) {
10 return equalStringObjects(a
,b
);
13 redisClient
*createClient(int fd
) {
16 /* Make sure to allocate a multiple of the page size to prevent wasting
17 * memory. A page size of 4096 is assumed here. We need to compensate
18 * for the zmalloc overhead of sizeof(size_t) bytes. */
19 size_t size
= 8192-sizeof(size_t);
20 redisAssert(size
> sizeof(redisClient
));
22 c
->buflen
= size
-sizeof(redisClient
);
25 anetNonBlock(NULL
,fd
);
26 anetTcpNoDelay(NULL
,fd
);
30 c
->querybuf
= sdsempty();
39 c
->lastinteraction
= time(NULL
);
41 c
->replstate
= REDIS_REPL_NONE
;
42 c
->reply
= listCreate();
43 listSetFreeMethod(c
->reply
,decrRefCount
);
44 listSetDupMethod(c
->reply
,dupClientReplyValue
);
45 c
->blocking_keys
= NULL
;
46 c
->blocking_keys_num
= 0;
47 c
->io_keys
= listCreate();
48 c
->watched_keys
= listCreate();
49 listSetFreeMethod(c
->io_keys
,decrRefCount
);
50 c
->pubsub_channels
= dictCreate(&setDictType
,NULL
);
51 c
->pubsub_patterns
= listCreate();
52 listSetFreeMethod(c
->pubsub_patterns
,decrRefCount
);
53 listSetMatchMethod(c
->pubsub_patterns
,listMatchObjects
);
54 if (aeCreateFileEvent(server
.el
, c
->fd
, AE_READABLE
,
55 readQueryFromClient
, c
) == AE_ERR
) {
59 listAddNodeTail(server
.clients
,c
);
60 initClientMultiState(c
);
64 int _ensureFileEvent(redisClient
*c
) {
65 if (c
->fd
<= 0) return REDIS_ERR
;
66 if (c
->bufpos
== 0 && listLength(c
->reply
) == 0 &&
67 (c
->replstate
== REDIS_REPL_NONE
||
68 c
->replstate
== REDIS_REPL_ONLINE
) &&
69 aeCreateFileEvent(server
.el
, c
->fd
, AE_WRITABLE
,
70 sendReplyToClient
, c
) == AE_ERR
) return REDIS_ERR
;
74 void _addReplyObjectToList(redisClient
*c
, robj
*obj
) {
75 redisAssert(obj
->type
== REDIS_STRING
&&
76 obj
->encoding
== REDIS_ENCODING_RAW
);
77 listAddNodeTail(c
->reply
,obj
);
80 void _ensureBufferInReplyList(redisClient
*c
) {
81 sds buffer
= sdsnewlen(NULL
,REDIS_REPLY_CHUNK_SIZE
);
82 sdsupdatelen(buffer
); /* sdsnewlen expects non-empty string */
83 listAddNodeTail(c
->reply
,createObject(REDIS_REPLY_NODE
,buffer
));
86 void _addReplyStringToBuffer(redisClient
*c
, char *s
, size_t len
) {
88 redisAssert(len
< REDIS_REPLY_CHUNK_THRESHOLD
);
89 if (listLength(c
->reply
) > 0) {
90 robj
*o
= listNodeValue(listLast(c
->reply
));
92 /* Make sure to append to a reply node with enough bytes available. */
93 if (o
->type
== REDIS_REPLY_NODE
) available
= sdsavail(o
->ptr
);
94 if (o
->type
!= REDIS_REPLY_NODE
|| len
> available
) {
95 _ensureBufferInReplyList(c
);
96 _addReplyStringToBuffer(c
,s
,len
);
98 o
->ptr
= sdscatlen(o
->ptr
,s
,len
);
101 available
= c
->buflen
-c
->bufpos
;
102 if (len
> available
) {
103 _ensureBufferInReplyList(c
);
104 _addReplyStringToBuffer(c
,s
,len
);
106 memcpy(c
->buf
+c
->bufpos
,s
,len
);
112 void addReply(redisClient
*c
, robj
*obj
) {
113 if (_ensureFileEvent(c
) != REDIS_OK
) return;
114 if (server
.vm_enabled
&& obj
->storage
!= REDIS_VM_MEMORY
) {
115 /* Returns a new object with refcount 1 */
116 obj
= dupStringObject(obj
);
118 /* This increments the refcount. */
119 obj
= getDecodedObject(obj
);
122 if (sdslen(obj
->ptr
) < REDIS_REPLY_CHUNK_THRESHOLD
) {
123 _addReplyStringToBuffer(c
,obj
->ptr
,sdslen(obj
->ptr
));
126 _addReplyObjectToList(c
,obj
);
130 void addReplySds(redisClient
*c
, sds s
) {
131 if (_ensureFileEvent(c
) != REDIS_OK
) {
132 /* The caller expects the sds to be free'd. */
136 if (sdslen(s
) < REDIS_REPLY_CHUNK_THRESHOLD
) {
137 _addReplyStringToBuffer(c
,s
,sdslen(s
));
140 _addReplyObjectToList(c
,createObject(REDIS_STRING
,s
));
144 void addReplyString(redisClient
*c
, char *s
, size_t len
) {
145 if (_ensureFileEvent(c
) != REDIS_OK
) return;
146 if (len
< REDIS_REPLY_CHUNK_THRESHOLD
) {
147 _addReplyStringToBuffer(c
,s
,len
);
149 _addReplyObjectToList(c
,createStringObject(s
,len
));
153 /* Adds an empty object to the reply list that will contain the multi bulk
154 * length, which is not known when this function is called. */
155 void *addDeferredMultiBulkLength(redisClient
*c
) {
156 if (_ensureFileEvent(c
) != REDIS_OK
) return NULL
;
157 _addReplyObjectToList(c
,createObject(REDIS_STRING
,NULL
));
158 return listLast(c
->reply
);
161 /* Populate the length object and try glueing it to the next chunk. */
162 void setDeferredMultiBulkLength(redisClient
*c
, void *node
, long length
) {
163 listNode
*ln
= (listNode
*)node
;
166 /* Abort when *node is NULL (see addDeferredMultiBulkLength). */
167 if (node
== NULL
) return;
169 len
= listNodeValue(ln
);
170 len
->ptr
= sdscatprintf(sdsempty(),"*%ld\r\n",length
);
171 if (ln
->next
!= NULL
) {
172 next
= listNodeValue(ln
->next
);
173 /* Only glue when the next node is a reply chunk. */
174 if (next
->type
== REDIS_REPLY_NODE
) {
175 len
->ptr
= sdscatlen(len
->ptr
,next
->ptr
,sdslen(next
->ptr
));
176 listDelNode(c
->reply
,ln
->next
);
181 void addReplyDouble(redisClient
*c
, double d
) {
182 char dbuf
[128], sbuf
[128];
184 dlen
= snprintf(dbuf
,sizeof(dbuf
),"%.17g",d
);
185 slen
= snprintf(sbuf
,sizeof(sbuf
),"$%d\r\n%s\r\n",dlen
,dbuf
);
186 addReplyString(c
,sbuf
,slen
);
189 void _addReplyLongLong(redisClient
*c
, long long ll
, char prefix
) {
193 len
= ll2string(buf
+1,sizeof(buf
)-1,ll
);
196 addReplyString(c
,buf
,len
+3);
199 void addReplyLongLong(redisClient
*c
, long long ll
) {
200 _addReplyLongLong(c
,ll
,':');
203 void addReplyUlong(redisClient
*c
, unsigned long ul
) {
204 _addReplyLongLong(c
,(long long)ul
,':');
207 void addReplyMultiBulkLen(redisClient
*c
, long length
) {
208 _addReplyLongLong(c
,length
,'*');
211 void addReplyBulkLen(redisClient
*c
, robj
*obj
) {
214 if (obj
->encoding
== REDIS_ENCODING_RAW
) {
215 len
= sdslen(obj
->ptr
);
217 long n
= (long)obj
->ptr
;
219 /* Compute how many bytes will take this integer as a radix 10 string */
225 while((n
= n
/10) != 0) {
229 _addReplyLongLong(c
,len
,'$');
232 void addReplyBulk(redisClient
*c
, robj
*obj
) {
233 addReplyBulkLen(c
,obj
);
235 addReply(c
,shared
.crlf
);
238 /* In the CONFIG command we need to add vanilla C string as bulk replies */
239 void addReplyBulkCString(redisClient
*c
, char *s
) {
241 addReply(c
,shared
.nullbulk
);
243 robj
*o
= createStringObject(s
,strlen(s
));
249 void acceptHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
255 REDIS_NOTUSED(privdata
);
257 cfd
= anetAccept(server
.neterr
, fd
, cip
, &cport
);
259 redisLog(REDIS_VERBOSE
,"Accepting client connection: %s", server
.neterr
);
262 redisLog(REDIS_VERBOSE
,"Accepted %s:%d", cip
, cport
);
263 if ((c
= createClient(cfd
)) == NULL
) {
264 redisLog(REDIS_WARNING
,"Error allocating resoures for the client");
265 close(cfd
); /* May be already closed, just ingore errors */
268 /* If maxclient directive is set and this is one client more... close the
269 * connection. Note that we create the client instead to check before
270 * for this condition, since now the socket is already set in nonblocking
271 * mode and we can send an error for free using the Kernel I/O */
272 if (server
.maxclients
&& listLength(server
.clients
) > server
.maxclients
) {
273 char *err
= "-ERR max number of clients reached\r\n";
275 /* That's a best effort error message, don't check write errors */
276 if (write(c
->fd
,err
,strlen(err
)) == -1) {
277 /* Nothing to do, Just to avoid the warning... */
282 server
.stat_numconnections
++;
285 static void freeClientArgv(redisClient
*c
) {
288 for (j
= 0; j
< c
->argc
; j
++)
289 decrRefCount(c
->argv
[j
]);
290 for (j
= 0; j
< c
->mbargc
; j
++)
291 decrRefCount(c
->mbargv
[j
]);
296 void freeClient(redisClient
*c
) {
299 /* Note that if the client we are freeing is blocked into a blocking
300 * call, we have to set querybuf to NULL *before* to call
301 * unblockClientWaitingData() to avoid processInputBuffer() will get
302 * called. Also it is important to remove the file events after
303 * this, because this call adds the READABLE event. */
304 sdsfree(c
->querybuf
);
306 if (c
->flags
& REDIS_BLOCKED
)
307 unblockClientWaitingData(c
);
309 /* UNWATCH all the keys */
311 listRelease(c
->watched_keys
);
312 /* Unsubscribe from all the pubsub channels */
313 pubsubUnsubscribeAllChannels(c
,0);
314 pubsubUnsubscribeAllPatterns(c
,0);
315 dictRelease(c
->pubsub_channels
);
316 listRelease(c
->pubsub_patterns
);
317 /* Obvious cleanup */
318 aeDeleteFileEvent(server
.el
,c
->fd
,AE_READABLE
);
319 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
320 listRelease(c
->reply
);
323 /* Remove from the list of clients */
324 ln
= listSearchKey(server
.clients
,c
);
325 redisAssert(ln
!= NULL
);
326 listDelNode(server
.clients
,ln
);
327 /* Remove from the list of clients waiting for swapped keys, or ready
328 * to be restarted, but not yet woken up again. */
329 if (c
->flags
& REDIS_IO_WAIT
) {
330 redisAssert(server
.vm_enabled
);
331 if (listLength(c
->io_keys
) == 0) {
332 ln
= listSearchKey(server
.io_ready_clients
,c
);
334 /* When this client is waiting to be woken up (REDIS_IO_WAIT),
335 * it should be present in the list io_ready_clients */
336 redisAssert(ln
!= NULL
);
337 listDelNode(server
.io_ready_clients
,ln
);
339 while (listLength(c
->io_keys
)) {
340 ln
= listFirst(c
->io_keys
);
341 dontWaitForSwappedKey(c
,ln
->value
);
344 server
.vm_blocked_clients
--;
346 listRelease(c
->io_keys
);
347 /* Master/slave cleanup.
348 * Case 1: we lost the connection with a slave. */
349 if (c
->flags
& REDIS_SLAVE
) {
350 if (c
->replstate
== REDIS_REPL_SEND_BULK
&& c
->repldbfd
!= -1)
352 list
*l
= (c
->flags
& REDIS_MONITOR
) ? server
.monitors
: server
.slaves
;
353 ln
= listSearchKey(l
,c
);
354 redisAssert(ln
!= NULL
);
358 /* Case 2: we lost the connection with the master. */
359 if (c
->flags
& REDIS_MASTER
) {
360 server
.master
= NULL
;
361 server
.replstate
= REDIS_REPL_CONNECT
;
362 /* Since we lost the connection with the master, we should also
363 * close the connection with all our slaves if we have any, so
364 * when we'll resync with the master the other slaves will sync again
365 * with us as well. Note that also when the slave is not connected
366 * to the master it will keep refusing connections by other slaves. */
367 while (listLength(server
.slaves
)) {
368 ln
= listFirst(server
.slaves
);
369 freeClient((redisClient
*)ln
->value
);
375 freeClientMultiState(c
);
379 void sendReplyToClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
380 redisClient
*c
= privdata
;
381 int nwritten
= 0, totwritten
= 0, objlen
;
386 /* Use writev() if we have enough buffers to send */
387 if (!server
.glueoutputbuf
&&
388 listLength(c
->reply
) > REDIS_WRITEV_THRESHOLD
&&
389 !(c
->flags
& REDIS_MASTER
))
391 sendReplyToClientWritev(el
, fd
, privdata
, mask
);
395 while(c
->bufpos
> 0 || listLength(c
->reply
)) {
397 if (c
->flags
& REDIS_MASTER
) {
398 /* Don't reply to a master */
399 nwritten
= c
->bufpos
- c
->sentlen
;
401 nwritten
= write(fd
,c
->buf
+c
->sentlen
,c
->bufpos
-c
->sentlen
);
402 if (nwritten
<= 0) break;
404 c
->sentlen
+= nwritten
;
405 totwritten
+= nwritten
;
407 /* If the buffer was sent, set bufpos to zero to continue with
408 * the remainder of the reply. */
409 if (c
->sentlen
== c
->bufpos
) {
414 o
= listNodeValue(listFirst(c
->reply
));
415 objlen
= sdslen(o
->ptr
);
418 listDelNode(c
->reply
,listFirst(c
->reply
));
422 if (c
->flags
& REDIS_MASTER
) {
423 /* Don't reply to a master */
424 nwritten
= objlen
- c
->sentlen
;
426 nwritten
= write(fd
, ((char*)o
->ptr
)+c
->sentlen
,objlen
-c
->sentlen
);
427 if (nwritten
<= 0) break;
429 c
->sentlen
+= nwritten
;
430 totwritten
+= nwritten
;
432 /* If we fully sent the object on head go to the next one */
433 if (c
->sentlen
== objlen
) {
434 listDelNode(c
->reply
,listFirst(c
->reply
));
438 /* Note that we avoid to send more thank REDIS_MAX_WRITE_PER_EVENT
439 * bytes, in a single threaded server it's a good idea to serve
440 * other clients as well, even if a very large request comes from
441 * super fast link that is always able to accept data (in real world
442 * scenario think about 'KEYS *' against the loopback interfae) */
443 if (totwritten
> REDIS_MAX_WRITE_PER_EVENT
) break;
445 if (nwritten
== -1) {
446 if (errno
== EAGAIN
) {
449 redisLog(REDIS_VERBOSE
,
450 "Error writing to client: %s", strerror(errno
));
455 if (totwritten
> 0) c
->lastinteraction
= time(NULL
);
456 if (listLength(c
->reply
) == 0) {
458 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
462 void sendReplyToClientWritev(aeEventLoop
*el
, int fd
, void *privdata
, int mask
)
464 redisClient
*c
= privdata
;
465 int nwritten
= 0, totwritten
= 0, objlen
, willwrite
;
467 struct iovec iov
[REDIS_WRITEV_IOVEC_COUNT
];
473 while (listLength(c
->reply
)) {
478 /* fill-in the iov[] array */
479 for(node
= listFirst(c
->reply
); node
; node
= listNextNode(node
)) {
480 o
= listNodeValue(node
);
481 objlen
= sdslen(o
->ptr
);
483 if (totwritten
+ objlen
- offset
> REDIS_MAX_WRITE_PER_EVENT
)
486 if(ion
== REDIS_WRITEV_IOVEC_COUNT
)
487 break; /* no more iovecs */
489 iov
[ion
].iov_base
= ((char*)o
->ptr
) + offset
;
490 iov
[ion
].iov_len
= objlen
- offset
;
491 willwrite
+= objlen
- offset
;
492 offset
= 0; /* just for the first item */
499 /* write all collected blocks at once */
500 if((nwritten
= writev(fd
, iov
, ion
)) < 0) {
501 if (errno
!= EAGAIN
) {
502 redisLog(REDIS_VERBOSE
,
503 "Error writing to client: %s", strerror(errno
));
510 totwritten
+= nwritten
;
513 /* remove written robjs from c->reply */
514 while (nwritten
&& listLength(c
->reply
)) {
515 o
= listNodeValue(listFirst(c
->reply
));
516 objlen
= sdslen(o
->ptr
);
518 if(nwritten
>= objlen
- offset
) {
519 listDelNode(c
->reply
, listFirst(c
->reply
));
520 nwritten
-= objlen
- offset
;
524 c
->sentlen
+= nwritten
;
532 c
->lastinteraction
= time(NULL
);
534 if (listLength(c
->reply
) == 0) {
536 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
540 /* resetClient prepare the client to process the next command */
541 void resetClient(redisClient
*c
) {
547 void closeTimedoutClients(void) {
550 time_t now
= time(NULL
);
553 listRewind(server
.clients
,&li
);
554 while ((ln
= listNext(&li
)) != NULL
) {
555 c
= listNodeValue(ln
);
556 if (server
.maxidletime
&&
557 !(c
->flags
& REDIS_SLAVE
) && /* no timeout for slaves */
558 !(c
->flags
& REDIS_MASTER
) && /* no timeout for masters */
559 !(c
->flags
& REDIS_BLOCKED
) && /* no timeout for BLPOP */
560 dictSize(c
->pubsub_channels
) == 0 && /* no timeout for pubsub */
561 listLength(c
->pubsub_patterns
) == 0 &&
562 (now
- c
->lastinteraction
> server
.maxidletime
))
564 redisLog(REDIS_VERBOSE
,"Closing idle client");
566 } else if (c
->flags
& REDIS_BLOCKED
) {
567 if (c
->blockingto
!= 0 && c
->blockingto
< now
) {
568 addReply(c
,shared
.nullmultibulk
);
569 unblockClientWaitingData(c
);
575 void processInputBuffer(redisClient
*c
) {
577 /* Before to process the input buffer, make sure the client is not
578 * waitig for a blocking operation such as BLPOP. Note that the first
579 * iteration the client is never blocked, otherwise the processInputBuffer
580 * would not be called at all, but after the execution of the first commands
581 * in the input buffer the client may be blocked, and the "goto again"
582 * will try to reiterate. The following line will make it return asap. */
583 if (c
->flags
& REDIS_BLOCKED
|| c
->flags
& REDIS_IO_WAIT
) return;
584 if (c
->bulklen
== -1) {
585 /* Read the first line of the query */
586 char *p
= strchr(c
->querybuf
,'\n');
594 c
->querybuf
= sdsempty();
595 querylen
= 1+(p
-(query
));
596 if (sdslen(query
) > querylen
) {
597 /* leave data after the first line of the query in the buffer */
598 c
->querybuf
= sdscatlen(c
->querybuf
,query
+querylen
,sdslen(query
)-querylen
);
600 *p
= '\0'; /* remove "\n" */
601 if (*(p
-1) == '\r') *(p
-1) = '\0'; /* and "\r" if any */
604 /* Now we can split the query in arguments */
605 argv
= sdssplitlen(query
,sdslen(query
)," ",1,&argc
);
608 if (c
->argv
) zfree(c
->argv
);
609 c
->argv
= zmalloc(sizeof(robj
*)*argc
);
611 for (j
= 0; j
< argc
; j
++) {
612 if (sdslen(argv
[j
])) {
613 c
->argv
[c
->argc
] = createObject(REDIS_STRING
,argv
[j
]);
621 /* Execute the command. If the client is still valid
622 * after processCommand() return and there is something
623 * on the query buffer try to process the next command. */
624 if (processCommand(c
) && sdslen(c
->querybuf
)) goto again
;
626 /* Nothing to process, argc == 0. Just process the query
627 * buffer if it's not empty or return to the caller */
628 if (sdslen(c
->querybuf
)) goto again
;
631 } else if (sdslen(c
->querybuf
) >= REDIS_REQUEST_MAX_SIZE
) {
632 redisLog(REDIS_VERBOSE
, "Client protocol error");
637 /* Bulk read handling. Note that if we are at this point
638 the client already sent a command terminated with a newline,
639 we are reading the bulk data that is actually the last
640 argument of the command. */
641 int qbl
= sdslen(c
->querybuf
);
643 if (c
->bulklen
<= qbl
) {
644 /* Copy everything but the final CRLF as final argument */
645 c
->argv
[c
->argc
] = createStringObject(c
->querybuf
,c
->bulklen
-2);
647 c
->querybuf
= sdsrange(c
->querybuf
,c
->bulklen
,-1);
648 /* Process the command. If the client is still valid after
649 * the processing and there is more data in the buffer
650 * try to parse it. */
651 if (processCommand(c
) && sdslen(c
->querybuf
)) goto again
;
657 void readQueryFromClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
658 redisClient
*c
= (redisClient
*) privdata
;
659 char buf
[REDIS_IOBUF_LEN
];
664 nread
= read(fd
, buf
, REDIS_IOBUF_LEN
);
666 if (errno
== EAGAIN
) {
669 redisLog(REDIS_VERBOSE
, "Reading from client: %s",strerror(errno
));
673 } else if (nread
== 0) {
674 redisLog(REDIS_VERBOSE
, "Client closed connection");
679 c
->querybuf
= sdscatlen(c
->querybuf
, buf
, nread
);
680 c
->lastinteraction
= time(NULL
);
684 processInputBuffer(c
);