epoplavskis/photon-pump

Photonpump should send heartbeat requests to the server

bobthemighty opened this issue · 3 comments

If we close the connection from underneath photonpump, eg. by terminating a VPN tunnel, then the client never notices any difference.

We should periodically (suggest every 60 secs) send a heartbeat request to the server. If we fail some consecutive number of requests, we should issue a reconnection request to the connection handler.

If we implement the Connector, per #9 then we can hook this logic up in there. We'll set up some kind of dead man's switch, and write events for HeartbeatRequested, HeartbeatReceived, and HeartbeatTimeout into the queue.

something like this?

error = 0
async def send_heartbeat(time=60, error=error):
    while True:
        if error > 5:
            await queue.put("HeartbeatError")
            break
        try: 
            await i_am_alive(url)
            await queue.put("HeartbeatReceived")
        except:
            error += 1
            await queue.put("HeartbeatFailed")
        await asyncio.sleep(time)

@barrachri This is implemented in 0.5

Your pseudo-code is pretty close!

https://github.com/madedotcom/photon-pump/blob/f37836d7c83322ecb11fdfb0d659c65964c264a9/photonpump/connection.py#L867

We need to reconsider, though, as part of #49