rust-web3 icon indicating copy to clipboard operation
rust-web3 copied to clipboard

Handling disconnection

Open pawanjay176 opened this issue 6 years ago • 5 comments
trafficstars

Currently, if I disconnect my websocket service while a request is in progress, the request just hangs. Is there any way I can detect disconnection from an endpoint and try to reconnect?

pawanjay176 avatar Aug 28 '19 04:08 pawanjay176

Unfortunately not yet. The ws transport should be handling reconnects on it's own, but it's not implemented. PRs welcome! :)

tomusdrw avatar Aug 30 '19 10:08 tomusdrw

Could you give me some basic pointers as to how to go about doing that?

My idea is to send a periodic OwnedMessage::Ping message in the writer future within with_event_loop function and if we don't get a response within some timeout, then call the with_event_loop function recursively to reconnect. Does this approach make sense?

pawanjay176 avatar Aug 31 '19 07:08 pawanjay176

Yeah, that makes perfect sense. Make sure to make Ping interval configurable. I was actually sure that websocket crate is handling low-level WS pings on it's own, but apparently it's not.

tomusdrw avatar Sep 02 '19 08:09 tomusdrw

Could you give me some basic pointers as to how to go about doing that?

My idea is to send a periodic OwnedMessage::Ping message in the writer future within with_event_loop function and if we don't get a response within some timeout, then call the with_event_loop function recursively to reconnect. Does this approach make sense?

Does this idea still make sense? @tomusdrw. Let me see if I can help. Without timeout and reconnection, it makes this wonderful crate useless in long-run jobs.

ngugcx avatar Jul 23 '21 00:07 ngugcx

Does this idea still make sense?

Yes, I believe so! The ideal implementation is not going to be trivial though, but I think having at least some basic disconnection/timeout detection mechanism will be better than nothing.

tomusdrw avatar Jul 23 '21 07:07 tomusdrw