summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean Reifschneider <jafo00@gmail.com>2023-04-16 10:20:57 -0600
committerGitHub <noreply@github.com>2023-04-16 10:20:57 -0600
commit6948c119219733e6cd1ba20e9c6d0909fc177435 (patch)
treebf61b836ae03c49bf70694b4183b78fbff41325f
parentab668ed17887c956af3e2af89555e31d190ab63d (diff)
parent582cfe01c3b634e55a748c9b5ca80a48aac097f4 (diff)
downloadpython-memcached-6948c119219733e6cd1ba20e9c6d0909fc177435.tar.gz
Merge pull request #15 from userrl/master
Added quit() method. This method sends the 'quit' command to the servers and then closes the connections, reducing the number of TIME_WAIT sockets hanging around the OS.
-rw-r--r--memcache.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/memcache.py b/memcache.py
index e3ce42a..1eefcf7 100644
--- a/memcache.py
+++ b/memcache.py
@@ -364,6 +364,11 @@ class Client(threading.local):
serverData[slab[0]][slab[1]] = item[2]
return data
+ def quit_all(self):
+ '''Send a "quit" command to all servers and wait for the connection to close.'''
+ for s in self.servers:
+ s.quit()
+
def get_slabs(self):
data = []
for s in self.servers:
@@ -1475,6 +1480,23 @@ class _Host(object):
self.buffer = buf[rlen:]
return buf[:rlen]
+ def quit(self):
+ '''Send a "quit" command to remote server and wait for connection to close.'''
+ if self.socket:
+ # Using self.send_cmd, so no need for '\r\n'.
+ self.send_cmd('quit')
+
+ # We can't close the local socket until the remote end processes the quit
+ # command and sends us a FIN packet. When that happens, socket.recv()
+ # will stop blocking and return an empty string. If we try to close the
+ # socket before then, the OS will think we're initiating the connection
+ # close and will put the socket into TIME_WAIT.
+ self.socket.recv(1)
+
+ # At this point, socket should be in CLOSE_WAIT. Closing the socket should
+ # release the port back to the OS.
+ self.close_socket()
+
def flush(self):
self.send_cmd('flush_all')
self.expect(b'OK')