rabbitmq-c icon indicating copy to clipboard operation
rabbitmq-c copied to clipboard

How to terminate amqp_consume_message loop?

Open w3sip opened this issue 4 years ago • 2 comments

My program has a thread polling AMQP messages (e.g. blocks and waits on amqp_consume_message to return).

When the program terminates, I want to interrupt amqp_consume_message, terminate the thread, and then proceed with additional cleanup. What is the best way to accomplish that?

w3sip avatar Jan 29 '21 22:01 w3sip

Based on what is read in #390 this was a problem 4 years ago, and my understanding it still is.

#390 is discussing amqp_interrupt ... it can be implemented with a pair of control sockets:

  • allocate a control socket pair (two sockets connected to each other on localhost)
  • have amqp_simple_wait_frame_noblock listen on two sockets (the actual queue connection, and the "server"-side control socket)
  • amqp_interruptsends a byte of data on the "client"-side control socket
  • amqp_simple_wait_frame_noblock wakes up because of activity on the control socket, and can exit with an "INTERRUPTED" result

w3sip avatar Jan 29 '21 22:01 w3sip

@w3sip yes, this is still an issue with rabbitmq-c.

If what you care about is terminating the connection as a part of tearing down the process, then I would suggest the workaround described here.

Given an extra 4-years of perspective, I would not accept a PR that implements amqp_interrupt function. Instead, I would prefer a solution that augments rabbitmq-c so that all blocking functions optionally return an error that indicates that it was interrupted, and can be restarted by calling the same function again. To make this a backward compatible feature, a new API would need to be added to allow users to enable this feature (so it'd default to the existing behavior).

alanxz avatar Feb 01 '21 07:02 alanxz