Where to establish Bunny connections in Ruby On Rails using the Puma web server? #642
Replies: 3 comments 3 replies
-
Initializing connections that way is fine. However, with Puma your resources will be accessed concurrently, so access to all channel operations with Bunny have to be synchronized. |
Beta Was this translation helpful? Give feedback.
-
The approach outlined in this post seems to be a good middle ground for at least some apps. Obviously there cannot be a single recommendation for all concurrency hazards. |
Beta Was this translation helpful? Give feedback.
-
Thanks for this. The blog post linked is from 2015, and for reasons I don't understand, this strategy no longer works. There doesn't seem to be a way to use |
Beta Was this translation helpful? Give feedback.
-
I am using Ruby On Rails 7 with the Puma web server.
Puma is a multi-threaded web server.
I send RabbitMQ messages from various rails "controllers".
I know that I am not supposed to make a new
connection
orchannel
with each controller action. But at the same time, I know I am not supposed to share these between threads.The
Bunny
docs seem to have recommendations for Unicorn, which was the dominant web server used before 2017 with Rails:http://rubybunny.info/articles/connecting.html#using_bunny_with_unicorn
I can't find any best practice recommendation for
Puma
.Currently, in my
puma.rb
initialization file, I am doing something like this, copying the recommendations for Unicorn:Puma does not seem to have a concept like Unicorn's
after_fork
, but I have noticed that it's recommended to initializedredis
connections in thison_worker_boot
area, so that's why I am using it for RabbitMQ.I have been using it like this for several months. In each rails controller action, I use the
$rabbitmq_channel
, like this:This has generally worked, but... recently, with higher traffic, however, I have had some worrying errors where certain messages were sent to entire the WRONG queue.
Here is some possibly related reference material:
#515
#494
https://wework.github.io/ruby/rails/bunny/rabbitmq/threads/concurrency/puma/errors/2015/11/12/bunny-threads/
https://www.cloudamqp.com/blog/part4-rabbitmq-13-common-errors.html
Beta Was this translation helpful? Give feedback.
All reactions