Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I deal with dequeue connection timeouts in rails/mongoid?

My stack: RoR4.2.6, Mongoid 5.0.1

I'm benchmarking my site using apache benchmark and keep hitting errors like the following:

2016-03-24 22:15:36 +0000 pid=23187, severity=FATAL, 
ActionView::Template::Error (Timed out attempting to dequeue connection after 1 sec.):
    22:   =link_to '/albums/' + mention.object.slug do
    23:     .small-12.medium-6.large-4.columns.left
    24:       .mention-box
    25:         %img.mention-thumb{src: mention.object.thumb_url}
    26:         %h5.mention-name
    27:           = mention.object.name
    28:           %br
  app/models/mention.rb:13:in `object'
  app/views/posts/_full_mention.html.haml:25:in `block in _app_views_posts__full_mention_html_haml___1744802549767261808_47000690052420'

and just for reference, this is the line that's getting called in mention.rb, just a simple find query:

  def object
    Object.const_get(type).find(mention_id)
  end

My assumption is that this means I'm hitting mongoDB with too many requests and it can't keep up, but not entirely sure how to resolve this. Should I just be setting a higher queue timeout for mongoid? Appreciate any advice!

like image 706
Christopher Changchien Avatar asked Mar 24 '16 22:03

Christopher Changchien


2 Answers

Had same problem, solved by adding wait_queue_timeout attribute in mongoid.yml production config:

production:
  clients:
    default:
     uri:  mongodb://xxx.com:27017/mongo
     options:
       connect_timeout: 15
       wait_queue_timeout: 15
like image 91
Vitali Mogilevsky Avatar answered Nov 12 '22 06:11

Vitali Mogilevsky


Instead of tweaking wait_queue_timeout which is for:

The time to wait, in seconds, in the connection pool for a connection [..] [1]

i would suggest tweaking min_pool_size and max_pool_size.

The minimum/maximum number of connections in the connection pool [1]

so your server/background worker doesn't have to wait for a connection.

If you are running your application using multi-threaded application server (Puma) or inside Sidekiq, you need to set up connection pool with appropriate size.

[1] https://docs.mongodb.com/ecosystem/tutorial/mongoid-installation/

like image 35
Maciej Majewski Avatar answered Nov 12 '22 07:11

Maciej Majewski