libmemcached: fix typo [ci skip]
[awesomized/libmemcached] / libmemcached / quit.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2010 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 #include <libmemcached/common.h>
39
40 namespace {
41 memcached_return_t send_quit_message(memcached_instance_st* instance)
42 {
43 memcached_return_t rc;
44 if (instance->root->flags.binary_protocol)
45 {
46 protocol_binary_request_quit request= {}; // = {.bytes= {0}};
47
48 initialize_binary_request(instance, request.message.header);
49
50 request.message.header.request.opcode = PROTOCOL_BINARY_CMD_QUIT;
51 request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES;
52
53 libmemcached_io_vector_st vector[]=
54 {
55 { request.bytes, sizeof(request.bytes) }
56 };
57
58 rc= memcached_vdo(instance, vector, 1, true);
59 }
60 else
61 {
62 libmemcached_io_vector_st vector[]=
63 {
64 { memcached_literal_param("quit\r\n") }
65 };
66
67 rc= memcached_vdo(instance, vector, 1, true);
68 }
69
70 return rc;
71 }
72
73 void drain_instance(memcached_instance_st* instance)
74 {
75 /* read until socket is closed, or there is an error
76 * closing the socket before all data is read
77 * results in server throwing away all data which is
78 * not read
79 *
80 * In .40 we began to only do this if we had been doing buffered
81 * requests of had replication enabled.
82 */
83 if (instance->root->flags.buffer_requests or instance->root->number_of_replicas)
84 {
85 memcached_io_slurp(instance);
86 }
87
88 /*
89 * memcached_io_read may call memcached_quit_server with io_death if
90 * it encounters problems, but we don't care about those occurences.
91 * The intention of that loop is to drain the data sent from the
92 * server to ensure that the server processed all of the data we
93 * sent to the server.
94 */
95 instance->server_failure_counter= 0;
96 instance->server_timeout_counter= 0;
97 }
98 }
99
100 /*
101 This closes all connections (forces flush of input as well).
102
103 Maybe add a host specific, or key specific version?
104
105 The reason we send "quit" is that in case we have buffered IO, this
106 will force data to be completed.
107 */
108
109 void memcached_quit_server(memcached_instance_st* instance, bool io_death)
110 {
111 if (instance->valid())
112 {
113 if (io_death == false and memcached_is_udp(instance->root) == false and instance->is_shutting_down() == false)
114 {
115 send_quit_message(instance);
116
117 instance->start_close_socket();
118 drain_instance(instance);
119 }
120 }
121
122 instance->close_socket();
123
124 if (io_death and memcached_is_udp(instance->root))
125 {
126 /*
127 If using UDP, we should stop using the server briefly on every IO
128 failure. If using TCP, it may be that the connection went down a
129 short while ago (e.g. the server failed) and we've only just
130 noticed, so we should only set the retry timeout on a connect
131 failure (which doesn't call this method).
132 */
133 memcached_mark_server_for_timeout(instance);
134 }
135 }
136
137 void send_quit(Memcached *memc)
138 {
139 for (uint32_t x= 0; x < memcached_server_count(memc); x++)
140 {
141 memcached_instance_st* instance= memcached_instance_fetch(memc, x);
142
143 memcached_quit_server(instance, false);
144 }
145 }
146
147 void memcached_quit(memcached_st *shell)
148 {
149 Memcached* memc= memcached2Memcached(shell);
150 memcached_return_t rc;
151 if (memcached_failed(rc= initialize_query(memc, true)))
152 {
153 return;
154 }
155
156 send_quit(memc);
157 }