Merge from Trond
[m6w6/libmemcached] / libmemcached / memcached_purge.c
1 #include "common.h"
2 #include "memcached_io.h"
3 #include "memcached_constants.h"
4
5 memcached_return memcached_purge(memcached_server_st *ptr)
6 {
7 uint32_t x;
8 memcached_return ret= MEMCACHED_SUCCESS;
9
10 if (ptr->root->purging || /* already purging */
11 (memcached_server_response_count(ptr) < ptr->root->io_msg_watermark &&
12 ptr->io_bytes_sent < ptr->root->io_bytes_watermark) ||
13 (ptr->io_bytes_sent >= ptr->root->io_bytes_watermark &&
14 memcached_server_response_count(ptr) < 2))
15 {
16 return MEMCACHED_SUCCESS;
17 }
18
19 /* memcached_io_write and memcached_response may call memcached_purge
20 so we need to be able stop any recursion.. */
21 ptr->root->purging= 1;
22
23 WATCHPOINT_ASSERT(ptr->fd != -1);
24 /* Force a flush of the buffer to ensure that we don't have the n-1 pending
25 requests buffered up.. */
26 if (memcached_io_write(ptr, NULL, 0, 1) == -1)
27 {
28 ptr->root->purging= 0;
29 return MEMCACHED_WRITE_FAILURE;
30 }
31 WATCHPOINT_ASSERT(ptr->fd != -1);
32
33 uint32_t no_msg= memcached_server_response_count(ptr) - 1;
34 if (no_msg > 0)
35 {
36 memcached_result_st result;
37 memcached_result_st *result_ptr;
38 char buffer[SMALL_STRING_LEN];
39
40 /*
41 * We need to increase the timeout, because we might be waiting for
42 * data to be sent from the server (the commands was in the output buffer
43 * and just flushed
44 */
45 int32_t timeo= ptr->root->poll_timeout;
46 ptr->root->poll_timeout= 2000;
47
48 result_ptr= memcached_result_create(ptr->root, &result);
49 WATCHPOINT_ASSERT(result_ptr);
50
51 for (x= 0; x < no_msg; x++)
52 {
53 memcached_result_reset(result_ptr);
54 memcached_return rc= memcached_read_one_response(ptr, buffer,
55 sizeof (buffer),
56 result_ptr);
57 /*
58 * Purge doesn't care for what kind of command results that is received.
59 * The only kind of errors I care about if is I'm out of sync with the
60 * protocol or have problems reading data from the network..
61 */
62 if (rc== MEMCACHED_PROTOCOL_ERROR || rc == MEMCACHED_UNKNOWN_READ_FAILURE)
63 {
64 WATCHPOINT_ERROR(rc);
65 ret = rc;
66 memcached_io_reset(ptr);
67 }
68 }
69
70 memcached_result_free(result_ptr);
71 ptr->root->poll_timeout= timeo;
72 }
73 ptr->root->purging= 0;
74
75 return ret;
76 }