1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following disclaimer
18 * in the documentation and/or other materials provided with the
21 * * The names of its contributors may not be used to endorse or
22 * promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <libmemcached/common.h>
43 memcached_return_t
memcached_purge(memcached_server_write_instance_st ptr
)
45 memcached_return_t ret
= MEMCACHED_SUCCESS
;
46 memcached_st
*root
= (memcached_st
*)ptr
->root
;
48 if (memcached_is_purging(ptr
->root
) || /* already purging */
49 (memcached_server_response_count(ptr
) < ptr
->root
->io_msg_watermark
&&
50 ptr
->io_bytes_sent
< ptr
->root
->io_bytes_watermark
) ||
51 (ptr
->io_bytes_sent
>= ptr
->root
->io_bytes_watermark
&&
52 memcached_server_response_count(ptr
) < 2))
54 return MEMCACHED_SUCCESS
;
57 /* memcached_io_write and memcached_response may call memcached_purge
58 so we need to be able stop any recursion.. */
59 memcached_set_purging(root
, true);
61 WATCHPOINT_ASSERT(ptr
->fd
!= INVALID_SOCKET
);
62 /* Force a flush of the buffer to ensure that we don't have the n-1 pending
63 requests buffered up.. */
64 if (memcached_io_write(ptr
, NULL
, 0, true) == -1)
66 memcached_set_purging(root
, true);
68 return memcached_set_error(*ptr
, MEMCACHED_WRITE_FAILURE
, MEMCACHED_AT
);
70 WATCHPOINT_ASSERT(ptr
->fd
!= INVALID_SOCKET
);
72 uint32_t no_msg
= memcached_server_response_count(ptr
) - 1;
75 memcached_result_st result
;
76 memcached_result_st
*result_ptr
;
77 char buffer
[SMALL_STRING_LEN
];
80 * We need to increase the timeout, because we might be waiting for
81 * data to be sent from the server (the commands was in the output buffer
84 const int32_t timeo
= ptr
->root
->poll_timeout
;
85 root
->poll_timeout
= 2000;
87 result_ptr
= memcached_result_create(root
, &result
);
88 WATCHPOINT_ASSERT(result_ptr
);
90 for (uint32_t x
= 0; x
< no_msg
; x
++)
92 memcached_result_reset(result_ptr
);
93 memcached_return_t rc
= memcached_read_one_response(ptr
, buffer
,
97 * Purge doesn't care for what kind of command results that is received.
98 * The only kind of errors I care about if is I'm out of sync with the
99 * protocol or have problems reading data from the network..
101 if (rc
== MEMCACHED_PROTOCOL_ERROR
or rc
== MEMCACHED_UNKNOWN_READ_FAILURE
or rc
== MEMCACHED_READ_FAILURE
)
103 WATCHPOINT_ERROR(rc
);
105 memcached_io_reset(ptr
);
106 memcached_set_error(*ptr
, rc
, MEMCACHED_AT
);
109 if (ptr
->root
->callbacks
!= NULL
)
111 memcached_callback_st cb
= *ptr
->root
->callbacks
;
112 if (rc
== MEMCACHED_SUCCESS
)
114 for (uint32_t y
= 0; y
< cb
.number_of_callback
; y
++)
116 rc
= (*cb
.callback
[y
])(ptr
->root
, result_ptr
, cb
.context
);
117 if (rc
!= MEMCACHED_SUCCESS
)
126 memcached_result_free(result_ptr
);
127 root
->poll_timeout
= timeo
;
129 memcached_set_purging(root
, false);