Skip to content

Commit

Permalink
Actually disconnect on timeout
Browse files Browse the repository at this point in the history
lymph.core.connection.Connection pings the endpoint regularly
to determine whether it is still live. `__init__` takes a
`unresponsive_disconnect` parameter, which was however ignored
so far. Similarly, the `idle_disconnect` parameter did not have
any effect.

This commit uses thees two parameters to do what they advertise,
i.e. close the connection once the respective timeouts have been
reached.

To this end, the greenlet management logic was also changed, such
that the monitoring greenlets now commit suicide when the connection
`status == CLOSED` instead of being explicitely killed from `close()`.
Without this change, the monitoring greenlet would inadvertently kill
itself, leading to (very nonobvious) dead code and leaving a half-dead
connection object still registered with the server.
  • Loading branch information
Drahflow committed Apr 21, 2015
1 parent 24b8e1b commit 3584433
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lymph/core/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def set_status(self, status):
self.status = status

def heartbeat_loop(self):
while True:
while self.status != CLOSED:
start = time.monotonic()
channel = self.server.ping(self.endpoint)
try:
Expand All @@ -76,16 +76,22 @@ def heartbeat_loop(self):
gevent.sleep(self.heartbeat_interval)

def live_check_loop(self):
while True:
while self.status != CLOSED:
self.update_status()
self.log_stats()
gevent.sleep(self.timeout)

def update_status(self):
if self.last_seen:
now = time.monotonic()
if now - self.last_seen >= self.timeout:

if now - self.last_seen >= self.unresponsive_disconnect:
logger.debug("disconnecting from unresponsive endpoint %s" % (self.endpoint))
self.close()
elif now - self.last_seen >= self.timeout:
self.set_status(UNRESPONSIVE)
elif now - self.last_message >= self.idle_disconnect:
self.close()
elif now - self.last_message >= self.idle_timeout:
self.set_status(IDLE)
self.idle_since = now
Expand All @@ -108,8 +114,6 @@ def close(self):
if self.status == CLOSED:
return
self.status = CLOSED
self.heartbeat_loop_greenlet.kill()
self.live_check_loop_greenlet.kill()
self.server.disconnect(self.endpoint)

def on_recv(self, msg):
Expand Down

0 comments on commit 3584433

Please sign in to comment.