Automatically reconnect to a TCP host

TL; DR: is there reusable code to automatically reconnect to a TCP server, which sometimes fails?


I am writing a server application - I call it hal- it also opens some TCP connections with other servers - among them xbmc. I originally wrote this so that when it xbmcfails, the xbmcerlang process will stop and subsequently restart by its supervisor.

This is apparently not a very good way to make persistent TCP connections in erlang. First of all, this really does not work: if it xbmcfails, the processes will restart too quickly, and the supervisor will shut down the entire program hal. Secondly, I apparently shouldn't use a supervisor to do this: Erlang Supervisor's strategy for reloading connections to Downed Hosts (When I read this related question only answers “does the supervisor solve this?”, And is not a duplicate of my question )

I think this sounds like a reasonably common use case, keeping the TCP connection as possible as possible, even with a host that goes blank from time to time. Is there some kind of OTP or other library code that I should use to achieve this?

+5
source share
1 answer

No, there is nothing in OTP that does this for you.

I would say that given the number of times I wrote this myself, it is time to create a decent connection management library. In particular, one that reviews the recent history of connection attempts and results can be provided with an endpoint address (host name and port) and use multiple addresses returned from a host search to quickly switch to another resource for endpoints that have multiple addresses destination.

+3
source

All Articles