Updating tree for new function work in libhashkit.
[m6w6/libmemcached] / libmemcached / quit.c
1 #include "common.h"
2
3 /*
4 This closes all connections (forces flush of input as well).
5
6 Maybe add a host specific, or key specific version?
7
8 The reason we send "quit" is that in case we have buffered IO, this
9 will force data to be completed.
10 */
11
12 void memcached_quit_server(memcached_server_st *ptr, uint8_t io_death)
13 {
14 if (ptr->fd != -1)
15 {
16 if (io_death == 0 && ptr->type != MEMCACHED_CONNECTION_UDP)
17 {
18 memcached_return_t rc;
19 char buffer[MEMCACHED_MAX_BUFFER];
20
21 if (ptr->root->flags.binary_protocol)
22 {
23 protocol_binary_request_quit request = {.bytes= {0}};
24 request.message.header.request.magic = PROTOCOL_BINARY_REQ;
25 request.message.header.request.opcode = PROTOCOL_BINARY_CMD_QUIT;
26 request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES;
27 rc= memcached_do(ptr, request.bytes, sizeof(request.bytes), 1);
28 }
29 else
30 {
31 rc= memcached_do(ptr, "quit\r\n", 6, 1);
32 }
33
34 WATCHPOINT_ASSERT(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_FETCH_NOTFINISHED);
35 (void)rc; // Shut up ICC
36
37 /* read until socket is closed, or there is an error
38 * closing the socket before all data is read
39 * results in server throwing away all data which is
40 * not read
41 */
42 ssize_t nread;
43 while (memcached_io_read(ptr, buffer, sizeof(buffer)/sizeof(*buffer),
44 &nread) == MEMCACHED_SUCCESS);
45
46 /*
47 * memcached_io_read may call memcached_quit_server with io_death if
48 * it encounters problems, but we don't care about those occurences.
49 * The intention of that loop is to drain the data sent from the
50 * server to ensure that the server processed all of the data we
51 * sent to the server.
52 */
53 ptr->server_failure_counter= 0;
54 }
55 memcached_io_close(ptr);
56 }
57
58 ptr->fd= -1;
59 ptr->write_buffer_offset= (size_t) ((ptr->type == MEMCACHED_CONNECTION_UDP) ? UDP_DATAGRAM_HEADER_LENGTH : 0);
60 ptr->read_buffer_length= 0;
61 ptr->read_ptr= ptr->read_buffer;
62 memcached_server_response_reset(ptr);
63
64 if(io_death)
65 {
66 ptr->server_failure_counter++;
67 }
68 }
69
70 void memcached_quit(memcached_st *ptr)
71 {
72 uint32_t x;
73
74 if (memcached_server_count(ptr) == 0)
75 return;
76
77 if (memcached_server_count(ptr))
78 {
79 for (x= 0; x < memcached_server_count(ptr); x++)
80 {
81 memcached_server_instance_st *instance=
82 memcached_server_instance_fetch(ptr, x);
83
84 memcached_quit_server(instance, 0);
85 }
86 }
87 }