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