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