d9d43057627a58f9703771b79f885a16c66d3f89
[m6w6/libmemcached] / libmemcached / memcached_purge.c
1 #include <assert.h>
2
3 #include "common.h"
4 #include "memcached_io.h"
5
6 void memcached_purge(memcached_server_st *ptr)
7 {
8 char buffer[2048];
9 size_t buffer_length = sizeof(buffer);
10 memcached_result_st result;
11
12 if (ptr->root->purging || /* already purging */
13 (memcached_server_response_count(ptr) < ptr->root->io_msg_watermark &&
14 ptr->io_bytes_sent < ptr->root->io_bytes_watermark) ||
15 (ptr->io_bytes_sent > ptr->root->io_bytes_watermark &&
16 memcached_server_response_count(ptr) < 10)) {
17 return;
18 }
19
20 /* memcached_io_write and memcached_response may call memcached_purge
21 so we need to be able stop any recursion.. */
22 ptr->root->purging = 1;
23
24 /* Force a flush of the buffer to ensure that we don't have the n-1 pending
25 requests buffered up.. */
26 memcached_io_write(ptr, NULL, 0, 1);
27
28 /* we have already incremented the response counter, and memcached_response
29 will read out all messages.. To avoid memcached_response to wait forever
30 for a response to a command I have in my buffer, let's decrement the
31 response counter :) */
32 memcached_server_response_decrement(ptr);
33
34 /* memcached_response may call memcached_io_read, but let's use a short
35 timeout if there is no data yet */
36 int32_t timeout = ptr->root->poll_timeout;
37 ptr->root->poll_timeout = 1;
38 memcached_response(ptr, buffer, sizeof(buffer), &result);
39 ptr->root->poll_timeout = timeout;
40 memcached_server_response_increment(ptr);
41 ptr->root->purging = 0;
42 }