Merge in updates (including removal of some depcrated bits from the examples).
[awesomized/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, bool io_death)
13 {
14 if (ptr->fd != INVALID_SOCKET)
15 {
16 if (io_death == false && ptr->type != MEMCACHED_CONNECTION_UDP && ptr->options.is_shutting_down == false)
17 {
18 memcached_return_t rc;
19 char buffer[MEMCACHED_MAX_BUFFER];
20
21 ptr->options.is_shutting_down= true;
22
23 if (ptr->root->flags.binary_protocol)
24 {
25 protocol_binary_request_quit request = {.bytes= {0}};
26 request.message.header.request.magic = PROTOCOL_BINARY_REQ;
27 request.message.header.request.opcode = PROTOCOL_BINARY_CMD_QUIT;
28 request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES;
29 rc= memcached_do(ptr, request.bytes, sizeof(request.bytes), true);
30 }
31 else
32 {
33 rc= memcached_do(ptr, "quit\r\n", sizeof("quit\r\n") -1, true);
34 }
35
36 WATCHPOINT_ASSERT(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_FETCH_NOTFINISHED);
37 (void)rc; // Shut up ICC
38
39 /* read until socket is closed, or there is an error
40 * closing the socket before all data is read
41 * results in server throwing away all data which is
42 * not read
43 *
44 * In .40 we began to only do this if we had been doing buffered
45 * requests of had replication enabled.
46 */
47 if (ptr->root->flags.buffer_requests || ptr->root->number_of_replicas)
48 {
49 ssize_t nread;
50 while (memcached_io_read(ptr, buffer, sizeof(buffer)/sizeof(*buffer),
51 &nread) == MEMCACHED_SUCCESS);
52 }
53
54
55 /*
56 * memcached_io_read may call memcached_quit_server with io_death if
57 * it encounters problems, but we don't care about those occurences.
58 * The intention of that loop is to drain the data sent from the
59 * server to ensure that the server processed all of the data we
60 * sent to the server.
61 */
62 ptr->server_failure_counter= 0;
63 }
64 memcached_io_close(ptr);
65 }
66
67 ptr->fd= INVALID_SOCKET;
68 ptr->io_bytes_sent= 0;
69 ptr->write_buffer_offset= (size_t) ((ptr->type == MEMCACHED_CONNECTION_UDP) ? UDP_DATAGRAM_HEADER_LENGTH : 0);
70 ptr->read_buffer_length= 0;
71 ptr->read_ptr= ptr->read_buffer;
72 ptr->options.is_shutting_down= false;
73 memcached_server_response_reset(ptr);
74
75 // We reset the version so that if we end up talking to a different server
76 // we don't have stale server version information.
77 ptr->major_version= ptr->minor_version= ptr->micro_version= UINT8_MAX;
78
79 if (io_death)
80 {
81 ptr->server_failure_counter++;
82 set_last_disconnected_host(ptr);
83 }
84 }
85
86 void send_quit(memcached_st *ptr)
87 {
88 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
89 {
90 memcached_server_write_instance_st instance=
91 memcached_server_instance_fetch(ptr, x);
92
93 memcached_quit_server(instance, false);
94 }
95 }
96
97 void memcached_quit(memcached_st *ptr)
98 {
99 if (initialize_query(ptr) != MEMCACHED_SUCCESS)
100 {
101 return;
102 }
103
104 send_quit(ptr);
105 }