Add support for AUTO_EJECT_HOST
[awesomized/libmemcached] / libmemcached / memcached_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 rc;
19 ssize_t read_length;
20 char buffer[MEMCACHED_MAX_BUFFER];
21
22 if (ptr->root->flags & MEM_BINARY_PROTOCOL)
23 {
24 protocol_binary_request_quit request = {.bytes= {0}};
25 request.message.header.request.magic = PROTOCOL_BINARY_REQ;
26 request.message.header.request.opcode = PROTOCOL_BINARY_CMD_QUIT;
27 request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES;
28 rc= memcached_do(ptr, request.bytes, sizeof(request.bytes), 1);
29 }
30 else
31 rc= memcached_do(ptr, "quit\r\n", 6, 1);
32
33 WATCHPOINT_ASSERT(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_FETCH_NOTFINISHED);
34
35 /* read until socket is closed, or there is an error
36 * closing the socket before all data is read
37 * results in server throwing away all data which is
38 * not read
39 */
40 while ((read_length=
41 memcached_io_read(ptr, buffer, sizeof(buffer)/sizeof(*buffer)))
42 > 0);
43 }
44 memcached_io_close(ptr);
45
46 ptr->fd= -1;
47 ptr->write_buffer_offset= (ptr->type == MEMCACHED_CONNECTION_UDP) ? UDP_DATAGRAM_HEADER_LENGTH : 0 ;
48 ptr->read_buffer_length= 0;
49 ptr->read_ptr= ptr->read_buffer;
50 memcached_server_response_reset(ptr);
51 }
52
53 ptr->server_failure_counter++;
54 }
55
56 void memcached_quit(memcached_st *ptr)
57 {
58 unsigned int x;
59
60 if (ptr->hosts == NULL ||
61 ptr->number_of_hosts == 0)
62 return;
63
64 if (ptr->hosts && ptr->number_of_hosts)
65 {
66 for (x= 0; x < ptr->number_of_hosts; x++)
67 memcached_quit_server(&ptr->hosts[x], 0);
68 }
69 }