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
) {
14 redisClient
*c
= zmalloc(sizeof(redisClient
));
17 anetNonBlock(NULL
,fd
);
18 anetTcpNoDelay(NULL
,fd
);
22 c
->querybuf
= sdsempty();
31 c
->lastinteraction
= time(NULL
);
33 c
->replstate
= REDIS_REPL_NONE
;
34 c
->reply
= listCreate();
35 listSetFreeMethod(c
->reply
,decrRefCount
);
36 listSetDupMethod(c
->reply
,dupClientReplyValue
);
37 c
->blocking_keys
= NULL
;
38 c
->blocking_keys_num
= 0;
39 c
->io_keys
= listCreate();
40 c
->watched_keys
= listCreate();
41 listSetFreeMethod(c
->io_keys
,decrRefCount
);
42 c
->pubsub_channels
= dictCreate(&setDictType
,NULL
);
43 c
->pubsub_patterns
= listCreate();
44 listSetFreeMethod(c
->pubsub_patterns
,decrRefCount
);
45 listSetMatchMethod(c
->pubsub_patterns
,listMatchObjects
);
46 if (aeCreateFileEvent(server
.el
, c
->fd
, AE_READABLE
,
47 readQueryFromClient
, c
) == AE_ERR
) {
51 listAddNodeTail(server
.clients
,c
);
52 initClientMultiState(c
);
56 int _ensureFileEvent(redisClient
*c
) {
57 if (c
->fd
<= 0) return REDIS_ERR
;
58 if (c
->bufpos
== 0 && listLength(c
->reply
) == 0 &&
59 (c
->replstate
== REDIS_REPL_NONE
||
60 c
->replstate
== REDIS_REPL_ONLINE
) &&
61 aeCreateFileEvent(server
.el
, c
->fd
, AE_WRITABLE
,
62 sendReplyToClient
, c
) == AE_ERR
) return REDIS_ERR
;
66 /* Create a duplicate of the last object in the reply list when
67 * it is not exclusively owned by the reply list. */
68 robj
*dupLastObjectIfNeeded(list
*reply
) {
71 redisAssert(listLength(reply
) > 0);
73 cur
= listNodeValue(ln
);
74 if (cur
->refcount
> 1) {
75 new = dupStringObject(cur
);
77 listNodeValue(ln
) = new;
79 return listNodeValue(ln
);
82 int _addReplyToBuffer(redisClient
*c
, char *s
, size_t len
) {
83 size_t available
= sizeof(c
->buf
)-c
->bufpos
;
85 /* If there already are entries in the reply list, we cannot
86 * add anything more to the static buffer. */
87 if (listLength(c
->reply
) > 0) return REDIS_ERR
;
89 /* Check that the buffer has enough space available for this string. */
90 if (len
> available
) return REDIS_ERR
;
92 memcpy(c
->buf
+c
->bufpos
,s
,len
);
97 void _addReplyObjectToList(redisClient
*c
, robj
*o
) {
99 if (listLength(c
->reply
) == 0) {
101 listAddNodeTail(c
->reply
,o
);
103 tail
= listNodeValue(listLast(c
->reply
));
105 /* Append to this object when possible. */
106 if (tail
->ptr
!= NULL
&&
107 sdslen(tail
->ptr
)+sdslen(o
->ptr
) <= REDIS_REPLY_CHUNK_BYTES
)
109 tail
= dupLastObjectIfNeeded(c
->reply
);
110 tail
->ptr
= sdscatlen(tail
->ptr
,o
->ptr
,sdslen(o
->ptr
));
113 listAddNodeTail(c
->reply
,o
);
118 /* This method takes responsibility over the sds. When it is no longer
119 * needed it will be free'd, otherwise it ends up in a robj. */
120 void _addReplySdsToList(redisClient
*c
, sds s
) {
122 if (listLength(c
->reply
) == 0) {
123 listAddNodeTail(c
->reply
,createObject(REDIS_STRING
,s
));
125 tail
= listNodeValue(listLast(c
->reply
));
127 /* Append to this object when possible. */
128 if (tail
->ptr
!= NULL
&&
129 sdslen(tail
->ptr
)+sdslen(s
) <= REDIS_REPLY_CHUNK_BYTES
)
131 tail
= dupLastObjectIfNeeded(c
->reply
);
132 tail
->ptr
= sdscatlen(tail
->ptr
,s
,sdslen(s
));
135 listAddNodeTail(c
->reply
,createObject(REDIS_STRING
,s
));
140 void _addReplyStringToList(redisClient
*c
, char *s
, size_t len
) {
142 if (listLength(c
->reply
) == 0) {
143 listAddNodeTail(c
->reply
,createStringObject(s
,len
));
145 tail
= listNodeValue(listLast(c
->reply
));
147 /* Append to this object when possible. */
148 if (tail
->ptr
!= NULL
&&
149 sdslen(tail
->ptr
)+len
<= REDIS_REPLY_CHUNK_BYTES
)
151 tail
= dupLastObjectIfNeeded(c
->reply
);
152 tail
->ptr
= sdscatlen(tail
->ptr
,s
,len
);
154 listAddNodeTail(c
->reply
,createStringObject(s
,len
));
159 void addReply(redisClient
*c
, robj
*obj
) {
160 if (_ensureFileEvent(c
) != REDIS_OK
) return;
161 if (server
.vm_enabled
&& obj
->storage
!= REDIS_VM_MEMORY
) {
162 /* Returns a new object with refcount 1 */
163 obj
= dupStringObject(obj
);
165 /* This increments the refcount. */
166 obj
= getDecodedObject(obj
);
168 if (_addReplyToBuffer(c
,obj
->ptr
,sdslen(obj
->ptr
)) != REDIS_OK
)
169 _addReplyObjectToList(c
,obj
);
173 void addReplySds(redisClient
*c
, sds s
) {
174 if (_ensureFileEvent(c
) != REDIS_OK
) {
175 /* The caller expects the sds to be free'd. */
179 if (_addReplyToBuffer(c
,s
,sdslen(s
)) == REDIS_OK
) {
182 /* This method free's the sds when it is no longer needed. */
183 _addReplySdsToList(c
,s
);
187 void addReplyString(redisClient
*c
, char *s
, size_t len
) {
188 if (_ensureFileEvent(c
) != REDIS_OK
) return;
189 if (_addReplyToBuffer(c
,s
,len
) != REDIS_OK
)
190 _addReplyStringToList(c
,s
,len
);
193 void _addReplyError(redisClient
*c
, char *s
, size_t len
) {
194 addReplyString(c
,"-ERR ",5);
195 addReplyString(c
,s
,len
);
196 addReplyString(c
,"\r\n",2);
199 void addReplyError(redisClient
*c
, char *err
) {
200 _addReplyError(c
,err
,strlen(err
));
203 void addReplyErrorFormat(redisClient
*c
, const char *fmt
, ...) {
206 sds s
= sdscatvprintf(sdsempty(),fmt
,ap
);
208 _addReplyError(c
,s
,sdslen(s
));
212 void _addReplyStatus(redisClient
*c
, char *s
, size_t len
) {
213 addReplyString(c
,"+",1);
214 addReplyString(c
,s
,len
);
215 addReplyString(c
,"\r\n",2);
218 void addReplyStatus(redisClient
*c
, char *status
) {
219 _addReplyStatus(c
,status
,strlen(status
));
222 void addReplyStatusFormat(redisClient
*c
, const char *fmt
, ...) {
225 sds s
= sdscatvprintf(sdsempty(),fmt
,ap
);
227 _addReplyStatus(c
,s
,sdslen(s
));
231 /* Adds an empty object to the reply list that will contain the multi bulk
232 * length, which is not known when this function is called. */
233 void *addDeferredMultiBulkLength(redisClient
*c
) {
234 if (_ensureFileEvent(c
) != REDIS_OK
) return NULL
;
235 listAddNodeTail(c
->reply
,createObject(REDIS_STRING
,NULL
));
236 return listLast(c
->reply
);
239 /* Populate the length object and try glueing it to the next chunk. */
240 void setDeferredMultiBulkLength(redisClient
*c
, void *node
, long length
) {
241 listNode
*ln
= (listNode
*)node
;
244 /* Abort when *node is NULL (see addDeferredMultiBulkLength). */
245 if (node
== NULL
) return;
247 len
= listNodeValue(ln
);
248 len
->ptr
= sdscatprintf(sdsempty(),"*%ld\r\n",length
);
249 if (ln
->next
!= NULL
) {
250 next
= listNodeValue(ln
->next
);
252 /* Only glue when the next node is non-NULL (an sds in this case) */
253 if (next
->ptr
!= NULL
) {
254 len
->ptr
= sdscatlen(len
->ptr
,next
->ptr
,sdslen(next
->ptr
));
255 listDelNode(c
->reply
,ln
->next
);
260 void addReplyDouble(redisClient
*c
, double d
) {
261 char dbuf
[128], sbuf
[128];
263 dlen
= snprintf(dbuf
,sizeof(dbuf
),"%.17g",d
);
264 slen
= snprintf(sbuf
,sizeof(sbuf
),"$%d\r\n%s\r\n",dlen
,dbuf
);
265 addReplyString(c
,sbuf
,slen
);
268 void _addReplyLongLong(redisClient
*c
, long long ll
, char prefix
) {
272 len
= ll2string(buf
+1,sizeof(buf
)-1,ll
);
275 addReplyString(c
,buf
,len
+3);
278 void addReplyLongLong(redisClient
*c
, long long ll
) {
279 _addReplyLongLong(c
,ll
,':');
282 void addReplyMultiBulkLen(redisClient
*c
, long length
) {
283 _addReplyLongLong(c
,length
,'*');
286 void addReplyBulkLen(redisClient
*c
, robj
*obj
) {
289 if (obj
->encoding
== REDIS_ENCODING_RAW
) {
290 len
= sdslen(obj
->ptr
);
292 long n
= (long)obj
->ptr
;
294 /* Compute how many bytes will take this integer as a radix 10 string */
300 while((n
= n
/10) != 0) {
304 _addReplyLongLong(c
,len
,'$');
307 void addReplyBulk(redisClient
*c
, robj
*obj
) {
308 addReplyBulkLen(c
,obj
);
310 addReply(c
,shared
.crlf
);
313 /* In the CONFIG command we need to add vanilla C string as bulk replies */
314 void addReplyBulkCString(redisClient
*c
, char *s
) {
316 addReply(c
,shared
.nullbulk
);
318 robj
*o
= createStringObject(s
,strlen(s
));
324 void acceptHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
330 REDIS_NOTUSED(privdata
);
332 cfd
= anetAccept(server
.neterr
, fd
, cip
, &cport
);
334 redisLog(REDIS_VERBOSE
,"Accepting client connection: %s", server
.neterr
);
337 redisLog(REDIS_VERBOSE
,"Accepted %s:%d", cip
, cport
);
338 if ((c
= createClient(cfd
)) == NULL
) {
339 redisLog(REDIS_WARNING
,"Error allocating resoures for the client");
340 close(cfd
); /* May be already closed, just ingore errors */
343 /* If maxclient directive is set and this is one client more... close the
344 * connection. Note that we create the client instead to check before
345 * for this condition, since now the socket is already set in nonblocking
346 * mode and we can send an error for free using the Kernel I/O */
347 if (server
.maxclients
&& listLength(server
.clients
) > server
.maxclients
) {
348 char *err
= "-ERR max number of clients reached\r\n";
350 /* That's a best effort error message, don't check write errors */
351 if (write(c
->fd
,err
,strlen(err
)) == -1) {
352 /* Nothing to do, Just to avoid the warning... */
357 server
.stat_numconnections
++;
360 static void freeClientArgv(redisClient
*c
) {
363 for (j
= 0; j
< c
->argc
; j
++)
364 decrRefCount(c
->argv
[j
]);
365 for (j
= 0; j
< c
->mbargc
; j
++)
366 decrRefCount(c
->mbargv
[j
]);
371 void freeClient(redisClient
*c
) {
374 /* Note that if the client we are freeing is blocked into a blocking
375 * call, we have to set querybuf to NULL *before* to call
376 * unblockClientWaitingData() to avoid processInputBuffer() will get
377 * called. Also it is important to remove the file events after
378 * this, because this call adds the READABLE event. */
379 sdsfree(c
->querybuf
);
381 if (c
->flags
& REDIS_BLOCKED
)
382 unblockClientWaitingData(c
);
384 /* UNWATCH all the keys */
386 listRelease(c
->watched_keys
);
387 /* Unsubscribe from all the pubsub channels */
388 pubsubUnsubscribeAllChannels(c
,0);
389 pubsubUnsubscribeAllPatterns(c
,0);
390 dictRelease(c
->pubsub_channels
);
391 listRelease(c
->pubsub_patterns
);
392 /* Obvious cleanup */
393 aeDeleteFileEvent(server
.el
,c
->fd
,AE_READABLE
);
394 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
395 listRelease(c
->reply
);
398 /* Remove from the list of clients */
399 ln
= listSearchKey(server
.clients
,c
);
400 redisAssert(ln
!= NULL
);
401 listDelNode(server
.clients
,ln
);
402 /* Remove from the list of clients waiting for swapped keys, or ready
403 * to be restarted, but not yet woken up again. */
404 if (c
->flags
& REDIS_IO_WAIT
) {
405 redisAssert(server
.vm_enabled
);
406 if (listLength(c
->io_keys
) == 0) {
407 ln
= listSearchKey(server
.io_ready_clients
,c
);
409 /* When this client is waiting to be woken up (REDIS_IO_WAIT),
410 * it should be present in the list io_ready_clients */
411 redisAssert(ln
!= NULL
);
412 listDelNode(server
.io_ready_clients
,ln
);
414 while (listLength(c
->io_keys
)) {
415 ln
= listFirst(c
->io_keys
);
416 dontWaitForSwappedKey(c
,ln
->value
);
419 server
.vm_blocked_clients
--;
421 listRelease(c
->io_keys
);
422 /* Master/slave cleanup.
423 * Case 1: we lost the connection with a slave. */
424 if (c
->flags
& REDIS_SLAVE
) {
425 if (c
->replstate
== REDIS_REPL_SEND_BULK
&& c
->repldbfd
!= -1)
427 list
*l
= (c
->flags
& REDIS_MONITOR
) ? server
.monitors
: server
.slaves
;
428 ln
= listSearchKey(l
,c
);
429 redisAssert(ln
!= NULL
);
433 /* Case 2: we lost the connection with the master. */
434 if (c
->flags
& REDIS_MASTER
) {
435 server
.master
= NULL
;
436 server
.replstate
= REDIS_REPL_CONNECT
;
437 /* Since we lost the connection with the master, we should also
438 * close the connection with all our slaves if we have any, so
439 * when we'll resync with the master the other slaves will sync again
440 * with us as well. Note that also when the slave is not connected
441 * to the master it will keep refusing connections by other slaves. */
442 while (listLength(server
.slaves
)) {
443 ln
= listFirst(server
.slaves
);
444 freeClient((redisClient
*)ln
->value
);
450 freeClientMultiState(c
);
454 void sendReplyToClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
455 redisClient
*c
= privdata
;
456 int nwritten
= 0, totwritten
= 0, objlen
;
461 /* Use writev() if we have enough buffers to send */
462 if (!server
.glueoutputbuf
&&
463 listLength(c
->reply
) > REDIS_WRITEV_THRESHOLD
&&
464 !(c
->flags
& REDIS_MASTER
))
466 sendReplyToClientWritev(el
, fd
, privdata
, mask
);
470 while(c
->bufpos
> 0 || listLength(c
->reply
)) {
472 if (c
->flags
& REDIS_MASTER
) {
473 /* Don't reply to a master */
474 nwritten
= c
->bufpos
- c
->sentlen
;
476 nwritten
= write(fd
,c
->buf
+c
->sentlen
,c
->bufpos
-c
->sentlen
);
477 if (nwritten
<= 0) break;
479 c
->sentlen
+= nwritten
;
480 totwritten
+= nwritten
;
482 /* If the buffer was sent, set bufpos to zero to continue with
483 * the remainder of the reply. */
484 if (c
->sentlen
== c
->bufpos
) {
489 o
= listNodeValue(listFirst(c
->reply
));
490 objlen
= sdslen(o
->ptr
);
493 listDelNode(c
->reply
,listFirst(c
->reply
));
497 if (c
->flags
& REDIS_MASTER
) {
498 /* Don't reply to a master */
499 nwritten
= objlen
- c
->sentlen
;
501 nwritten
= write(fd
, ((char*)o
->ptr
)+c
->sentlen
,objlen
-c
->sentlen
);
502 if (nwritten
<= 0) break;
504 c
->sentlen
+= nwritten
;
505 totwritten
+= nwritten
;
507 /* If we fully sent the object on head go to the next one */
508 if (c
->sentlen
== objlen
) {
509 listDelNode(c
->reply
,listFirst(c
->reply
));
513 /* Note that we avoid to send more thank REDIS_MAX_WRITE_PER_EVENT
514 * bytes, in a single threaded server it's a good idea to serve
515 * other clients as well, even if a very large request comes from
516 * super fast link that is always able to accept data (in real world
517 * scenario think about 'KEYS *' against the loopback interfae) */
518 if (totwritten
> REDIS_MAX_WRITE_PER_EVENT
) break;
520 if (nwritten
== -1) {
521 if (errno
== EAGAIN
) {
524 redisLog(REDIS_VERBOSE
,
525 "Error writing to client: %s", strerror(errno
));
530 if (totwritten
> 0) c
->lastinteraction
= time(NULL
);
531 if (listLength(c
->reply
) == 0) {
533 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
537 void sendReplyToClientWritev(aeEventLoop
*el
, int fd
, void *privdata
, int mask
)
539 redisClient
*c
= privdata
;
540 int nwritten
= 0, totwritten
= 0, objlen
, willwrite
;
542 struct iovec iov
[REDIS_WRITEV_IOVEC_COUNT
];
548 while (listLength(c
->reply
)) {
553 /* fill-in the iov[] array */
554 for(node
= listFirst(c
->reply
); node
; node
= listNextNode(node
)) {
555 o
= listNodeValue(node
);
556 objlen
= sdslen(o
->ptr
);
558 if (totwritten
+ objlen
- offset
> REDIS_MAX_WRITE_PER_EVENT
)
561 if(ion
== REDIS_WRITEV_IOVEC_COUNT
)
562 break; /* no more iovecs */
564 iov
[ion
].iov_base
= ((char*)o
->ptr
) + offset
;
565 iov
[ion
].iov_len
= objlen
- offset
;
566 willwrite
+= objlen
- offset
;
567 offset
= 0; /* just for the first item */
574 /* write all collected blocks at once */
575 if((nwritten
= writev(fd
, iov
, ion
)) < 0) {
576 if (errno
!= EAGAIN
) {
577 redisLog(REDIS_VERBOSE
,
578 "Error writing to client: %s", strerror(errno
));
585 totwritten
+= nwritten
;
588 /* remove written robjs from c->reply */
589 while (nwritten
&& listLength(c
->reply
)) {
590 o
= listNodeValue(listFirst(c
->reply
));
591 objlen
= sdslen(o
->ptr
);
593 if(nwritten
>= objlen
- offset
) {
594 listDelNode(c
->reply
, listFirst(c
->reply
));
595 nwritten
-= objlen
- offset
;
599 c
->sentlen
+= nwritten
;
607 c
->lastinteraction
= time(NULL
);
609 if (listLength(c
->reply
) == 0) {
611 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
615 /* resetClient prepare the client to process the next command */
616 void resetClient(redisClient
*c
) {
622 void closeTimedoutClients(void) {
625 time_t now
= time(NULL
);
628 listRewind(server
.clients
,&li
);
629 while ((ln
= listNext(&li
)) != NULL
) {
630 c
= listNodeValue(ln
);
631 if (server
.maxidletime
&&
632 !(c
->flags
& REDIS_SLAVE
) && /* no timeout for slaves */
633 !(c
->flags
& REDIS_MASTER
) && /* no timeout for masters */
634 !(c
->flags
& REDIS_BLOCKED
) && /* no timeout for BLPOP */
635 dictSize(c
->pubsub_channels
) == 0 && /* no timeout for pubsub */
636 listLength(c
->pubsub_patterns
) == 0 &&
637 (now
- c
->lastinteraction
> server
.maxidletime
))
639 redisLog(REDIS_VERBOSE
,"Closing idle client");
641 } else if (c
->flags
& REDIS_BLOCKED
) {
642 if (c
->blockingto
!= 0 && c
->blockingto
< now
) {
643 addReply(c
,shared
.nullmultibulk
);
644 unblockClientWaitingData(c
);
650 void processInputBuffer(redisClient
*c
) {
652 /* Before to process the input buffer, make sure the client is not
653 * waitig for a blocking operation such as BLPOP. Note that the first
654 * iteration the client is never blocked, otherwise the processInputBuffer
655 * would not be called at all, but after the execution of the first commands
656 * in the input buffer the client may be blocked, and the "goto again"
657 * will try to reiterate. The following line will make it return asap. */
658 if (c
->flags
& REDIS_BLOCKED
|| c
->flags
& REDIS_IO_WAIT
) return;
659 if (c
->bulklen
== -1) {
660 /* Read the first line of the query */
661 char *p
= strchr(c
->querybuf
,'\n');
669 c
->querybuf
= sdsempty();
670 querylen
= 1+(p
-(query
));
671 if (sdslen(query
) > querylen
) {
672 /* leave data after the first line of the query in the buffer */
673 c
->querybuf
= sdscatlen(c
->querybuf
,query
+querylen
,sdslen(query
)-querylen
);
675 *p
= '\0'; /* remove "\n" */
676 if (*(p
-1) == '\r') *(p
-1) = '\0'; /* and "\r" if any */
679 /* Now we can split the query in arguments */
680 argv
= sdssplitlen(query
,sdslen(query
)," ",1,&argc
);
683 if (c
->argv
) zfree(c
->argv
);
684 c
->argv
= zmalloc(sizeof(robj
*)*argc
);
686 for (j
= 0; j
< argc
; j
++) {
687 if (sdslen(argv
[j
])) {
688 c
->argv
[c
->argc
] = createObject(REDIS_STRING
,argv
[j
]);
696 /* Execute the command. If the client is still valid
697 * after processCommand() return and there is something
698 * on the query buffer try to process the next command. */
699 if (processCommand(c
) && sdslen(c
->querybuf
)) goto again
;
701 /* Nothing to process, argc == 0. Just process the query
702 * buffer if it's not empty or return to the caller */
703 if (sdslen(c
->querybuf
)) goto again
;
706 } else if (sdslen(c
->querybuf
) >= REDIS_REQUEST_MAX_SIZE
) {
707 redisLog(REDIS_VERBOSE
, "Client protocol error");
712 /* Bulk read handling. Note that if we are at this point
713 the client already sent a command terminated with a newline,
714 we are reading the bulk data that is actually the last
715 argument of the command. */
716 int qbl
= sdslen(c
->querybuf
);
718 if (c
->bulklen
<= qbl
) {
719 /* Copy everything but the final CRLF as final argument */
720 c
->argv
[c
->argc
] = createStringObject(c
->querybuf
,c
->bulklen
-2);
722 c
->querybuf
= sdsrange(c
->querybuf
,c
->bulklen
,-1);
723 /* Process the command. If the client is still valid after
724 * the processing and there is more data in the buffer
725 * try to parse it. */
726 if (processCommand(c
) && sdslen(c
->querybuf
)) goto again
;
732 void readQueryFromClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
733 redisClient
*c
= (redisClient
*) privdata
;
734 char buf
[REDIS_IOBUF_LEN
];
739 nread
= read(fd
, buf
, REDIS_IOBUF_LEN
);
741 if (errno
== EAGAIN
) {
744 redisLog(REDIS_VERBOSE
, "Reading from client: %s",strerror(errno
));
748 } else if (nread
== 0) {
749 redisLog(REDIS_VERBOSE
, "Client closed connection");
754 c
->querybuf
= sdscatlen(c
->querybuf
, buf
, nread
);
755 c
->lastinteraction
= time(NULL
);
759 processInputBuffer(c
);