[erlang-questions] handling crash on db connect

Jachym Holecek freza@REDACTED
Thu Jun 6 12:54:59 CEST 2013


# Jesper Louis Andersen 2013-06-06:
> On Jun 5, 2013, at 10:59 PM, Paul Rubin <paul@REDACTED> wrote:
> 
> > I'm using gar1t's redis driver and find that if the redis server is running, redis:connect() causes the client to crash.  try/catch doesn't handle it, so I think it's linking some other process which then crashes and propagates the exit.
> > 
> > What's the right way to handle the crash, in order to report some reasonable error and/or do a sleep/retry loop?  One obvious way is trapexit() and explicitly handle the exit signal, but I have the impression that this would be overly drastic in Erlang.  Connection failure to a db should be a fairly routine error condition, so I feel like there's something I'm missing.
> > 
> > Any advice?  Thanks.
> > 
> 
> Rewrite the driver? It makes sense to return some kind of error tuple in this case because it is expected behaviour that you cannot establish a connection. Then you can crash on
> 
>   ok = redis:connect(?)
> 
> which is better. Of course this is a matter of style, but I much prefer attempts at connections to be able to fail like this.

Yes. Probably good to have 'redis:connect(...)' retry connect periodically and
send you a message when it's up -- not only connect succeeded but ideally some
heartbeat roundtrip completed with reasonable latency too. When connection
goes down, send a message about it and go back to reconnect state. Keep doing
heartbeats every now and then while connection is up so you spot failures
proactively. Can additionally leverage user traffic for link monitoring (ex-
cessive request timeouts -- probably not good, whether it's failing link
or overloaded peer you probably want to go away for a while).

You could push this extra complexity to user code, but then you'll likely find
that all users want it. Can't trust those dodgy networks after all, they always
fail, sometimes in funny ways.

BR,
	-- Jachym



More information about the erlang-questions mailing list