Cluster: clusterReadHandler() reworked to be more correct and simpler to follow.

This commit is contained in:
antirez 2013-09-03 11:43:07 +02:00
parent 1036b4b21b
commit 354a5de270

View File

@ -1169,7 +1169,7 @@ void clusterReadHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
REDIS_NOTUSED(el);
REDIS_NOTUSED(mask);
again:
while(1) { /* Read as long as there is data to read. */
rcvbuflen = sdslen(link->rcvbuf);
if (rcvbuflen < 4) {
/* First, obtain the first four bytes to get the full message
@ -1188,6 +1188,7 @@ again:
}
}
readlen = ntohl(hdr->totlen) - rcvbuflen;
if (readlen > sizeof(buf)) readlen = sizeof(buf);
}
nread = read(fd,buf,readlen);
@ -1206,16 +1207,14 @@ again:
rcvbuflen += nread;
}
/* Total length obtained? read the payload now instead of burning
* cycles waiting for a new event to fire. */
if (rcvbuflen == 4) goto again;
/* Whole packet in memory? We can process it. */
if (rcvbuflen == ntohl(hdr->totlen)) {
/* Total length obtained? Process this packet. */
if (rcvbuflen >= 4 && rcvbuflen == ntohl(hdr->totlen)) {
if (clusterProcessPacket(link)) {
sdsfree(link->rcvbuf);
link->rcvbuf = sdsempty();
rcvbuflen = 0; /* Useless line of code currently... defensive. */
} else {
return; /* Link no longer valid. */
}
}
}
}