crossbario / autobahn-python

WebSocket and WAMP in Python for Twisted and asyncio
https://crossbar.io/autobahn
MIT License
2.48k stars 766 forks source link

Describe heartbeating for quick connection loss detection #1095

Open dziad-yge opened 5 years ago

dziad-yge commented 5 years ago

Hi,

How can I make autobahn.twisted.component.Component notice lost of connection with router quickly? Do I have any influence on time it takes?

I've tried couple of ideas but they seem not to work.

My case: I have crossbar.io router with ping-pong specified. I have clients communicating with router using autobahn in Python 2.7 (on Ubuntu and on Mint). I've created an autobahn.twisted.component.Component object and also keep access to Session object I've received at 'on component join'. If I lose connection to router, error 'wamp.close.transport_lost' is raised, then session leaves and component tries to reconnect. This is the scenario I like.

However when I lose connection (by unplugging the internet cable), it can take a lot of time before autobahn fires 'wamp.close.transport_lost. For clients running on Ubuntu it's around 10 sec. But for clients on Mint it's around 20 minutes. I wish I could have a ping-pong with router, but I haven't found such an option for Component transport.

  1. I've tried to publish anything, so that component/session would notice that it has lost transport. But on Mint it takes around 20 minutes before it realizes.
  2. In documentation I found that I can define 'endpoint' with option 'timeout' on my own. However, after adding 'endpoint' with 'timeout' to my transport definition, I got the 'KeyError'.

Then I got another idea. I've decided to use simple python socket to check if I can connect to the port the crossbar.io listens on. If no, I know I have no connection with router for whatever reason. If yes, I need to verify if my session is still attached to the router. If it is not attached, I would use session.disconnect() and then session.leave(). After that 'wamp.close.transport_lost' is raised and Component starts reconnecting by himself.

How can I verify if my session is attached to the router?

  1. I've tried the method of session object "is_attached". However it returns 'True' even when my client is disconnected from the internet. After plugging the internet cable back, I keep receiving the same answer - True. But in fact the session is disconnected.

Could you give me some advice, how to achieve my goal?

Thanks for help Dziad-yge

oberstet commented 5 years ago

For quick detection of connection loss, both peers (the WAMP client and the WAMP router) need to do heartbeating (WebSocket ping/pong). Anything using TCP over WAN needs to do sth like that for quick detection.

For the ApplicationRunner when using Twisted we do have client-initiated heartbeating: https://github.com/crossbario/autobahn-python/blob/master/autobahn/twisted/wamp.py#L289

For Component: @meejah not sure ...

dionytadema commented 5 years ago

i have this same issue, the router i'm using already uses WebSocket ping/pong and correctly detects the client no longer being connected

The client however can take up to a few minutes to detect that it's wireless network is gone and it disconnected, this makes a short loss of network cause long periods of not being connected, a big problem for our system where all devices are wireless

having a option to set the timeout on a components transport config in a similar way to how the application runner does it seems like a good solution to me.

dionytadema commented 4 years ago

Still an issue, any progress or workaround?

it's been about a year now