1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2010 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
38 #include <libmemcached/common.h>
41 This closes all connections (forces flush of input as well).
43 Maybe add a host specific, or key specific version?
45 The reason we send "quit" is that in case we have buffered IO, this
46 will force data to be completed.
49 void memcached_quit_server(memcached_server_st
*ptr
, bool io_death
)
51 if (ptr
->fd
!= INVALID_SOCKET
)
53 if (io_death
== false && ptr
->type
!= MEMCACHED_CONNECTION_UDP
&& ptr
->options
.is_shutting_down
== false)
55 memcached_return_t rc
;
56 char buffer
[MEMCACHED_MAX_BUFFER
];
58 ptr
->options
.is_shutting_down
= true;
60 if (ptr
->root
->flags
.binary_protocol
)
62 protocol_binary_request_quit request
= {}; // = {.bytes= {0}};
63 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
64 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_QUIT
;
65 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
66 rc
= memcached_do(ptr
, request
.bytes
, sizeof(request
.bytes
), true);
70 rc
= memcached_do(ptr
, memcached_literal_param("quit\r\n"), true);
73 WATCHPOINT_ASSERT(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_FETCH_NOTFINISHED
);
74 (void)rc
; // Shut up ICC
76 /* read until socket is closed, or there is an error
77 * closing the socket before all data is read
78 * results in server throwing away all data which is
81 * In .40 we began to only do this if we had been doing buffered
82 * requests of had replication enabled.
84 if (ptr
->root
->flags
.buffer_requests
|| ptr
->root
->number_of_replicas
)
87 while (memcached_io_read(ptr
, buffer
, sizeof(buffer
)/sizeof(*buffer
),
88 &nread
) == MEMCACHED_SUCCESS
) {} ;
93 * memcached_io_read may call memcached_quit_server with io_death if
94 * it encounters problems, but we don't care about those occurences.
95 * The intention of that loop is to drain the data sent from the
96 * server to ensure that the server processed all of the data we
99 ptr
->server_failure_counter
= 0;
101 memcached_io_close(ptr
);
104 ptr
->fd
= INVALID_SOCKET
;
105 ptr
->io_bytes_sent
= 0;
106 ptr
->write_buffer_offset
= (size_t) ((ptr
->type
== MEMCACHED_CONNECTION_UDP
) ? UDP_DATAGRAM_HEADER_LENGTH
: 0);
107 ptr
->read_buffer_length
= 0;
108 ptr
->read_ptr
= ptr
->read_buffer
;
109 ptr
->options
.is_shutting_down
= false;
110 memcached_server_response_reset(ptr
);
112 // We reset the version so that if we end up talking to a different server
113 // we don't have stale server version information.
114 ptr
->major_version
= ptr
->minor_version
= ptr
->micro_version
= UINT8_MAX
;
118 ptr
->server_failure_counter
++;
119 set_last_disconnected_host(ptr
);
123 void send_quit(memcached_st
*ptr
)
125 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
127 memcached_server_write_instance_st instance
=
128 memcached_server_instance_fetch(ptr
, x
);
130 memcached_quit_server(instance
, false);
134 void memcached_quit(memcached_st
*ptr
)
136 if (initialize_query(ptr
) != MEMCACHED_SUCCESS
)