wagslane / go-rabbitmq

A wrapper of streadway/amqp that provides reconnection logic and sane defaults
https://blog.boot.dev/golang/connecting-to-rabbitmq-in-golang-easy/
MIT License
808 stars 129 forks source link

Implementation for graceful shutdown for consumers #48

Closed alter123 closed 2 years ago

alter123 commented 3 years ago

There seems to be missing case while handling SIGINT, SIGTERM signals in the implementation, if the consumers are running in goroutine. One possible implementation can be stop listening to new messages (closing the channel), and provide deadline context to running goroutines.

Would like to know if there can more elegant way for handling these case, also open for contributing.

miketonks commented 3 years ago

I'm also interested in the correct way to shutdown the consumers gracefully.

Reading through the docs, I can't find a complete example but looks like we should first call StopConsuming(consumerName) and then Disconnect()

I updated the consumer example and it appears to work ok.

See PR https://github.com/wagslane/go-rabbitmq/pull/51

wagslane commented 2 years ago

example given in #51

christian-ehmig commented 2 years ago

Seems like StopConsuming was removed in previous releases. How is the current approach for a graceful shutdown of in-flight consumer goroutines? I don't see a way in the current code base how one could wait for running handlers to finish their work and even more important, to send a final ACK before the consumer is finally closed.

NicklasWallgren commented 2 years ago

I'm been wondering the same. We should be able to gracefully shutdown the consumers.

  1. Stop consuming new message.
  2. Let in-flight consumers finish gracefully, and send ACK/NACK.
  3. Close the channel and connection.
biswaz commented 1 year ago

Looks like there is no way to achieve graceful shutdown with go-rabbitmq. I'm planning to move to the amqp091-go because of this.

@wagslane Please clarify if graceful shutdown of in-flight consumer goroutines is possible anymore.