Fix test case for weighted servers.
[awesomized/libmemcached] / libmemcached / memcached_purge.c
1 #include <assert.h>
2
3 #include "common.h"
4 #include "memcached_io.h"
5
6 memcached_return memcached_purge(memcached_server_st *ptr)
7 {
8 memcached_return rc;
9 int32_t timeout;
10 char buffer[2048];
11 memcached_result_st result;
12 memcached_result_st *result_ptr;
13
14 if (ptr->root->purging || /* already purging */
15 (memcached_server_response_count(ptr) < ptr->root->io_msg_watermark &&
16 ptr->io_bytes_sent < ptr->root->io_bytes_watermark) ||
17 (ptr->io_bytes_sent > ptr->root->io_bytes_watermark &&
18 memcached_server_response_count(ptr) < 10))
19 {
20 return MEMCACHED_SUCCESS;
21 }
22
23 /* memcached_io_write and memcached_response may call memcached_purge
24 so we need to be able stop any recursion.. */
25 ptr->root->purging= 1;
26
27 WATCHPOINT_ASSERT(ptr->fd != -1);
28 /* Force a flush of the buffer to ensure that we don't have the n-1 pending
29 requests buffered up.. */
30 if (memcached_io_write(ptr, NULL, 0, 1) == -1)
31 return MEMCACHED_FAILURE;
32 WATCHPOINT_ASSERT(ptr->fd != -1);
33
34 /* we have already incremented the response counter, and memcached_response
35 will read out all messages.. To avoid memcached_response to wait forever
36 for a response to a command I have in my buffer, let's decrement the
37 response counter :) */
38 memcached_server_response_decrement(ptr);
39
40 /* memcached_response may call memcached_io_read, but let's use a short
41 timeout if there is no data yet */
42 timeout= ptr->root->poll_timeout;
43 ptr->root->poll_timeout= 1;
44 result_ptr= memcached_result_create(ptr->root, &result);
45
46 if (result_ptr == NULL)
47 return MEMCACHED_FAILURE;
48
49 WATCHPOINT_ASSERT(ptr->fd != -1);
50 rc= memcached_response(ptr, buffer, sizeof(buffer), &result);
51 WATCHPOINT_ERROR(rc);
52 WATCHPOINT_ASSERT(ptr->fd != -1);
53 ptr->root->poll_timeout= timeout;
54 memcached_server_response_increment(ptr);
55 ptr->root->purging = 0;
56
57 memcached_result_free(&result);
58
59 return rc;
60 }