4 static void setProtocolError(redisClient
*c
, int pos
);
6 /* To evaluate the output buffer size of a client we need to get size of
7 * allocated objects, however we can't used zmalloc_size() directly on sds
8 * strings because of the trick they use to work (the header is before the
9 * returned pointer), so we use this helper function. */
10 size_t zmalloc_size_sds(sds s
) {
11 return zmalloc_size(s
-sizeof(struct sdshdr
));
14 void *dupClientReplyValue(void *o
) {
15 incrRefCount((robj
*)o
);
19 int listMatchObjects(void *a
, void *b
) {
20 return equalStringObjects(a
,b
);
23 redisClient
*createClient(int fd
) {
24 redisClient
*c
= zmalloc(sizeof(redisClient
));
27 /* passing -1 as fd it is possible to create a non connected client.
28 * This is useful since all the Redis commands needs to be executed
29 * in the context of a client. When commands are executed in other
30 * contexts (for instance a Lua script) we need a non connected client. */
32 anetNonBlock(NULL
,fd
);
33 anetTcpNoDelay(NULL
,fd
);
34 if (aeCreateFileEvent(server
.el
,fd
,AE_READABLE
,
35 readQueryFromClient
, c
) == AE_ERR
)
45 c
->querybuf
= sdsempty();
49 c
->cmd
= c
->lastcmd
= NULL
;
54 c
->lastinteraction
= time(NULL
);
56 c
->replstate
= REDIS_REPL_NONE
;
57 c
->reply
= listCreate();
59 c
->obuf_soft_limit_reached_time
= 0;
60 listSetFreeMethod(c
->reply
,decrRefCount
);
61 listSetDupMethod(c
->reply
,dupClientReplyValue
);
65 c
->bpop
.target
= NULL
;
66 c
->io_keys
= listCreate();
67 c
->watched_keys
= listCreate();
68 listSetFreeMethod(c
->io_keys
,decrRefCount
);
69 c
->pubsub_channels
= dictCreate(&setDictType
,NULL
);
70 c
->pubsub_patterns
= listCreate();
71 listSetFreeMethod(c
->pubsub_patterns
,decrRefCount
);
72 listSetMatchMethod(c
->pubsub_patterns
,listMatchObjects
);
73 if (fd
!= -1) listAddNodeTail(server
.clients
,c
);
74 initClientMultiState(c
);
78 /* This function is called every time we are going to transmit new data
79 * to the client. The behavior is the following:
81 * If the client should receive new data (normal clients will) the function
82 * returns REDIS_OK, and make sure to install the write handler in our event
83 * loop so that when the socket is writable new data gets written.
85 * If the client should not receive new data, because it is a fake client
86 * or a slave, or because the setup of the write handler failed, the function
89 * Typically gets called every time a reply is built, before adding more
90 * data to the clients output buffers. If the function returns REDIS_ERR no
91 * data should be appended to the output buffers. */
92 int prepareClientToWrite(redisClient
*c
) {
93 if (c
->flags
& REDIS_LUA_CLIENT
) return REDIS_OK
;
94 if (c
->fd
<= 0) return REDIS_ERR
; /* Fake client */
95 if (c
->bufpos
== 0 && listLength(c
->reply
) == 0 &&
96 (c
->replstate
== REDIS_REPL_NONE
||
97 c
->replstate
== REDIS_REPL_ONLINE
) &&
98 aeCreateFileEvent(server
.el
, c
->fd
, AE_WRITABLE
,
99 sendReplyToClient
, c
) == AE_ERR
) return REDIS_ERR
;
103 /* Create a duplicate of the last object in the reply list when
104 * it is not exclusively owned by the reply list. */
105 robj
*dupLastObjectIfNeeded(list
*reply
) {
108 redisAssert(listLength(reply
) > 0);
109 ln
= listLast(reply
);
110 cur
= listNodeValue(ln
);
111 if (cur
->refcount
> 1) {
112 new = dupStringObject(cur
);
114 listNodeValue(ln
) = new;
116 return listNodeValue(ln
);
119 /* -----------------------------------------------------------------------------
120 * Low level functions to add more data to output buffers.
121 * -------------------------------------------------------------------------- */
123 int _addReplyToBuffer(redisClient
*c
, char *s
, size_t len
) {
124 size_t available
= sizeof(c
->buf
)-c
->bufpos
;
126 if (c
->flags
& REDIS_CLOSE_AFTER_REPLY
) return REDIS_OK
;
128 /* If there already are entries in the reply list, we cannot
129 * add anything more to the static buffer. */
130 if (listLength(c
->reply
) > 0) return REDIS_ERR
;
132 /* Check that the buffer has enough space available for this string. */
133 if (len
> available
) return REDIS_ERR
;
135 memcpy(c
->buf
+c
->bufpos
,s
,len
);
140 void _addReplyObjectToList(redisClient
*c
, robj
*o
) {
143 if (c
->flags
& REDIS_CLOSE_AFTER_REPLY
) return;
145 if (listLength(c
->reply
) == 0) {
147 listAddNodeTail(c
->reply
,o
);
148 c
->reply_bytes
+= zmalloc_size_sds(o
->ptr
);
150 tail
= listNodeValue(listLast(c
->reply
));
152 /* Append to this object when possible. */
153 if (tail
->ptr
!= NULL
&&
154 sdslen(tail
->ptr
)+sdslen(o
->ptr
) <= REDIS_REPLY_CHUNK_BYTES
)
156 c
->reply_bytes
-= zmalloc_size_sds(tail
->ptr
);
157 tail
= dupLastObjectIfNeeded(c
->reply
);
158 tail
->ptr
= sdscatlen(tail
->ptr
,o
->ptr
,sdslen(o
->ptr
));
159 c
->reply_bytes
+= zmalloc_size_sds(tail
->ptr
);
162 listAddNodeTail(c
->reply
,o
);
163 c
->reply_bytes
+= zmalloc_size_sds(o
->ptr
);
166 asyncCloseClientOnOutputBufferLimitReached(c
);
169 /* This method takes responsibility over the sds. When it is no longer
170 * needed it will be free'd, otherwise it ends up in a robj. */
171 void _addReplySdsToList(redisClient
*c
, sds s
) {
174 if (c
->flags
& REDIS_CLOSE_AFTER_REPLY
) {
179 if (listLength(c
->reply
) == 0) {
180 listAddNodeTail(c
->reply
,createObject(REDIS_STRING
,s
));
181 c
->reply_bytes
+= zmalloc_size_sds(s
);
183 tail
= listNodeValue(listLast(c
->reply
));
185 /* Append to this object when possible. */
186 if (tail
->ptr
!= NULL
&&
187 sdslen(tail
->ptr
)+sdslen(s
) <= REDIS_REPLY_CHUNK_BYTES
)
189 c
->reply_bytes
-= zmalloc_size_sds(tail
->ptr
);
190 tail
= dupLastObjectIfNeeded(c
->reply
);
191 tail
->ptr
= sdscatlen(tail
->ptr
,s
,sdslen(s
));
192 c
->reply_bytes
+= zmalloc_size_sds(tail
->ptr
);
195 listAddNodeTail(c
->reply
,createObject(REDIS_STRING
,s
));
196 c
->reply_bytes
+= zmalloc_size_sds(s
);
199 asyncCloseClientOnOutputBufferLimitReached(c
);
202 void _addReplyStringToList(redisClient
*c
, char *s
, size_t len
) {
205 if (c
->flags
& REDIS_CLOSE_AFTER_REPLY
) return;
207 if (listLength(c
->reply
) == 0) {
208 robj
*o
= createStringObject(s
,len
);
210 listAddNodeTail(c
->reply
,o
);
211 c
->reply_bytes
+= zmalloc_size_sds(o
->ptr
);
213 tail
= listNodeValue(listLast(c
->reply
));
215 /* Append to this object when possible. */
216 if (tail
->ptr
!= NULL
&&
217 sdslen(tail
->ptr
)+len
<= REDIS_REPLY_CHUNK_BYTES
)
219 c
->reply_bytes
-= zmalloc_size_sds(tail
->ptr
);
220 tail
= dupLastObjectIfNeeded(c
->reply
);
221 tail
->ptr
= sdscatlen(tail
->ptr
,s
,len
);
222 c
->reply_bytes
+= zmalloc_size_sds(tail
->ptr
);
224 robj
*o
= createStringObject(s
,len
);
226 listAddNodeTail(c
->reply
,o
);
227 c
->reply_bytes
+= zmalloc_size_sds(o
->ptr
);
230 asyncCloseClientOnOutputBufferLimitReached(c
);
233 /* -----------------------------------------------------------------------------
234 * Higher level functions to queue data on the client output buffer.
235 * The following functions are the ones that commands implementations will call.
236 * -------------------------------------------------------------------------- */
238 void addReply(redisClient
*c
, robj
*obj
) {
239 if (prepareClientToWrite(c
) != REDIS_OK
) return;
241 /* This is an important place where we can avoid copy-on-write
242 * when there is a saving child running, avoiding touching the
243 * refcount field of the object if it's not needed.
245 * If the encoding is RAW and there is room in the static buffer
246 * we'll be able to send the object to the client without
247 * messing with its page. */
248 if (obj
->encoding
== REDIS_ENCODING_RAW
) {
249 if (_addReplyToBuffer(c
,obj
->ptr
,sdslen(obj
->ptr
)) != REDIS_OK
)
250 _addReplyObjectToList(c
,obj
);
251 } else if (obj
->encoding
== REDIS_ENCODING_INT
) {
252 /* Optimization: if there is room in the static buffer for 32 bytes
253 * (more than the max chars a 64 bit integer can take as string) we
254 * avoid decoding the object and go for the lower level approach. */
255 if (listLength(c
->reply
) == 0 && (sizeof(c
->buf
) - c
->bufpos
) >= 32) {
259 len
= ll2string(buf
,sizeof(buf
),(long)obj
->ptr
);
260 if (_addReplyToBuffer(c
,buf
,len
) == REDIS_OK
)
262 /* else... continue with the normal code path, but should never
263 * happen actually since we verified there is room. */
265 obj
= getDecodedObject(obj
);
266 if (_addReplyToBuffer(c
,obj
->ptr
,sdslen(obj
->ptr
)) != REDIS_OK
)
267 _addReplyObjectToList(c
,obj
);
270 redisPanic("Wrong obj->encoding in addReply()");
274 void addReplySds(redisClient
*c
, sds s
) {
275 if (prepareClientToWrite(c
) != REDIS_OK
) {
276 /* The caller expects the sds to be free'd. */
280 if (_addReplyToBuffer(c
,s
,sdslen(s
)) == REDIS_OK
) {
283 /* This method free's the sds when it is no longer needed. */
284 _addReplySdsToList(c
,s
);
288 void addReplyString(redisClient
*c
, char *s
, size_t len
) {
289 if (prepareClientToWrite(c
) != REDIS_OK
) return;
290 if (_addReplyToBuffer(c
,s
,len
) != REDIS_OK
)
291 _addReplyStringToList(c
,s
,len
);
294 void addReplyErrorLength(redisClient
*c
, char *s
, size_t len
) {
295 addReplyString(c
,"-ERR ",5);
296 addReplyString(c
,s
,len
);
297 addReplyString(c
,"\r\n",2);
300 void addReplyError(redisClient
*c
, char *err
) {
301 addReplyErrorLength(c
,err
,strlen(err
));
304 void addReplyErrorFormat(redisClient
*c
, const char *fmt
, ...) {
308 sds s
= sdscatvprintf(sdsempty(),fmt
,ap
);
310 /* Make sure there are no newlines in the string, otherwise invalid protocol
313 for (j
= 0; j
< l
; j
++) {
314 if (s
[j
] == '\r' || s
[j
] == '\n') s
[j
] = ' ';
316 addReplyErrorLength(c
,s
,sdslen(s
));
320 void addReplyStatusLength(redisClient
*c
, char *s
, size_t len
) {
321 addReplyString(c
,"+",1);
322 addReplyString(c
,s
,len
);
323 addReplyString(c
,"\r\n",2);
326 void addReplyStatus(redisClient
*c
, char *status
) {
327 addReplyStatusLength(c
,status
,strlen(status
));
330 void addReplyStatusFormat(redisClient
*c
, const char *fmt
, ...) {
333 sds s
= sdscatvprintf(sdsempty(),fmt
,ap
);
335 addReplyStatusLength(c
,s
,sdslen(s
));
339 /* Adds an empty object to the reply list that will contain the multi bulk
340 * length, which is not known when this function is called. */
341 void *addDeferredMultiBulkLength(redisClient
*c
) {
342 /* Note that we install the write event here even if the object is not
343 * ready to be sent, since we are sure that before returning to the
344 * event loop setDeferredMultiBulkLength() will be called. */
345 if (prepareClientToWrite(c
) != REDIS_OK
) return NULL
;
346 listAddNodeTail(c
->reply
,createObject(REDIS_STRING
,NULL
));
347 return listLast(c
->reply
);
350 /* Populate the length object and try glueing it to the next chunk. */
351 void setDeferredMultiBulkLength(redisClient
*c
, void *node
, long length
) {
352 listNode
*ln
= (listNode
*)node
;
355 /* Abort when *node is NULL (see addDeferredMultiBulkLength). */
356 if (node
== NULL
) return;
358 len
= listNodeValue(ln
);
359 len
->ptr
= sdscatprintf(sdsempty(),"*%ld\r\n",length
);
360 c
->reply_bytes
+= zmalloc_size_sds(len
->ptr
);
361 if (ln
->next
!= NULL
) {
362 next
= listNodeValue(ln
->next
);
364 /* Only glue when the next node is non-NULL (an sds in this case) */
365 if (next
->ptr
!= NULL
) {
366 len
->ptr
= sdscatlen(len
->ptr
,next
->ptr
,sdslen(next
->ptr
));
367 listDelNode(c
->reply
,ln
->next
);
370 asyncCloseClientOnOutputBufferLimitReached(c
);
373 /* Add a duble as a bulk reply */
374 void addReplyDouble(redisClient
*c
, double d
) {
375 char dbuf
[128], sbuf
[128];
377 dlen
= snprintf(dbuf
,sizeof(dbuf
),"%.17g",d
);
378 slen
= snprintf(sbuf
,sizeof(sbuf
),"$%d\r\n%s\r\n",dlen
,dbuf
);
379 addReplyString(c
,sbuf
,slen
);
382 /* Add a long long as integer reply or bulk len / multi bulk count.
383 * Basically this is used to output <prefix><long long><crlf>. */
384 void addReplyLongLongWithPrefix(redisClient
*c
, long long ll
, char prefix
) {
388 /* Things like $3\r\n or *2\r\n are emitted very often by the protocol
389 * so we have a few shared objects to use if the integer is small
390 * like it is most of the times. */
391 if (prefix
== '*' && ll
< REDIS_SHARED_BULKHDR_LEN
) {
392 addReply(c
,shared
.mbulkhdr
[ll
]);
394 } else if (prefix
== '$' && ll
< REDIS_SHARED_BULKHDR_LEN
) {
395 addReply(c
,shared
.bulkhdr
[ll
]);
400 len
= ll2string(buf
+1,sizeof(buf
)-1,ll
);
403 addReplyString(c
,buf
,len
+3);
406 void addReplyLongLong(redisClient
*c
, long long ll
) {
408 addReply(c
,shared
.czero
);
410 addReply(c
,shared
.cone
);
412 addReplyLongLongWithPrefix(c
,ll
,':');
415 void addReplyMultiBulkLen(redisClient
*c
, long length
) {
416 addReplyLongLongWithPrefix(c
,length
,'*');
419 /* Create the length prefix of a bulk reply, example: $2234 */
420 void addReplyBulkLen(redisClient
*c
, robj
*obj
) {
423 if (obj
->encoding
== REDIS_ENCODING_RAW
) {
424 len
= sdslen(obj
->ptr
);
426 long n
= (long)obj
->ptr
;
428 /* Compute how many bytes will take this integer as a radix 10 string */
434 while((n
= n
/10) != 0) {
438 addReplyLongLongWithPrefix(c
,len
,'$');
441 /* Add a Redis Object as a bulk reply */
442 void addReplyBulk(redisClient
*c
, robj
*obj
) {
443 addReplyBulkLen(c
,obj
);
445 addReply(c
,shared
.crlf
);
448 /* Add a C buffer as bulk reply */
449 void addReplyBulkCBuffer(redisClient
*c
, void *p
, size_t len
) {
450 addReplyLongLongWithPrefix(c
,len
,'$');
451 addReplyString(c
,p
,len
);
452 addReply(c
,shared
.crlf
);
455 /* Add a C nul term string as bulk reply */
456 void addReplyBulkCString(redisClient
*c
, char *s
) {
458 addReply(c
,shared
.nullbulk
);
460 addReplyBulkCBuffer(c
,s
,strlen(s
));
464 /* Add a long long as a bulk reply */
465 void addReplyBulkLongLong(redisClient
*c
, long long ll
) {
469 len
= ll2string(buf
,64,ll
);
470 addReplyBulkCBuffer(c
,buf
,len
);
473 /* Copy 'src' client output buffers into 'dst' client output buffers.
474 * The function takes care of freeing the old output buffers of the
475 * destination client. */
476 void copyClientOutputBuffer(redisClient
*dst
, redisClient
*src
) {
477 listRelease(dst
->reply
);
478 dst
->reply
= listDup(src
->reply
);
479 memcpy(dst
->buf
,src
->buf
,src
->bufpos
);
480 dst
->bufpos
= src
->bufpos
;
481 dst
->reply_bytes
= src
->reply_bytes
;
484 static void acceptCommonHandler(int fd
) {
486 if ((c
= createClient(fd
)) == NULL
) {
487 redisLog(REDIS_WARNING
,"Error allocating resoures for the client");
488 close(fd
); /* May be already closed, just ingore errors */
491 /* If maxclient directive is set and this is one client more... close the
492 * connection. Note that we create the client instead to check before
493 * for this condition, since now the socket is already set in nonblocking
494 * mode and we can send an error for free using the Kernel I/O */
495 if (listLength(server
.clients
) > server
.maxclients
) {
496 char *err
= "-ERR max number of clients reached\r\n";
498 /* That's a best effort error message, don't check write errors */
499 if (write(c
->fd
,err
,strlen(err
)) == -1) {
500 /* Nothing to do, Just to avoid the warning... */
502 server
.stat_rejected_conn
++;
506 server
.stat_numconnections
++;
509 void acceptTcpHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
514 REDIS_NOTUSED(privdata
);
516 cfd
= anetTcpAccept(server
.neterr
, fd
, cip
, &cport
);
518 redisLog(REDIS_WARNING
,"Accepting client connection: %s", server
.neterr
);
521 redisLog(REDIS_VERBOSE
,"Accepted %s:%d", cip
, cport
);
522 acceptCommonHandler(cfd
);
525 void acceptUnixHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
529 REDIS_NOTUSED(privdata
);
531 cfd
= anetUnixAccept(server
.neterr
, fd
);
533 redisLog(REDIS_WARNING
,"Accepting client connection: %s", server
.neterr
);
536 redisLog(REDIS_VERBOSE
,"Accepted connection to %s", server
.unixsocket
);
537 acceptCommonHandler(cfd
);
541 static void freeClientArgv(redisClient
*c
) {
543 for (j
= 0; j
< c
->argc
; j
++)
544 decrRefCount(c
->argv
[j
]);
549 void freeClient(redisClient
*c
) {
552 /* If this is marked as current client unset it */
553 if (server
.current_client
== c
) server
.current_client
= NULL
;
555 /* Note that if the client we are freeing is blocked into a blocking
556 * call, we have to set querybuf to NULL *before* to call
557 * unblockClientWaitingData() to avoid processInputBuffer() will get
558 * called. Also it is important to remove the file events after
559 * this, because this call adds the READABLE event. */
560 sdsfree(c
->querybuf
);
562 if (c
->flags
& REDIS_BLOCKED
)
563 unblockClientWaitingData(c
);
565 /* UNWATCH all the keys */
567 listRelease(c
->watched_keys
);
568 /* Unsubscribe from all the pubsub channels */
569 pubsubUnsubscribeAllChannels(c
,0);
570 pubsubUnsubscribeAllPatterns(c
,0);
571 dictRelease(c
->pubsub_channels
);
572 listRelease(c
->pubsub_patterns
);
573 /* Obvious cleanup */
574 aeDeleteFileEvent(server
.el
,c
->fd
,AE_READABLE
);
575 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
576 listRelease(c
->reply
);
579 /* Remove from the list of clients */
580 ln
= listSearchKey(server
.clients
,c
);
581 redisAssert(ln
!= NULL
);
582 listDelNode(server
.clients
,ln
);
583 /* When client was just unblocked because of a blocking operation,
584 * remove it from the list with unblocked clients. */
585 if (c
->flags
& REDIS_UNBLOCKED
) {
586 ln
= listSearchKey(server
.unblocked_clients
,c
);
587 redisAssert(ln
!= NULL
);
588 listDelNode(server
.unblocked_clients
,ln
);
590 listRelease(c
->io_keys
);
591 /* Master/slave cleanup.
592 * Case 1: we lost the connection with a slave. */
593 if (c
->flags
& REDIS_SLAVE
) {
594 if (c
->replstate
== REDIS_REPL_SEND_BULK
&& c
->repldbfd
!= -1)
596 list
*l
= (c
->flags
& REDIS_MONITOR
) ? server
.monitors
: server
.slaves
;
597 ln
= listSearchKey(l
,c
);
598 redisAssert(ln
!= NULL
);
602 /* Case 2: we lost the connection with the master. */
603 if (c
->flags
& REDIS_MASTER
) {
604 server
.master
= NULL
;
605 server
.repl_state
= REDIS_REPL_CONNECT
;
606 server
.repl_down_since
= time(NULL
);
607 /* Since we lost the connection with the master, we should also
608 * close the connection with all our slaves if we have any, so
609 * when we'll resync with the master the other slaves will sync again
610 * with us as well. Note that also when the slave is not connected
611 * to the master it will keep refusing connections by other slaves.
613 * We do this only if server.masterhost != NULL. If it is NULL this
614 * means the user called SLAVEOF NO ONE and we are freeing our
615 * link with the master, so no need to close link with slaves. */
616 if (server
.masterhost
!= NULL
) {
617 while (listLength(server
.slaves
)) {
618 ln
= listFirst(server
.slaves
);
619 freeClient((redisClient
*)ln
->value
);
624 /* If this client was scheduled for async freeing we need to remove it
626 if (c
->flags
& REDIS_CLOSE_ASAP
) {
627 ln
= listSearchKey(server
.clients_to_close
,c
);
628 redisAssert(ln
!= NULL
);
629 listDelNode(server
.clients_to_close
,ln
);
634 freeClientMultiState(c
);
638 /* Schedule a client to free it at a safe time in the serverCron() function.
639 * This function is useful when we need to terminate a client but we are in
640 * a context where calling freeClient() is not possible, because the client
641 * should be valid for the continuation of the flow of the program. */
642 void freeClientAsync(redisClient
*c
) {
643 if (c
->flags
& REDIS_CLOSE_ASAP
) return;
644 c
->flags
|= REDIS_CLOSE_ASAP
;
645 listAddNodeTail(server
.clients_to_close
,c
);
648 void freeClientsInAsyncFreeQueue(void) {
649 while (listLength(server
.clients_to_close
)) {
650 listNode
*ln
= listFirst(server
.clients_to_close
);
651 redisClient
*c
= listNodeValue(ln
);
653 c
->flags
&= ~REDIS_CLOSE_ASAP
;
655 listDelNode(server
.clients_to_close
,ln
);
659 void sendReplyToClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
660 redisClient
*c
= privdata
;
661 int nwritten
= 0, totwritten
= 0, objlen
;
667 while(c
->bufpos
> 0 || listLength(c
->reply
)) {
669 if (c
->flags
& REDIS_MASTER
) {
670 /* Don't reply to a master */
671 nwritten
= c
->bufpos
- c
->sentlen
;
673 nwritten
= write(fd
,c
->buf
+c
->sentlen
,c
->bufpos
-c
->sentlen
);
674 if (nwritten
<= 0) break;
676 c
->sentlen
+= nwritten
;
677 totwritten
+= nwritten
;
679 /* If the buffer was sent, set bufpos to zero to continue with
680 * the remainder of the reply. */
681 if (c
->sentlen
== c
->bufpos
) {
686 o
= listNodeValue(listFirst(c
->reply
));
687 objlen
= sdslen(o
->ptr
);
688 objmem
= zmalloc_size_sds(o
->ptr
);
691 listDelNode(c
->reply
,listFirst(c
->reply
));
695 if (c
->flags
& REDIS_MASTER
) {
696 /* Don't reply to a master */
697 nwritten
= objlen
- c
->sentlen
;
699 nwritten
= write(fd
, ((char*)o
->ptr
)+c
->sentlen
,objlen
-c
->sentlen
);
700 if (nwritten
<= 0) break;
702 c
->sentlen
+= nwritten
;
703 totwritten
+= nwritten
;
705 /* If we fully sent the object on head go to the next one */
706 if (c
->sentlen
== objlen
) {
707 listDelNode(c
->reply
,listFirst(c
->reply
));
709 c
->reply_bytes
-= objmem
;
712 /* Note that we avoid to send more than REDIS_MAX_WRITE_PER_EVENT
713 * bytes, in a single threaded server it's a good idea to serve
714 * other clients as well, even if a very large request comes from
715 * super fast link that is always able to accept data (in real world
716 * scenario think about 'KEYS *' against the loopback interface).
718 * However if we are over the maxmemory limit we ignore that and
719 * just deliver as much data as it is possible to deliver. */
720 if (totwritten
> REDIS_MAX_WRITE_PER_EVENT
&&
721 (server
.maxmemory
== 0 ||
722 zmalloc_used_memory() < server
.maxmemory
)) break;
724 if (nwritten
== -1) {
725 if (errno
== EAGAIN
) {
728 redisLog(REDIS_VERBOSE
,
729 "Error writing to client: %s", strerror(errno
));
734 if (totwritten
> 0) c
->lastinteraction
= time(NULL
);
735 if (c
->bufpos
== 0 && listLength(c
->reply
) == 0) {
737 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
739 /* Close connection after entire reply has been sent. */
740 if (c
->flags
& REDIS_CLOSE_AFTER_REPLY
) freeClient(c
);
744 /* resetClient prepare the client to process the next command */
745 void resetClient(redisClient
*c
) {
750 /* We clear the ASKING flag as well if we are not inside a MULTI. */
751 if (!(c
->flags
& REDIS_MULTI
)) c
->flags
&= (~REDIS_ASKING
);
754 void closeTimedoutClients(void) {
757 time_t now
= time(NULL
);
760 listRewind(server
.clients
,&li
);
761 while ((ln
= listNext(&li
)) != NULL
) {
762 c
= listNodeValue(ln
);
763 if (server
.maxidletime
&&
764 !(c
->flags
& REDIS_SLAVE
) && /* no timeout for slaves */
765 !(c
->flags
& REDIS_MASTER
) && /* no timeout for masters */
766 !(c
->flags
& REDIS_BLOCKED
) && /* no timeout for BLPOP */
767 dictSize(c
->pubsub_channels
) == 0 && /* no timeout for pubsub */
768 listLength(c
->pubsub_patterns
) == 0 &&
769 (now
- c
->lastinteraction
> server
.maxidletime
))
771 redisLog(REDIS_VERBOSE
,"Closing idle client");
773 } else if (c
->flags
& REDIS_BLOCKED
) {
774 if (c
->bpop
.timeout
!= 0 && c
->bpop
.timeout
< now
) {
775 addReply(c
,shared
.nullmultibulk
);
776 unblockClientWaitingData(c
);
782 int processInlineBuffer(redisClient
*c
) {
783 char *newline
= strstr(c
->querybuf
,"\r\n");
788 /* Nothing to do without a \r\n */
789 if (newline
== NULL
) {
790 if (sdslen(c
->querybuf
) > REDIS_INLINE_MAX_SIZE
) {
791 addReplyError(c
,"Protocol error: too big inline request");
792 setProtocolError(c
,0);
797 /* Split the input buffer up to the \r\n */
798 querylen
= newline
-(c
->querybuf
);
799 argv
= sdssplitlen(c
->querybuf
,querylen
," ",1,&argc
);
801 /* Leave data after the first line of the query in the buffer */
802 c
->querybuf
= sdsrange(c
->querybuf
,querylen
+2,-1);
804 /* Setup argv array on client structure */
805 if (c
->argv
) zfree(c
->argv
);
806 c
->argv
= zmalloc(sizeof(robj
*)*argc
);
808 /* Create redis objects for all arguments. */
809 for (c
->argc
= 0, j
= 0; j
< argc
; j
++) {
810 if (sdslen(argv
[j
])) {
811 c
->argv
[c
->argc
] = createObject(REDIS_STRING
,argv
[j
]);
821 /* Helper function. Trims query buffer to make the function that processes
822 * multi bulk requests idempotent. */
823 static void setProtocolError(redisClient
*c
, int pos
) {
824 if (server
.verbosity
>= REDIS_VERBOSE
) {
825 sds client
= getClientInfoString(c
);
826 redisLog(REDIS_VERBOSE
,
827 "Protocol error from client: %s", client
);
830 c
->flags
|= REDIS_CLOSE_AFTER_REPLY
;
831 c
->querybuf
= sdsrange(c
->querybuf
,pos
,-1);
834 int processMultibulkBuffer(redisClient
*c
) {
835 char *newline
= NULL
;
839 if (c
->multibulklen
== 0) {
840 /* The client should have been reset */
841 redisAssertWithInfo(c
,NULL
,c
->argc
== 0);
843 /* Multi bulk length cannot be read without a \r\n */
844 newline
= strchr(c
->querybuf
,'\r');
845 if (newline
== NULL
) {
846 if (sdslen(c
->querybuf
) > REDIS_INLINE_MAX_SIZE
) {
847 addReplyError(c
,"Protocol error: too big mbulk count string");
848 setProtocolError(c
,0);
853 /* Buffer should also contain \n */
854 if (newline
-(c
->querybuf
) > ((signed)sdslen(c
->querybuf
)-2))
857 /* We know for sure there is a whole line since newline != NULL,
858 * so go ahead and find out the multi bulk length. */
859 redisAssertWithInfo(c
,NULL
,c
->querybuf
[0] == '*');
860 ok
= string2ll(c
->querybuf
+1,newline
-(c
->querybuf
+1),&ll
);
861 if (!ok
|| ll
> 1024*1024) {
862 addReplyError(c
,"Protocol error: invalid multibulk length");
863 setProtocolError(c
,pos
);
867 pos
= (newline
-c
->querybuf
)+2;
869 c
->querybuf
= sdsrange(c
->querybuf
,pos
,-1);
873 c
->multibulklen
= ll
;
875 /* Setup argv array on client structure */
876 if (c
->argv
) zfree(c
->argv
);
877 c
->argv
= zmalloc(sizeof(robj
*)*c
->multibulklen
);
880 redisAssertWithInfo(c
,NULL
,c
->multibulklen
> 0);
881 while(c
->multibulklen
) {
882 /* Read bulk length if unknown */
883 if (c
->bulklen
== -1) {
884 newline
= strchr(c
->querybuf
+pos
,'\r');
885 if (newline
== NULL
) {
886 if (sdslen(c
->querybuf
) > REDIS_INLINE_MAX_SIZE
) {
887 addReplyError(c
,"Protocol error: too big bulk count string");
888 setProtocolError(c
,0);
893 /* Buffer should also contain \n */
894 if (newline
-(c
->querybuf
) > ((signed)sdslen(c
->querybuf
)-2))
897 if (c
->querybuf
[pos
] != '$') {
898 addReplyErrorFormat(c
,
899 "Protocol error: expected '$', got '%c'",
901 setProtocolError(c
,pos
);
905 ok
= string2ll(c
->querybuf
+pos
+1,newline
-(c
->querybuf
+pos
+1),&ll
);
906 if (!ok
|| ll
< 0 || ll
> 512*1024*1024) {
907 addReplyError(c
,"Protocol error: invalid bulk length");
908 setProtocolError(c
,pos
);
912 pos
+= newline
-(c
->querybuf
+pos
)+2;
913 if (ll
>= REDIS_MBULK_BIG_ARG
) {
914 /* If we are going to read a large object from network
915 * try to make it likely that it will start at c->querybuf
916 * boundary so that we can optimized object creation
917 * avoiding a large copy of data. */
918 c
->querybuf
= sdsrange(c
->querybuf
,pos
,-1);
920 /* Hint the sds library about the amount of bytes this string is
921 * going to contain. */
922 c
->querybuf
= sdsMakeRoomFor(c
->querybuf
,ll
+2);
927 /* Read bulk argument */
928 if (sdslen(c
->querybuf
)-pos
< (unsigned)(c
->bulklen
+2)) {
929 /* Not enough data (+2 == trailing \r\n) */
932 /* Optimization: if the buffer contanins JUST our bulk element
933 * instead of creating a new object by *copying* the sds we
934 * just use the current sds string. */
936 c
->bulklen
>= REDIS_MBULK_BIG_ARG
&&
937 (signed) sdslen(c
->querybuf
) == c
->bulklen
+2)
939 c
->argv
[c
->argc
++] = createObject(REDIS_STRING
,c
->querybuf
);
940 sdsIncrLen(c
->querybuf
,-2); /* remove CRLF */
941 c
->querybuf
= sdsempty();
942 /* Assume that if we saw a fat argument we'll see another one
944 c
->querybuf
= sdsMakeRoomFor(c
->querybuf
,c
->bulklen
+2);
948 createStringObject(c
->querybuf
+pos
,c
->bulklen
);
957 if (pos
) c
->querybuf
= sdsrange(c
->querybuf
,pos
,-1);
959 /* We're done when c->multibulk == 0 */
960 if (c
->multibulklen
== 0) return REDIS_OK
;
962 /* Still not read to process the command */
966 void processInputBuffer(redisClient
*c
) {
967 /* Keep processing while there is something in the input buffer */
968 while(sdslen(c
->querybuf
)) {
969 /* Immediately abort if the client is in the middle of something. */
970 if (c
->flags
& REDIS_BLOCKED
) return;
972 /* REDIS_CLOSE_AFTER_REPLY closes the connection once the reply is
973 * written to the client. Make sure to not let the reply grow after
974 * this flag has been set (i.e. don't process more commands). */
975 if (c
->flags
& REDIS_CLOSE_AFTER_REPLY
) return;
977 /* Determine request type when unknown. */
979 if (c
->querybuf
[0] == '*') {
980 c
->reqtype
= REDIS_REQ_MULTIBULK
;
982 c
->reqtype
= REDIS_REQ_INLINE
;
986 if (c
->reqtype
== REDIS_REQ_INLINE
) {
987 if (processInlineBuffer(c
) != REDIS_OK
) break;
988 } else if (c
->reqtype
== REDIS_REQ_MULTIBULK
) {
989 if (processMultibulkBuffer(c
) != REDIS_OK
) break;
991 redisPanic("Unknown request type");
994 /* Multibulk processing could see a <= 0 length. */
998 /* Only reset the client when the command was executed. */
999 if (processCommand(c
) == REDIS_OK
)
1005 void readQueryFromClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
1006 redisClient
*c
= (redisClient
*) privdata
;
1010 REDIS_NOTUSED(mask
);
1012 server
.current_client
= c
;
1013 readlen
= REDIS_IOBUF_LEN
;
1014 /* If this is a multi bulk request, and we are processing a bulk reply
1015 * that is large enough, try to maximize the probabilty that the query
1016 * buffer contains excatly the SDS string representing the object, even
1017 * at the risk of requring more read(2) calls. This way the function
1018 * processMultiBulkBuffer() can avoid copying buffers to create the
1019 * Redis Object representing the argument. */
1020 if (c
->reqtype
== REDIS_REQ_MULTIBULK
&& c
->multibulklen
&& c
->bulklen
!= -1
1021 && c
->bulklen
>= REDIS_MBULK_BIG_ARG
)
1023 int remaining
= (unsigned)(c
->bulklen
+2)-sdslen(c
->querybuf
);
1025 if (remaining
< readlen
) readlen
= remaining
;
1028 qblen
= sdslen(c
->querybuf
);
1029 c
->querybuf
= sdsMakeRoomFor(c
->querybuf
, readlen
);
1030 nread
= read(fd
, c
->querybuf
+qblen
, readlen
);
1032 if (errno
== EAGAIN
) {
1035 redisLog(REDIS_VERBOSE
, "Reading from client: %s",strerror(errno
));
1039 } else if (nread
== 0) {
1040 redisLog(REDIS_VERBOSE
, "Client closed connection");
1045 sdsIncrLen(c
->querybuf
,nread
);
1046 c
->lastinteraction
= time(NULL
);
1048 server
.current_client
= NULL
;
1051 if (sdslen(c
->querybuf
) > server
.client_max_querybuf_len
) {
1052 sds ci
= getClientInfoString(c
), bytes
= sdsempty();
1054 bytes
= sdscatrepr(bytes
,c
->querybuf
,64);
1055 redisLog(REDIS_WARNING
,"Closing client that reached max query buffer length: %s (qbuf initial bytes: %s)", ci
, bytes
);
1061 processInputBuffer(c
);
1062 server
.current_client
= NULL
;
1065 void getClientsMaxBuffers(unsigned long *longest_output_list
,
1066 unsigned long *biggest_input_buffer
) {
1070 unsigned long lol
= 0, bib
= 0;
1072 listRewind(server
.clients
,&li
);
1073 while ((ln
= listNext(&li
)) != NULL
) {
1074 c
= listNodeValue(ln
);
1076 if (listLength(c
->reply
) > lol
) lol
= listLength(c
->reply
);
1077 if (sdslen(c
->querybuf
) > bib
) bib
= sdslen(c
->querybuf
);
1079 *longest_output_list
= lol
;
1080 *biggest_input_buffer
= bib
;
1083 /* Turn a Redis client into an sds string representing its state. */
1084 sds
getClientInfoString(redisClient
*client
) {
1085 char ip
[32], flags
[16], events
[3], *p
;
1087 time_t now
= time(NULL
);
1090 anetPeerToString(client
->fd
,ip
,&port
);
1092 if (client
->flags
& REDIS_SLAVE
) {
1093 if (client
->flags
& REDIS_MONITOR
)
1098 if (client
->flags
& REDIS_MASTER
) *p
++ = 'M';
1099 if (client
->flags
& REDIS_MULTI
) *p
++ = 'x';
1100 if (client
->flags
& REDIS_BLOCKED
) *p
++ = 'b';
1101 if (client
->flags
& REDIS_DIRTY_CAS
) *p
++ = 'd';
1102 if (client
->flags
& REDIS_CLOSE_AFTER_REPLY
) *p
++ = 'c';
1103 if (client
->flags
& REDIS_UNBLOCKED
) *p
++ = 'u';
1104 if (client
->flags
& REDIS_CLOSE_ASAP
) *p
++ = 'A';
1105 if (p
== flags
) *p
++ = 'N';
1108 emask
= client
->fd
== -1 ? 0 : aeGetFileEvents(server
.el
,client
->fd
);
1110 if (emask
& AE_READABLE
) *p
++ = 'r';
1111 if (emask
& AE_WRITABLE
) *p
++ = 'w';
1113 return sdscatprintf(sdsempty(),
1114 "addr=%s:%d fd=%d idle=%ld flags=%s db=%d sub=%d psub=%d qbuf=%lu obl=%lu oll=%lu omem=%lu events=%s cmd=%s",
1116 (long)(now
- client
->lastinteraction
),
1119 (int) dictSize(client
->pubsub_channels
),
1120 (int) listLength(client
->pubsub_patterns
),
1121 (unsigned long) sdslen(client
->querybuf
),
1122 (unsigned long) client
->bufpos
,
1123 (unsigned long) listLength(client
->reply
),
1124 getClientOutputBufferMemoryUsage(client
),
1126 client
->lastcmd
? client
->lastcmd
->name
: "NULL");
1129 sds
getAllClientsInfoString(void) {
1132 redisClient
*client
;
1135 listRewind(server
.clients
,&li
);
1136 while ((ln
= listNext(&li
)) != NULL
) {
1139 client
= listNodeValue(ln
);
1140 cs
= getClientInfoString(client
);
1141 o
= sdscatsds(o
,cs
);
1143 o
= sdscatlen(o
,"\n",1);
1148 void clientCommand(redisClient
*c
) {
1151 redisClient
*client
;
1153 if (!strcasecmp(c
->argv
[1]->ptr
,"list") && c
->argc
== 2) {
1154 sds o
= getAllClientsInfoString();
1155 addReplyBulkCBuffer(c
,o
,sdslen(o
));
1157 } else if (!strcasecmp(c
->argv
[1]->ptr
,"kill") && c
->argc
== 3) {
1158 listRewind(server
.clients
,&li
);
1159 while ((ln
= listNext(&li
)) != NULL
) {
1160 char ip
[32], addr
[64];
1163 client
= listNodeValue(ln
);
1164 if (anetPeerToString(client
->fd
,ip
,&port
) == -1) continue;
1165 snprintf(addr
,sizeof(addr
),"%s:%d",ip
,port
);
1166 if (strcmp(addr
,c
->argv
[2]->ptr
) == 0) {
1167 addReply(c
,shared
.ok
);
1169 client
->flags
|= REDIS_CLOSE_AFTER_REPLY
;
1176 addReplyError(c
,"No such client");
1178 addReplyError(c
, "Syntax error, try CLIENT (LIST | KILL ip:port)");
1182 /* Rewrite the command vector of the client. All the new objects ref count
1183 * is incremented. The old command vector is freed, and the old objects
1184 * ref count is decremented. */
1185 void rewriteClientCommandVector(redisClient
*c
, int argc
, ...) {
1188 robj
**argv
; /* The new argument vector */
1190 argv
= zmalloc(sizeof(robj
*)*argc
);
1192 for (j
= 0; j
< argc
; j
++) {
1195 a
= va_arg(ap
, robj
*);
1199 /* We free the objects in the original vector at the end, so we are
1200 * sure that if the same objects are reused in the new vector the
1201 * refcount gets incremented before it gets decremented. */
1202 for (j
= 0; j
< c
->argc
; j
++) decrRefCount(c
->argv
[j
]);
1204 /* Replace argv and argc with our new versions. */
1207 c
->cmd
= lookupCommand(c
->argv
[0]->ptr
);
1208 redisAssertWithInfo(c
,NULL
,c
->cmd
!= NULL
);
1212 /* Rewrite a single item in the command vector.
1213 * The new val ref count is incremented, and the old decremented. */
1214 void rewriteClientCommandArgument(redisClient
*c
, int i
, robj
*newval
) {
1217 redisAssertWithInfo(c
,NULL
,i
< c
->argc
);
1218 oldval
= c
->argv
[i
];
1219 c
->argv
[i
] = newval
;
1220 incrRefCount(newval
);
1221 decrRefCount(oldval
);
1223 /* If this is the command name make sure to fix c->cmd. */
1225 c
->cmd
= lookupCommand(c
->argv
[0]->ptr
);
1226 redisAssertWithInfo(c
,NULL
,c
->cmd
!= NULL
);
1230 /* This function returns the number of bytes that Redis is virtually
1231 * using to store the reply still not read by the client.
1232 * It is "virtual" since the reply output list may contain objects that
1233 * are shared and are not really using additional memory.
1235 * The function returns the total sum of the length of all the objects
1236 * stored in the output list, plus the memory used to allocate every
1237 * list node. The static reply buffer is not taken into account since it
1238 * is allocated anyway.
1240 * Note: this function is very fast so can be called as many time as
1241 * the caller wishes. The main usage of this function currently is
1242 * enforcing the client output length limits. */
1243 unsigned long getClientOutputBufferMemoryUsage(redisClient
*c
) {
1244 unsigned long list_item_size
= sizeof(listNode
)+sizeof(robj
);
1246 return c
->reply_bytes
+ (list_item_size
*listLength(c
->reply
));
1249 /* Get the class of a client, used in order to envorce limits to different
1250 * classes of clients.
1252 * The function will return one of the following:
1253 * REDIS_CLIENT_LIMIT_CLASS_NORMAL -> Normal client
1254 * REDIS_CLIENT_LIMIT_CLASS_SLAVE -> Slave or client executing MONITOR command
1255 * REDIS_CLIENT_LIMIT_CLASS_PUBSUB -> Client subscribed to Pub/Sub channels
1257 int getClientLimitClass(redisClient
*c
) {
1258 if (c
->flags
& REDIS_SLAVE
) return REDIS_CLIENT_LIMIT_CLASS_SLAVE
;
1259 if (dictSize(c
->pubsub_channels
) || listLength(c
->pubsub_patterns
))
1260 return REDIS_CLIENT_LIMIT_CLASS_PUBSUB
;
1261 return REDIS_CLIENT_LIMIT_CLASS_NORMAL
;
1264 int getClientLimitClassByName(char *name
) {
1265 if (!strcasecmp(name
,"normal")) return REDIS_CLIENT_LIMIT_CLASS_NORMAL
;
1266 else if (!strcasecmp(name
,"slave")) return REDIS_CLIENT_LIMIT_CLASS_SLAVE
;
1267 else if (!strcasecmp(name
,"pubsub")) return REDIS_CLIENT_LIMIT_CLASS_PUBSUB
;
1271 char *getClientLimitClassName(int class) {
1273 case REDIS_CLIENT_LIMIT_CLASS_NORMAL
: return "normal";
1274 case REDIS_CLIENT_LIMIT_CLASS_SLAVE
: return "slave";
1275 case REDIS_CLIENT_LIMIT_CLASS_PUBSUB
: return "pubsub";
1276 default: return NULL
;
1280 /* The function checks if the client reached output buffer soft or hard
1281 * limit, and also update the state needed to check the soft limit as
1284 * Return value: non-zero if the client reached the soft or the hard limit.
1285 * Otherwise zero is returned. */
1286 int checkClientOutputBufferLimits(redisClient
*c
) {
1287 int soft
= 0, hard
= 0, class;
1288 unsigned long used_mem
= getClientOutputBufferMemoryUsage(c
);
1290 class = getClientLimitClass(c
);
1291 if (server
.client_obuf_limits
[class].hard_limit_bytes
&&
1292 used_mem
>= server
.client_obuf_limits
[class].hard_limit_bytes
)
1294 if (server
.client_obuf_limits
[class].soft_limit_bytes
&&
1295 used_mem
>= server
.client_obuf_limits
[class].soft_limit_bytes
)
1298 /* We need to check if the soft limit is reached continuously for the
1299 * specified amount of seconds. */
1301 if (c
->obuf_soft_limit_reached_time
== 0) {
1302 c
->obuf_soft_limit_reached_time
= server
.unixtime
;
1303 soft
= 0; /* First time we see the soft limit reached */
1305 time_t elapsed
= server
.unixtime
- c
->obuf_soft_limit_reached_time
;
1308 server
.client_obuf_limits
[class].soft_limit_seconds
) {
1309 soft
= 0; /* The client still did not reached the max number of
1310 seconds for the soft limit to be considered
1315 c
->obuf_soft_limit_reached_time
= 0;
1317 return soft
|| hard
;
1320 /* Asynchronously close a client if soft or hard limit is reached on the
1321 * output buffer size. The caller can check if the client will be closed
1322 * checking if the client REDIS_CLOSE_ASAP flag is set.
1324 * Note: we need to close the client asynchronously because this function is
1325 * called from contexts where the client can't be freed safely, i.e. from the
1326 * lower level functions pushing data inside the client output buffers. */
1327 void asyncCloseClientOnOutputBufferLimitReached(redisClient
*c
) {
1328 if (c
->reply_bytes
== 0 || c
->flags
& REDIS_CLOSE_ASAP
) return;
1329 if (checkClientOutputBufferLimits(c
)) {
1330 sds client
= getClientInfoString(c
);
1333 redisLog(REDIS_WARNING
,"Client %s scheduled to be closed ASAP for overcoming of output buffer limits.", client
);
1338 /* Helper function used by freeMemoryIfNeeded() in order to flush slaves
1339 * output buffers without returning control to the event loop. */
1340 void flushSlavesOutputBuffers(void) {
1344 listRewind(server
.slaves
,&li
);
1345 while((ln
= listNext(&li
))) {
1346 redisClient
*slave
= listNodeValue(ln
);
1349 events
= aeGetFileEvents(server
.el
,slave
->fd
);
1350 if (events
& AE_WRITABLE
&&
1351 slave
->replstate
== REDIS_REPL_ONLINE
&&
1352 listLength(slave
->reply
))
1354 sendReplyToClient(server
.el
,slave
->fd
,slave
,0);