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