Handling errors in RabbitMQ consumer

What I’ve done is the following. I’ve created a wrapper for a Connection and for a Channel.

The wrapper for the Connection starts a new connection in the init and traps exits Process.flag(:trap_exit,t rue) and it links the pid of the connection Process.link(connection.pid)`

So something like this:

{:ok, conn} = AMQP.Connection.open(...)

Process.flag(:trap_exit, true)
Process.link(conn.pid)

As a last thing, I’ll implement the terminate callback and try to close the Connection/Channel if it still lives.

For Channel I do the same thing. In my code I put my Connection and Channel wrappers in a supervision tree so that they get killed if needed.

I’m not sure that this is the correct way, but it works to have no zombie channels when something goes wrong in my code.
I was thinking about asking this in the amqp repository, why the Connection and Channel are no otp behaviours, because if they were, the wrappers wouldn’t be necessary.

See this topic as well: Processing multiple messages in parallel with AMQP

You basically setup one connection and multiple channels.
For error handling and timeouts. In the handle_info that receives the message I start a task and await it with a sensible timeout. If it’s to slow, or something fails you can do your error handling.