Skip to content

Commit 10fc811

Browse files
committed
memcached: call socket.recv multiple times to get all stats
New versions of memcached have more stats, making it more likely that stats data will go across packet boundaries. Previously, this caused the collector to crash on interpreting the resulting data, since some stats lines would be truncated, and some stats would be missing altogether. This also explicitly closes the socket when finished, as well as sets a timeout on reading from the socket so it won't hang around waiting forever to receive data if something goes wrong.
1 parent 175a4db commit 10fc811

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/collectors/memcached/memcached.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,26 @@ def get_raw_stats(self, host, port):
8787
else:
8888
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8989
sock.connect((host, int(port)))
90+
91+
# give up after a reasonable amount of time
92+
sock.settimeout(3)
93+
9094
# request stats
9195
sock.send('stats\n')
92-
# something big enough to get whatever is sent back
93-
data = sock.recv(4096)
96+
97+
# stats can be sent across multiple packets, so make sure we've
98+
# read up until the END marker
99+
while True:
100+
received = sock.recv(4096)
101+
if not received:
102+
break
103+
data += received
104+
if data.endswith('END\r\n'):
105+
break
94106
except socket.error:
95107
self.log.exception('Failed to get stats from %s:%s',
96108
host, port)
109+
sock.close()
97110
return data
98111

99112
def get_stats(self, host, port):

0 commit comments

Comments
 (0)