]> git.saurik.com Git - redis.git/blame - src/networking.c
variadic SADD
[redis.git] / src / networking.c
CommitLineData
e2641e09 1#include "redis.h"
e2641e09 2#include <sys/uio.h>
3
4void *dupClientReplyValue(void *o) {
5 incrRefCount((robj*)o);
6 return o;
7}
8
9int listMatchObjects(void *a, void *b) {
10 return equalStringObjects(a,b);
11}
12
13redisClient *createClient(int fd) {
f3357792 14 redisClient *c = zmalloc(sizeof(redisClient));
834ef78e 15 c->bufpos = 0;
e2641e09 16
17 anetNonBlock(NULL,fd);
18 anetTcpNoDelay(NULL,fd);
19 if (!c) return NULL;
106bd87a
PN
20 if (aeCreateFileEvent(server.el,fd,AE_READABLE,
21 readQueryFromClient, c) == AE_ERR)
22 {
23 close(fd);
24 zfree(c);
25 return NULL;
26 }
27
e2641e09 28 selectDb(c,0);
29 c->fd = fd;
30 c->querybuf = sdsempty();
cd8788f2 31 c->reqtype = 0;
e2641e09 32 c->argc = 0;
33 c->argv = NULL;
cd8788f2 34 c->multibulklen = 0;
e2641e09 35 c->bulklen = -1;
e2641e09 36 c->sentlen = 0;
37 c->flags = 0;
38 c->lastinteraction = time(NULL);
39 c->authenticated = 0;
40 c->replstate = REDIS_REPL_NONE;
41 c->reply = listCreate();
42 listSetFreeMethod(c->reply,decrRefCount);
43 listSetDupMethod(c->reply,dupClientReplyValue);
e3c51c4b
DJMM
44 c->bpop.keys = NULL;
45 c->bpop.count = 0;
46 c->bpop.timeout = 0;
47 c->bpop.target = NULL;
e2641e09 48 c->io_keys = listCreate();
49 c->watched_keys = listCreate();
50 listSetFreeMethod(c->io_keys,decrRefCount);
51 c->pubsub_channels = dictCreate(&setDictType,NULL);
52 c->pubsub_patterns = listCreate();
53 listSetFreeMethod(c->pubsub_patterns,decrRefCount);
54 listSetMatchMethod(c->pubsub_patterns,listMatchObjects);
e2641e09 55 listAddNodeTail(server.clients,c);
56 initClientMultiState(c);
57 return c;
58}
59
a3a323e0
PN
60/* Set the event loop to listen for write events on the client's socket.
61 * Typically gets called every time a reply is built. */
4c2e506a 62int _installWriteEvent(redisClient *c) {
57b07380 63 if (c->fd <= 0) return REDIS_ERR;
834ef78e 64 if (c->bufpos == 0 && listLength(c->reply) == 0 &&
e2641e09 65 (c->replstate == REDIS_REPL_NONE ||
66 c->replstate == REDIS_REPL_ONLINE) &&
67 aeCreateFileEvent(server.el, c->fd, AE_WRITABLE,
834ef78e
PN
68 sendReplyToClient, c) == AE_ERR) return REDIS_ERR;
69 return REDIS_OK;
70}
71
36c19d03
PN
72/* Create a duplicate of the last object in the reply list when
73 * it is not exclusively owned by the reply list. */
74robj *dupLastObjectIfNeeded(list *reply) {
75 robj *new, *cur;
76 listNode *ln;
77 redisAssert(listLength(reply) > 0);
78 ln = listLast(reply);
79 cur = listNodeValue(ln);
80 if (cur->refcount > 1) {
81 new = dupStringObject(cur);
82 decrRefCount(cur);
83 listNodeValue(ln) = new;
84 }
85 return listNodeValue(ln);
834ef78e
PN
86}
87
25ef3192 88/* -----------------------------------------------------------------------------
89 * Low level functions to add more data to output buffers.
90 * -------------------------------------------------------------------------- */
91
36c19d03 92int _addReplyToBuffer(redisClient *c, char *s, size_t len) {
f3357792 93 size_t available = sizeof(c->buf)-c->bufpos;
36c19d03 94
25ef3192 95 if (c->flags & REDIS_CLOSE_AFTER_REPLY) return REDIS_OK;
96
36c19d03
PN
97 /* If there already are entries in the reply list, we cannot
98 * add anything more to the static buffer. */
99 if (listLength(c->reply) > 0) return REDIS_ERR;
100
101 /* Check that the buffer has enough space available for this string. */
102 if (len > available) return REDIS_ERR;
e2641e09 103
36c19d03
PN
104 memcpy(c->buf+c->bufpos,s,len);
105 c->bufpos+=len;
106 return REDIS_OK;
834ef78e
PN
107}
108
36c19d03
PN
109void _addReplyObjectToList(redisClient *c, robj *o) {
110 robj *tail;
25ef3192 111
112 if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;
113
36c19d03
PN
114 if (listLength(c->reply) == 0) {
115 incrRefCount(o);
116 listAddNodeTail(c->reply,o);
117 } else {
118 tail = listNodeValue(listLast(c->reply));
119
120 /* Append to this object when possible. */
121 if (tail->ptr != NULL &&
122 sdslen(tail->ptr)+sdslen(o->ptr) <= REDIS_REPLY_CHUNK_BYTES)
123 {
124 tail = dupLastObjectIfNeeded(c->reply);
125 tail->ptr = sdscatlen(tail->ptr,o->ptr,sdslen(o->ptr));
126 } else {
127 incrRefCount(o);
128 listAddNodeTail(c->reply,o);
129 }
130 }
131}
834ef78e 132
36c19d03
PN
133/* This method takes responsibility over the sds. When it is no longer
134 * needed it will be free'd, otherwise it ends up in a robj. */
135void _addReplySdsToList(redisClient *c, sds s) {
136 robj *tail;
25ef3192 137
5b94b8ac 138 if (c->flags & REDIS_CLOSE_AFTER_REPLY) {
139 sdsfree(s);
140 return;
141 }
25ef3192 142
36c19d03
PN
143 if (listLength(c->reply) == 0) {
144 listAddNodeTail(c->reply,createObject(REDIS_STRING,s));
145 } else {
146 tail = listNodeValue(listLast(c->reply));
147
148 /* Append to this object when possible. */
149 if (tail->ptr != NULL &&
150 sdslen(tail->ptr)+sdslen(s) <= REDIS_REPLY_CHUNK_BYTES)
151 {
152 tail = dupLastObjectIfNeeded(c->reply);
153 tail->ptr = sdscatlen(tail->ptr,s,sdslen(s));
154 sdsfree(s);
834ef78e 155 } else {
36c19d03 156 listAddNodeTail(c->reply,createObject(REDIS_STRING,s));
834ef78e 157 }
36c19d03
PN
158 }
159}
160
161void _addReplyStringToList(redisClient *c, char *s, size_t len) {
162 robj *tail;
25ef3192 163
164 if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;
165
36c19d03
PN
166 if (listLength(c->reply) == 0) {
167 listAddNodeTail(c->reply,createStringObject(s,len));
834ef78e 168 } else {
36c19d03
PN
169 tail = listNodeValue(listLast(c->reply));
170
171 /* Append to this object when possible. */
172 if (tail->ptr != NULL &&
173 sdslen(tail->ptr)+len <= REDIS_REPLY_CHUNK_BYTES)
174 {
175 tail = dupLastObjectIfNeeded(c->reply);
176 tail->ptr = sdscatlen(tail->ptr,s,len);
834ef78e 177 } else {
36c19d03 178 listAddNodeTail(c->reply,createStringObject(s,len));
834ef78e
PN
179 }
180 }
181}
e2641e09 182
25ef3192 183/* -----------------------------------------------------------------------------
184 * Higher level functions to queue data on the client output buffer.
185 * The following functions are the ones that commands implementations will call.
186 * -------------------------------------------------------------------------- */
187
834ef78e 188void addReply(redisClient *c, robj *obj) {
4c2e506a 189 if (_installWriteEvent(c) != REDIS_OK) return;
4c2e506a 190
191 /* This is an important place where we can avoid copy-on-write
192 * when there is a saving child running, avoiding touching the
193 * refcount field of the object if it's not needed.
194 *
195 * If the encoding is RAW and there is room in the static buffer
196 * we'll be able to send the object to the client without
197 * messing with its page. */
198 if (obj->encoding == REDIS_ENCODING_RAW) {
199 if (_addReplyToBuffer(c,obj->ptr,sdslen(obj->ptr)) != REDIS_OK)
200 _addReplyObjectToList(c,obj);
834ef78e 201 } else {
d51ebef5 202 /* FIXME: convert the long into string and use _addReplyToBuffer()
203 * instead of calling getDecodedObject. As this place in the
204 * code is too performance critical. */
834ef78e 205 obj = getDecodedObject(obj);
4c2e506a 206 if (_addReplyToBuffer(c,obj->ptr,sdslen(obj->ptr)) != REDIS_OK)
207 _addReplyObjectToList(c,obj);
208 decrRefCount(obj);
e2641e09 209 }
e2641e09 210}
211
212void addReplySds(redisClient *c, sds s) {
4c2e506a 213 if (_installWriteEvent(c) != REDIS_OK) {
cd76bb65
PN
214 /* The caller expects the sds to be free'd. */
215 sdsfree(s);
216 return;
217 }
36c19d03 218 if (_addReplyToBuffer(c,s,sdslen(s)) == REDIS_OK) {
834ef78e
PN
219 sdsfree(s);
220 } else {
36c19d03
PN
221 /* This method free's the sds when it is no longer needed. */
222 _addReplySdsToList(c,s);
834ef78e 223 }
e2641e09 224}
225
834ef78e 226void addReplyString(redisClient *c, char *s, size_t len) {
4c2e506a 227 if (_installWriteEvent(c) != REDIS_OK) return;
36c19d03
PN
228 if (_addReplyToBuffer(c,s,len) != REDIS_OK)
229 _addReplyStringToList(c,s,len);
834ef78e 230}
e2641e09 231
3ab20376
PN
232void _addReplyError(redisClient *c, char *s, size_t len) {
233 addReplyString(c,"-ERR ",5);
234 addReplyString(c,s,len);
235 addReplyString(c,"\r\n",2);
e2641e09 236}
237
3ab20376
PN
238void addReplyError(redisClient *c, char *err) {
239 _addReplyError(c,err,strlen(err));
240}
e2641e09 241
3ab20376
PN
242void addReplyErrorFormat(redisClient *c, const char *fmt, ...) {
243 va_list ap;
244 va_start(ap,fmt);
245 sds s = sdscatvprintf(sdsempty(),fmt,ap);
246 va_end(ap);
247 _addReplyError(c,s,sdslen(s));
248 sdsfree(s);
249}
250
251void _addReplyStatus(redisClient *c, char *s, size_t len) {
252 addReplyString(c,"+",1);
253 addReplyString(c,s,len);
254 addReplyString(c,"\r\n",2);
255}
256
257void addReplyStatus(redisClient *c, char *status) {
258 _addReplyStatus(c,status,strlen(status));
259}
260
261void addReplyStatusFormat(redisClient *c, const char *fmt, ...) {
262 va_list ap;
263 va_start(ap,fmt);
264 sds s = sdscatvprintf(sdsempty(),fmt,ap);
265 va_end(ap);
266 _addReplyStatus(c,s,sdslen(s));
267 sdsfree(s);
268}
269
b301c1fc
PN
270/* Adds an empty object to the reply list that will contain the multi bulk
271 * length, which is not known when this function is called. */
272void *addDeferredMultiBulkLength(redisClient *c) {
4c2e506a 273 /* Note that we install the write event here even if the object is not
274 * ready to be sent, since we are sure that before returning to the
275 * event loop setDeferredMultiBulkLength() will be called. */
276 if (_installWriteEvent(c) != REDIS_OK) return NULL;
36c19d03 277 listAddNodeTail(c->reply,createObject(REDIS_STRING,NULL));
b301c1fc
PN
278 return listLast(c->reply);
279}
280
281/* Populate the length object and try glueing it to the next chunk. */
282void setDeferredMultiBulkLength(redisClient *c, void *node, long length) {
283 listNode *ln = (listNode*)node;
284 robj *len, *next;
285
286 /* Abort when *node is NULL (see addDeferredMultiBulkLength). */
287 if (node == NULL) return;
288
289 len = listNodeValue(ln);
290 len->ptr = sdscatprintf(sdsempty(),"*%ld\r\n",length);
291 if (ln->next != NULL) {
292 next = listNodeValue(ln->next);
36c19d03 293
49128f0b 294 /* Only glue when the next node is non-NULL (an sds in this case) */
36c19d03 295 if (next->ptr != NULL) {
49128f0b 296 len->ptr = sdscatlen(len->ptr,next->ptr,sdslen(next->ptr));
b301c1fc
PN
297 listDelNode(c->reply,ln->next);
298 }
e2641e09 299 }
b301c1fc
PN
300}
301
d51ebef5 302/* Add a duble as a bulk reply */
834ef78e
PN
303void addReplyDouble(redisClient *c, double d) {
304 char dbuf[128], sbuf[128];
305 int dlen, slen;
306 dlen = snprintf(dbuf,sizeof(dbuf),"%.17g",d);
307 slen = snprintf(sbuf,sizeof(sbuf),"$%d\r\n%s\r\n",dlen,dbuf);
308 addReplyString(c,sbuf,slen);
e2641e09 309}
310
d51ebef5 311/* Add a long long as integer reply or bulk len / multi bulk count.
312 * Basically this is used to output <prefix><long long><crlf>. */
834ef78e 313void _addReplyLongLong(redisClient *c, long long ll, char prefix) {
e2641e09 314 char buf[128];
834ef78e
PN
315 int len;
316 buf[0] = prefix;
e2641e09 317 len = ll2string(buf+1,sizeof(buf)-1,ll);
318 buf[len+1] = '\r';
319 buf[len+2] = '\n';
834ef78e 320 addReplyString(c,buf,len+3);
e2641e09 321}
322
834ef78e 323void addReplyLongLong(redisClient *c, long long ll) {
009db676 324 if (ll == 0)
325 addReply(c,shared.czero);
326 else if (ll == 1)
327 addReply(c,shared.cone);
328 else
329 _addReplyLongLong(c,ll,':');
834ef78e 330}
e2641e09 331
0537e7bf
PN
332void addReplyMultiBulkLen(redisClient *c, long length) {
333 _addReplyLongLong(c,length,'*');
e2641e09 334}
335
d51ebef5 336/* Create the length prefix of a bulk reply, example: $2234 */
e2641e09 337void addReplyBulkLen(redisClient *c, robj *obj) {
834ef78e 338 size_t len;
e2641e09 339
340 if (obj->encoding == REDIS_ENCODING_RAW) {
341 len = sdslen(obj->ptr);
342 } else {
343 long n = (long)obj->ptr;
344
345 /* Compute how many bytes will take this integer as a radix 10 string */
346 len = 1;
347 if (n < 0) {
348 len++;
349 n = -n;
350 }
351 while((n = n/10) != 0) {
352 len++;
353 }
354 }
834ef78e 355 _addReplyLongLong(c,len,'$');
e2641e09 356}
357
d51ebef5 358/* Add a Redis Object as a bulk reply */
e2641e09 359void addReplyBulk(redisClient *c, robj *obj) {
360 addReplyBulkLen(c,obj);
361 addReply(c,obj);
362 addReply(c,shared.crlf);
363}
364
d51ebef5 365/* Add a C buffer as bulk reply */
366void addReplyBulkCBuffer(redisClient *c, void *p, size_t len) {
367 _addReplyLongLong(c,len,'$');
368 addReplyString(c,p,len);
369 addReply(c,shared.crlf);
370}
371
372/* Add a C nul term string as bulk reply */
e2641e09 373void addReplyBulkCString(redisClient *c, char *s) {
374 if (s == NULL) {
375 addReply(c,shared.nullbulk);
376 } else {
d51ebef5 377 addReplyBulkCBuffer(c,s,strlen(s));
e2641e09 378 }
379}
380
d51ebef5 381/* Add a long long as a bulk reply */
382void addReplyBulkLongLong(redisClient *c, long long ll) {
383 char buf[64];
384 int len;
385
386 len = ll2string(buf,64,ll);
387 addReplyBulkCBuffer(c,buf,len);
388}
389
ab17b909 390static void acceptCommonHandler(int fd) {
e2641e09 391 redisClient *c;
ab17b909 392 if ((c = createClient(fd)) == NULL) {
e2641e09 393 redisLog(REDIS_WARNING,"Error allocating resoures for the client");
ab17b909 394 close(fd); /* May be already closed, just ingore errors */
e2641e09 395 return;
396 }
397 /* If maxclient directive is set and this is one client more... close the
398 * connection. Note that we create the client instead to check before
399 * for this condition, since now the socket is already set in nonblocking
400 * mode and we can send an error for free using the Kernel I/O */
401 if (server.maxclients && listLength(server.clients) > server.maxclients) {
402 char *err = "-ERR max number of clients reached\r\n";
403
404 /* That's a best effort error message, don't check write errors */
405 if (write(c->fd,err,strlen(err)) == -1) {
406 /* Nothing to do, Just to avoid the warning... */
407 }
408 freeClient(c);
409 return;
410 }
411 server.stat_numconnections++;
412}
413
ab17b909
PN
414void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
415 int cport, cfd;
416 char cip[128];
417 REDIS_NOTUSED(el);
418 REDIS_NOTUSED(mask);
419 REDIS_NOTUSED(privdata);
420
421 cfd = anetTcpAccept(server.neterr, fd, cip, &cport);
422 if (cfd == AE_ERR) {
423 redisLog(REDIS_VERBOSE,"Accepting client connection: %s", server.neterr);
424 return;
425 }
426 redisLog(REDIS_VERBOSE,"Accepted %s:%d", cip, cport);
427 acceptCommonHandler(cfd);
428}
429
430void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
431 int cfd;
ab17b909
PN
432 REDIS_NOTUSED(el);
433 REDIS_NOTUSED(mask);
434 REDIS_NOTUSED(privdata);
435
4fe83b55 436 cfd = anetUnixAccept(server.neterr, fd);
ab17b909
PN
437 if (cfd == AE_ERR) {
438 redisLog(REDIS_VERBOSE,"Accepting client connection: %s", server.neterr);
439 return;
440 }
441 redisLog(REDIS_VERBOSE,"Accepted connection to %s", server.unixsocket);
442 acceptCommonHandler(cfd);
443}
444
445
e2641e09 446static void freeClientArgv(redisClient *c) {
447 int j;
e2641e09 448 for (j = 0; j < c->argc; j++)
449 decrRefCount(c->argv[j]);
e2641e09 450 c->argc = 0;
e2641e09 451}
452
453void freeClient(redisClient *c) {
454 listNode *ln;
455
456 /* Note that if the client we are freeing is blocked into a blocking
457 * call, we have to set querybuf to NULL *before* to call
458 * unblockClientWaitingData() to avoid processInputBuffer() will get
459 * called. Also it is important to remove the file events after
460 * this, because this call adds the READABLE event. */
461 sdsfree(c->querybuf);
462 c->querybuf = NULL;
463 if (c->flags & REDIS_BLOCKED)
464 unblockClientWaitingData(c);
465
466 /* UNWATCH all the keys */
467 unwatchAllKeys(c);
468 listRelease(c->watched_keys);
469 /* Unsubscribe from all the pubsub channels */
470 pubsubUnsubscribeAllChannels(c,0);
471 pubsubUnsubscribeAllPatterns(c,0);
472 dictRelease(c->pubsub_channels);
473 listRelease(c->pubsub_patterns);
474 /* Obvious cleanup */
475 aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
476 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
477 listRelease(c->reply);
478 freeClientArgv(c);
479 close(c->fd);
480 /* Remove from the list of clients */
481 ln = listSearchKey(server.clients,c);
482 redisAssert(ln != NULL);
483 listDelNode(server.clients,ln);
3bcffcbe
PN
484 /* When client was just unblocked because of a blocking operation,
485 * remove it from the list with unblocked clients. */
486 if (c->flags & REDIS_UNBLOCKED) {
487 ln = listSearchKey(server.unblocked_clients,c);
488 redisAssert(ln != NULL);
489 listDelNode(server.unblocked_clients,ln);
490 }
1a71fb96 491 /* Remove from the list of clients waiting for swapped keys, or ready
492 * to be restarted, but not yet woken up again. */
493 if (c->flags & REDIS_IO_WAIT) {
697af434 494 redisAssert(server.ds_enabled);
1a71fb96 495 if (listLength(c->io_keys) == 0) {
496 ln = listSearchKey(server.io_ready_clients,c);
497
498 /* When this client is waiting to be woken up (REDIS_IO_WAIT),
499 * it should be present in the list io_ready_clients */
500 redisAssert(ln != NULL);
e2641e09 501 listDelNode(server.io_ready_clients,ln);
1a71fb96 502 } else {
503 while (listLength(c->io_keys)) {
504 ln = listFirst(c->io_keys);
505 dontWaitForSwappedKey(c,ln->value);
506 }
e2641e09 507 }
697af434 508 server.cache_blocked_clients--;
e2641e09 509 }
510 listRelease(c->io_keys);
778b2210 511 /* Master/slave cleanup.
512 * Case 1: we lost the connection with a slave. */
e2641e09 513 if (c->flags & REDIS_SLAVE) {
514 if (c->replstate == REDIS_REPL_SEND_BULK && c->repldbfd != -1)
515 close(c->repldbfd);
516 list *l = (c->flags & REDIS_MONITOR) ? server.monitors : server.slaves;
517 ln = listSearchKey(l,c);
518 redisAssert(ln != NULL);
519 listDelNode(l,ln);
520 }
778b2210 521
522 /* Case 2: we lost the connection with the master. */
e2641e09 523 if (c->flags & REDIS_MASTER) {
524 server.master = NULL;
525 server.replstate = REDIS_REPL_CONNECT;
778b2210 526 /* Since we lost the connection with the master, we should also
527 * close the connection with all our slaves if we have any, so
528 * when we'll resync with the master the other slaves will sync again
529 * with us as well. Note that also when the slave is not connected
530 * to the master it will keep refusing connections by other slaves. */
531 while (listLength(server.slaves)) {
532 ln = listFirst(server.slaves);
533 freeClient((redisClient*)ln->value);
534 }
e2641e09 535 }
536 /* Release memory */
537 zfree(c->argv);
e2641e09 538 freeClientMultiState(c);
539 zfree(c);
540}
541
e2641e09 542void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
543 redisClient *c = privdata;
544 int nwritten = 0, totwritten = 0, objlen;
545 robj *o;
546 REDIS_NOTUSED(el);
547 REDIS_NOTUSED(mask);
548
834ef78e
PN
549 while(c->bufpos > 0 || listLength(c->reply)) {
550 if (c->bufpos > 0) {
551 if (c->flags & REDIS_MASTER) {
552 /* Don't reply to a master */
553 nwritten = c->bufpos - c->sentlen;
554 } else {
555 nwritten = write(fd,c->buf+c->sentlen,c->bufpos-c->sentlen);
556 if (nwritten <= 0) break;
557 }
558 c->sentlen += nwritten;
559 totwritten += nwritten;
560
561 /* If the buffer was sent, set bufpos to zero to continue with
562 * the remainder of the reply. */
563 if (c->sentlen == c->bufpos) {
564 c->bufpos = 0;
565 c->sentlen = 0;
566 }
567 } else {
568 o = listNodeValue(listFirst(c->reply));
569 objlen = sdslen(o->ptr);
e2641e09 570
834ef78e
PN
571 if (objlen == 0) {
572 listDelNode(c->reply,listFirst(c->reply));
573 continue;
574 }
e2641e09 575
834ef78e
PN
576 if (c->flags & REDIS_MASTER) {
577 /* Don't reply to a master */
578 nwritten = objlen - c->sentlen;
579 } else {
580 nwritten = write(fd, ((char*)o->ptr)+c->sentlen,objlen-c->sentlen);
581 if (nwritten <= 0) break;
582 }
583 c->sentlen += nwritten;
584 totwritten += nwritten;
e2641e09 585
834ef78e
PN
586 /* If we fully sent the object on head go to the next one */
587 if (c->sentlen == objlen) {
588 listDelNode(c->reply,listFirst(c->reply));
589 c->sentlen = 0;
590 }
e2641e09 591 }
592 /* Note that we avoid to send more thank REDIS_MAX_WRITE_PER_EVENT
593 * bytes, in a single threaded server it's a good idea to serve
594 * other clients as well, even if a very large request comes from
595 * super fast link that is always able to accept data (in real world
596 * scenario think about 'KEYS *' against the loopback interfae) */
597 if (totwritten > REDIS_MAX_WRITE_PER_EVENT) break;
598 }
599 if (nwritten == -1) {
600 if (errno == EAGAIN) {
601 nwritten = 0;
602 } else {
603 redisLog(REDIS_VERBOSE,
604 "Error writing to client: %s", strerror(errno));
605 freeClient(c);
606 return;
607 }
608 }
609 if (totwritten > 0) c->lastinteraction = time(NULL);
610 if (listLength(c->reply) == 0) {
611 c->sentlen = 0;
612 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
941c9fa2
PN
613
614 /* Close connection after entire reply has been sent. */
cd8788f2 615 if (c->flags & REDIS_CLOSE_AFTER_REPLY) freeClient(c);
e2641e09 616 }
617}
618
e2641e09 619/* resetClient prepare the client to process the next command */
620void resetClient(redisClient *c) {
621 freeClientArgv(c);
cd8788f2
PN
622 c->reqtype = 0;
623 c->multibulklen = 0;
e2641e09 624 c->bulklen = -1;
e2641e09 625}
626
627void closeTimedoutClients(void) {
628 redisClient *c;
629 listNode *ln;
630 time_t now = time(NULL);
631 listIter li;
632
633 listRewind(server.clients,&li);
634 while ((ln = listNext(&li)) != NULL) {
635 c = listNodeValue(ln);
636 if (server.maxidletime &&
637 !(c->flags & REDIS_SLAVE) && /* no timeout for slaves */
638 !(c->flags & REDIS_MASTER) && /* no timeout for masters */
e452436a 639 !(c->flags & REDIS_BLOCKED) && /* no timeout for BLPOP */
e2641e09 640 dictSize(c->pubsub_channels) == 0 && /* no timeout for pubsub */
641 listLength(c->pubsub_patterns) == 0 &&
642 (now - c->lastinteraction > server.maxidletime))
643 {
644 redisLog(REDIS_VERBOSE,"Closing idle client");
645 freeClient(c);
646 } else if (c->flags & REDIS_BLOCKED) {
e3c51c4b 647 if (c->bpop.timeout != 0 && c->bpop.timeout < now) {
e2641e09 648 addReply(c,shared.nullmultibulk);
649 unblockClientWaitingData(c);
650 }
651 }
652 }
653}
654
cd8788f2
PN
655int processInlineBuffer(redisClient *c) {
656 char *newline = strstr(c->querybuf,"\r\n");
657 int argc, j;
658 sds *argv;
659 size_t querylen;
660
661 /* Nothing to do without a \r\n */
662 if (newline == NULL)
663 return REDIS_ERR;
664
665 /* Split the input buffer up to the \r\n */
666 querylen = newline-(c->querybuf);
667 argv = sdssplitlen(c->querybuf,querylen," ",1,&argc);
668
669 /* Leave data after the first line of the query in the buffer */
670 c->querybuf = sdsrange(c->querybuf,querylen+2,-1);
671
672 /* Setup argv array on client structure */
673 if (c->argv) zfree(c->argv);
674 c->argv = zmalloc(sizeof(robj*)*argc);
675
676 /* Create redis objects for all arguments. */
677 for (c->argc = 0, j = 0; j < argc; j++) {
678 if (sdslen(argv[j])) {
679 c->argv[c->argc] = createObject(REDIS_STRING,argv[j]);
680 c->argc++;
681 } else {
682 sdsfree(argv[j]);
683 }
684 }
685 zfree(argv);
686 return REDIS_OK;
687}
688
689/* Helper function. Trims query buffer to make the function that processes
690 * multi bulk requests idempotent. */
691static void setProtocolError(redisClient *c, int pos) {
692 c->flags |= REDIS_CLOSE_AFTER_REPLY;
693 c->querybuf = sdsrange(c->querybuf,pos,-1);
694}
695
696int processMultibulkBuffer(redisClient *c) {
697 char *newline = NULL;
698 char *eptr;
699 int pos = 0, tolerr;
700 long bulklen;
701
702 if (c->multibulklen == 0) {
703 /* The client should have been reset */
704 redisAssert(c->argc == 0);
705
706 /* Multi bulk length cannot be read without a \r\n */
707 newline = strstr(c->querybuf,"\r\n");
708 if (newline == NULL)
709 return REDIS_ERR;
710
711 /* We know for sure there is a whole line since newline != NULL,
712 * so go ahead and find out the multi bulk length. */
713 redisAssert(c->querybuf[0] == '*');
714 c->multibulklen = strtol(c->querybuf+1,&eptr,10);
715 pos = (newline-c->querybuf)+2;
716 if (c->multibulklen <= 0) {
717 c->querybuf = sdsrange(c->querybuf,pos,-1);
718 return REDIS_OK;
b19c33d4
PN
719 } else if (c->multibulklen > 1024*1024) {
720 addReplyError(c,"Protocol error: invalid multibulk length");
721 setProtocolError(c,pos);
722 return REDIS_ERR;
cd8788f2
PN
723 }
724
725 /* Setup argv array on client structure */
726 if (c->argv) zfree(c->argv);
727 c->argv = zmalloc(sizeof(robj*)*c->multibulklen);
728
729 /* Search new newline */
730 newline = strstr(c->querybuf+pos,"\r\n");
731 }
732
733 redisAssert(c->multibulklen > 0);
734 while(c->multibulklen) {
735 /* Read bulk length if unknown */
736 if (c->bulklen == -1) {
737 newline = strstr(c->querybuf+pos,"\r\n");
738 if (newline != NULL) {
739 if (c->querybuf[pos] != '$') {
740 addReplyErrorFormat(c,
741 "Protocol error: expected '$', got '%c'",
742 c->querybuf[pos]);
743 setProtocolError(c,pos);
744 return REDIS_ERR;
e2641e09 745 }
cd8788f2
PN
746
747 bulklen = strtol(c->querybuf+pos+1,&eptr,10);
748 tolerr = (eptr[0] != '\r');
749 if (tolerr || bulklen == LONG_MIN || bulklen == LONG_MAX ||
401c3e21 750 bulklen < 0 || bulklen > 512*1024*1024)
cd8788f2
PN
751 {
752 addReplyError(c,"Protocol error: invalid bulk length");
753 setProtocolError(c,pos);
754 return REDIS_ERR;
755 }
756 pos += eptr-(c->querybuf+pos)+2;
757 c->bulklen = bulklen;
758 } else {
759 /* No newline in current buffer, so wait for more data */
760 break;
e2641e09 761 }
cd8788f2
PN
762 }
763
764 /* Read bulk argument */
765 if (sdslen(c->querybuf)-pos < (unsigned)(c->bulklen+2)) {
766 /* Not enough data (+2 == trailing \r\n) */
767 break;
768 } else {
769 c->argv[c->argc++] = createStringObject(c->querybuf+pos,c->bulklen);
770 pos += c->bulklen+2;
771 c->bulklen = -1;
772 c->multibulklen--;
773 }
774 }
775
776 /* Trim to pos */
777 c->querybuf = sdsrange(c->querybuf,pos,-1);
778
779 /* We're done when c->multibulk == 0 */
780 if (c->multibulklen == 0) {
781 return REDIS_OK;
782 }
783 return REDIS_ERR;
784}
785
786void processInputBuffer(redisClient *c) {
787 /* Keep processing while there is something in the input buffer */
788 while(sdslen(c->querybuf)) {
4794d88f 789 /* Immediately abort if the client is in the middle of something. */
cd8788f2
PN
790 if (c->flags & REDIS_BLOCKED || c->flags & REDIS_IO_WAIT) return;
791
5e78edb3
PN
792 /* REDIS_CLOSE_AFTER_REPLY closes the connection once the reply is
793 * written to the client. Make sure to not let the reply grow after
794 * this flag has been set (i.e. don't process more commands). */
795 if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;
cd8788f2
PN
796
797 /* Determine request type when unknown. */
798 if (!c->reqtype) {
799 if (c->querybuf[0] == '*') {
800 c->reqtype = REDIS_REQ_MULTIBULK;
e2641e09 801 } else {
cd8788f2 802 c->reqtype = REDIS_REQ_INLINE;
e2641e09 803 }
e2641e09 804 }
cd8788f2
PN
805
806 if (c->reqtype == REDIS_REQ_INLINE) {
807 if (processInlineBuffer(c) != REDIS_OK) break;
808 } else if (c->reqtype == REDIS_REQ_MULTIBULK) {
809 if (processMultibulkBuffer(c) != REDIS_OK) break;
810 } else {
811 redisPanic("Unknown request type");
e2641e09 812 }
cd8788f2
PN
813
814 /* Multibulk processing could see a <= 0 length. */
9da6caac
PN
815 if (c->argc == 0) {
816 resetClient(c);
817 } else {
818 /* Only reset the client when the command was executed. */
819 if (processCommand(c) == REDIS_OK)
820 resetClient(c);
821 }
e2641e09 822 }
823}
824
825void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) {
826 redisClient *c = (redisClient*) privdata;
827 char buf[REDIS_IOBUF_LEN];
828 int nread;
829 REDIS_NOTUSED(el);
830 REDIS_NOTUSED(mask);
831
832 nread = read(fd, buf, REDIS_IOBUF_LEN);
833 if (nread == -1) {
834 if (errno == EAGAIN) {
835 nread = 0;
836 } else {
837 redisLog(REDIS_VERBOSE, "Reading from client: %s",strerror(errno));
838 freeClient(c);
839 return;
840 }
841 } else if (nread == 0) {
842 redisLog(REDIS_VERBOSE, "Client closed connection");
843 freeClient(c);
844 return;
845 }
846 if (nread) {
cd8788f2 847 c->querybuf = sdscatlen(c->querybuf,buf,nread);
e2641e09 848 c->lastinteraction = time(NULL);
849 } else {
850 return;
851 }
852 processInputBuffer(c);
853}
7a1fd61e 854
855void getClientsMaxBuffers(unsigned long *longest_output_list,
856 unsigned long *biggest_input_buffer) {
857 redisClient *c;
858 listNode *ln;
859 listIter li;
860 unsigned long lol = 0, bib = 0;
861
862 listRewind(server.clients,&li);
863 while ((ln = listNext(&li)) != NULL) {
864 c = listNodeValue(ln);
865
866 if (listLength(c->reply) > lol) lol = listLength(c->reply);
867 if (sdslen(c->querybuf) > bib) bib = sdslen(c->querybuf);
868 }
869 *longest_output_list = lol;
870 *biggest_input_buffer = bib;
871}
872