The client query buffer is an sds.c string that can end with a lot of * free space not used, this function reclaims space if needed. * * The function always returns 0 as it never terminates the client. */
| 1679 | * |
| 1680 | * The function always returns 0 as it never terminates the client. */ |
| 1681 | int clientsCronResizeQueryBuffer(client *c) { |
| 1682 | size_t querybuf_size = sdsAllocSize(c->querybuf); |
| 1683 | time_t idletime = server.unixtime - c->lastinteraction; |
| 1684 | |
| 1685 | /* There are two conditions to resize the query buffer: |
| 1686 | * 1) Query buffer is > BIG_ARG and too big for latest peak. |
| 1687 | * 2) Query buffer is > BIG_ARG and client is idle. */ |
| 1688 | if (querybuf_size > PROTO_MBULK_BIG_ARG && |
| 1689 | ((querybuf_size/(c->querybuf_peak+1)) > 2 || |
| 1690 | idletime > 2)) |
| 1691 | { |
| 1692 | /* Only resize the query buffer if it is actually wasting |
| 1693 | * at least a few kbytes. */ |
| 1694 | if (sdsavail(c->querybuf) > 1024*4) { |
| 1695 | c->querybuf = sdsRemoveFreeSpace(c->querybuf); |
| 1696 | } |
| 1697 | } |
| 1698 | /* Reset the peak again to capture the peak memory usage in the next |
| 1699 | * cycle. */ |
| 1700 | c->querybuf_peak = 0; |
| 1701 | |
| 1702 | /* Clients representing masters also use a "pending query buffer" that |
| 1703 | * is the yet not applied part of the stream we are reading. Such buffer |
| 1704 | * also needs resizing from time to time, otherwise after a very large |
| 1705 | * transfer (a huge value or a big MIGRATE operation) it will keep using |
| 1706 | * a lot of memory. */ |
| 1707 | if (c->flags & CLIENT_MASTER) { |
| 1708 | /* There are two conditions to resize the pending query buffer: |
| 1709 | * 1) Pending Query buffer is > LIMIT_PENDING_QUERYBUF. |
| 1710 | * 2) Used length is smaller than pending_querybuf_size/2 */ |
| 1711 | size_t pending_querybuf_size = sdsAllocSize(c->pending_querybuf); |
| 1712 | if(pending_querybuf_size > LIMIT_PENDING_QUERYBUF && |
| 1713 | sdslen(c->pending_querybuf) < (pending_querybuf_size/2)) |
| 1714 | { |
| 1715 | c->pending_querybuf = sdsRemoveFreeSpace(c->pending_querybuf); |
| 1716 | } |
| 1717 | } |
| 1718 | return 0; |
| 1719 | } |
| 1720 | |
| 1721 | /* This function is used in order to track clients using the biggest amount |
| 1722 | * of memory in the latest few seconds. This way we can provide such information |
no test coverage detected