Write data in output buffers to client. Return C_OK if the client * is still valid after the call, C_ERR if it was freed because of some * error. If handler_installed is set, it will attempt to clear the * write event. * * This function is called by threads, but always with handler_installed * set to 0. So when handler_installed is set to 0 the function must be * thread safe. */
| 1503 | * set to 0. So when handler_installed is set to 0 the function must be |
| 1504 | * thread safe. */ |
| 1505 | int writeToClient(client *c, int handler_installed) { |
| 1506 | /* Update total number of writes on server */ |
| 1507 | atomicIncr(server.stat_total_writes_processed, 1); |
| 1508 | |
| 1509 | ssize_t nwritten = 0, totwritten = 0; |
| 1510 | size_t objlen; |
| 1511 | clientReplyBlock *o; |
| 1512 | |
| 1513 | while(clientHasPendingReplies(c)) { |
| 1514 | if (c->bufpos > 0) { |
| 1515 | nwritten = connWrite(c->conn,c->buf+c->sentlen,c->bufpos-c->sentlen); |
| 1516 | if (nwritten <= 0) break; |
| 1517 | c->sentlen += nwritten; |
| 1518 | totwritten += nwritten; |
| 1519 | |
| 1520 | /* If the buffer was sent, set bufpos to zero to continue with |
| 1521 | * the remainder of the reply. */ |
| 1522 | if ((int)c->sentlen == c->bufpos) { |
| 1523 | c->bufpos = 0; |
| 1524 | c->sentlen = 0; |
| 1525 | } |
| 1526 | } else { |
| 1527 | o = listNodeValue(listFirst(c->reply)); |
| 1528 | objlen = o->used; |
| 1529 | |
| 1530 | if (objlen == 0) { |
| 1531 | c->reply_bytes -= o->size; |
| 1532 | listDelNode(c->reply,listFirst(c->reply)); |
| 1533 | continue; |
| 1534 | } |
| 1535 | |
| 1536 | nwritten = connWrite(c->conn, o->buf + c->sentlen, objlen - c->sentlen); |
| 1537 | if (nwritten <= 0) break; |
| 1538 | c->sentlen += nwritten; |
| 1539 | totwritten += nwritten; |
| 1540 | |
| 1541 | /* If we fully sent the object on head go to the next one */ |
| 1542 | if (c->sentlen == objlen) { |
| 1543 | c->reply_bytes -= o->size; |
| 1544 | listDelNode(c->reply,listFirst(c->reply)); |
| 1545 | c->sentlen = 0; |
| 1546 | /* If there are no longer objects in the list, we expect |
| 1547 | * the count of reply bytes to be exactly zero. */ |
| 1548 | if (listLength(c->reply) == 0) |
| 1549 | serverAssert(c->reply_bytes == 0); |
| 1550 | } |
| 1551 | } |
| 1552 | /* Note that we avoid to send more than NET_MAX_WRITES_PER_EVENT |
| 1553 | * bytes, in a single threaded server it's a good idea to serve |
| 1554 | * other clients as well, even if a very large request comes from |
| 1555 | * super fast link that is always able to accept data (in real world |
| 1556 | * scenario think about 'KEYS *' against the loopback interface). |
| 1557 | * |
| 1558 | * However if we are over the maxmemory limit we ignore that and |
| 1559 | * just deliver as much data as it is possible to deliver. |
| 1560 | * |
| 1561 | * Moreover, we also send as much as possible if the client is |
| 1562 | * a slave or a monitor (otherwise, on high-speed traffic, the |
no test coverage detected