Azolo / websockex

An Elixir Websocket Client
MIT License
520 stars 100 forks source link

handle disconnect backoff #111

Open BrendanBall opened 2 years ago

BrendanBall commented 2 years ago

Websockex currently supports infinite reconnect to handle intermittent network failures, however it is still quite limiting. Currently it only supports attempting to reconnect directly after disconnecting with:

      def handle_disconnect(status, state) do
        {:reconnect, state}
      end

However this means that it spams reconnect. You could theoretically put in a Process.sleep to handle some kind of backoff, however I don't think that's a good solution since it's a blocking operation, so backoff is better implemented with Process.send_after.

I believe Ecto has implemented this quite well for DB connections, and has created a reusable library which defines a behaviour for implementing connection processes (see https://hexdocs.pm/connection/Connection.html). Unfortunately it does this by implementing its own special process, which WebSockex also does, so the 2 can't be tightly integrated. I'm currently looking at using the Connection library on top of WebSockex to solve this, which I believe should work, however given that WebSockex almost already supports this and it's clearly a common feature (since we want our applications to be fault tolerant), I want to find out whether you're interested in having reconnection logic in WebSockex improved so that it's as simple as specifying some backoff parameters for users of WebSockex. I'm currently considering just copying the design of Connection to WebSockex since I don't think it's that much code and WebSockex has its own reasons for implementing a custom special process (although it would've been easy to integrate this if it was just using a GenServer).

What's your opinion? Would you accept a PR for this?