Skip to content
This repository has been archived by the owner on Aug 1, 2021. It is now read-only.

Commit

Permalink
Handle zombie connections (#79)
Browse files Browse the repository at this point in the history
* Handle zombie connections

* Close w/ non-1000 code, make variable private
  • Loading branch information
PixeLInc authored and b1naryth1ef committed Jan 22, 2018
1 parent a137dc2 commit 02cc56e
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions disco/gateway/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self, client, max_reconnects=5, encoder='json', zlib_stream_enabled
# Create emitter and bind to gateway payloads
self.packets.on((RECV, OPCode.DISPATCH), self.handle_dispatch)
self.packets.on((RECV, OPCode.HEARTBEAT), self.handle_heartbeat)
self.packets.on((RECV, OPCode.HEARTBEAT_ACK), self.handle_heartbeat_acknowledge)
self.packets.on((RECV, OPCode.RECONNECT), self.handle_reconnect)
self.packets.on((RECV, OPCode.INVALID_SESSION), self.handle_invalid_session)
self.packets.on((RECV, OPCode.HELLO), self.handle_hello)
Expand Down Expand Up @@ -67,6 +68,7 @@ def __init__(self, client, max_reconnects=5, encoder='json', zlib_stream_enabled

# Heartbeat
self._heartbeat_task = None
self._heartbeat_acknowledged = True

def send(self, op, data):
self.limiter.check()
Expand All @@ -82,7 +84,14 @@ def _send(self, op, data):

def heartbeat_task(self, interval):
while True:
if not self._heartbeat_acknowledged:
self.log.warning('Received HEARTBEAT without HEARTBEAT_ACK, forcing a fresh reconnect')
self._heartbeat_acknowledged = True
self.ws.close(status=4000)
return

self._send(OPCode.HEARTBEAT, self.seq)
self._heartbeat_acknowledged = False
gevent.sleep(interval / 1000)

def handle_dispatch(self, packet):
Expand All @@ -95,6 +104,10 @@ def handle_dispatch(self, packet):
def handle_heartbeat(self, _):
self._send(OPCode.HEARTBEAT, self.seq)

def handle_heartbeat_acknowledge(self, _):
self.log.debug('Received HEARTBEAT_ACK')
self._heartbeat_acknowledged = True

def handle_reconnect(self, _):
self.log.warning('Received RECONNECT request, forcing a fresh reconnect')
self.session_id = None
Expand Down

0 comments on commit 02cc56e

Please sign in to comment.