Skip to content

Commit

Permalink
Merge pull request #15 from userrl/master
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
linsomniac authored Apr 16, 2023
2 parents ab668ed + 582cfe0 commit 6948c11
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions memcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ def get_slab_stats(self):
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:
Expand Down Expand Up @@ -1475,6 +1480,23 @@ def recv(self, rlen):
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')
Expand Down

0 comments on commit 6948c11

Please sign in to comment.