Does aknowledging the message kills handler thread?
Closed this issue · 4 comments
Hello!
Didn't know where to write you, so writing here.
I'm interested in the scenario where we're acknowledging the message AT THE START of callback function. Is it possible, that amqp will immediately kill the handler thread?
Hi,
I'm not sure I'm understanding your question. Do you mean something like this:
consumeMsgs chan "myQueue" Ack $ \(msg, env) -> do
ackEnv env
-- Could the handler-thread be killed right here, before the call to handleMsg?
handleMsg msg
Yep, precisely
In theory this could happen. For example, when you call closeConnection
, all channel-threads will be killed, even if they might be in the middle of the consumeMsgs-callback. Similar behaviour exists for closeChannel
.
In practice I think this scenario is unlikely and will probably only happen if you have a very high number of incoming messages; but if you absolutely need to avoid it, you may want to only call ackEnv
after you've fully processed the message.
Alternatively, you could use the consumeMsgs-callback to put the message in some kind of queue, and then use a separate thread to read messages from the queue and handle them. Roughly like this:
import qualified Control.Concurrent.Chan as C
msgQueue <- C.newChan
consumeMsgs chan "myQueue" Ack $ \(msg, env) -> do
C.writeChan msgQueue (msg, env)
ackEnv env
-- in a different thread:
go = do
(msg, env) <- C.readChan msgQueue
-- TOOD: handle msg
go
Thank you very much!