rust-web3
rust-web3 copied to clipboard
Handling disconnection
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?
Unfortunately not yet. The ws transport should be handling reconnects on it's own, but it's not implemented. PRs welcome! :)
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?
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.
Could you give me some basic pointers as to how to go about doing that?
My idea is to send a periodic
OwnedMessage::Pingmessage in thewriterfuture withinwith_event_loopfunction and if we don't get a response within some timeout, then call thewith_event_loopfunction 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.
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.