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. */
| 1793 | * |
| 1794 | * The function always returns 0 as it never terminates the client. */ |
| 1795 | int clientsCronResizeQueryBuffer(client *c) { |
| 1796 | AssertCorrectThread(c); |
| 1797 | size_t querybuf_size = sdsAllocSize(c->querybuf); |
| 1798 | time_t idletime = g_pserver->unixtime - c->lastinteraction; |
| 1799 | |
| 1800 | /* There are two conditions to resize the query buffer: |
| 1801 | * 1) Query buffer is > BIG_ARG and too big for latest peak. |
| 1802 | * 2) Query buffer is > BIG_ARG and client is idle. */ |
| 1803 | if (querybuf_size > PROTO_MBULK_BIG_ARG && |
| 1804 | ((querybuf_size/(c->querybuf_peak+1)) > 2 || |
| 1805 | idletime > 2)) |
| 1806 | { |
| 1807 | /* Only resize the query buffer if it is actually wasting |
| 1808 | * at least a few kbytes. */ |
| 1809 | if (sdsavail(c->querybuf) > 1024*4) { |
| 1810 | c->querybuf = sdsRemoveFreeSpace(c->querybuf); |
| 1811 | } |
| 1812 | } |
| 1813 | /* Reset the peak again to capture the peak memory usage in the next |
| 1814 | * cycle. */ |
| 1815 | c->querybuf_peak = 0; |
| 1816 | |
| 1817 | /* Clients representing masters also use a "pending query buffer" that |
| 1818 | * is the yet not applied part of the stream we are reading. Such buffer |
| 1819 | * also needs resizing from time to time, otherwise after a very large |
| 1820 | * transfer (a huge value or a big MIGRATE operation) it will keep using |
| 1821 | * a lot of memory. */ |
| 1822 | if (c->flags & CLIENT_MASTER) { |
| 1823 | /* There are two conditions to resize the pending query buffer: |
| 1824 | * 1) Pending Query buffer is > LIMIT_PENDING_QUERYBUF. |
| 1825 | * 2) Used length is smaller than pending_querybuf_size/2 */ |
| 1826 | size_t pending_querybuf_size = sdsAllocSize(c->pending_querybuf); |
| 1827 | if(pending_querybuf_size > LIMIT_PENDING_QUERYBUF && |
| 1828 | sdslen(c->pending_querybuf) < (pending_querybuf_size/2)) |
| 1829 | { |
| 1830 | c->pending_querybuf = sdsRemoveFreeSpace(c->pending_querybuf); |
| 1831 | } |
| 1832 | } |
| 1833 | return 0; |
| 1834 | } |
| 1835 | |
| 1836 | SymVer parseVersion(const char *version) |
| 1837 | { |
no test coverage detected