Mongo::ConnectionTimeoutError possible problem with threading?

214 views
Skip to first unread message

Michael Lawlor

unread,
May 14, 2013, 11:06:00 AM5/14/13
to mongo...@googlegroups.com
Hi there,

I've been running into frequent Mongo::ConnectionTimeoutError exceptions when running MongoMapper behind Sidekiq.

I'm running a Sidekiq server with 30 concurrent actors on MRI 1.9.3. I've configured Mongo to use a connection pool with 40 connections and a 15 second timeout, figuring that should more than cover me. However I hit these timeouts constantly:

Mongo::ConnectionTimeoutError: could not obtain connection within 15.0 seconds. The max pool size is currently 40; consider increasing the pool size or timeout.

As far as I've read online, MongoMapper is thread safe, but I'm wondering if the connections to mongo are not being closed when a Sidekiq thread finishes?

There is a gem for Mongoid that adds some Sidekiq worker middleware to close connections when a worker finishes: https://github.com/mongoid/kiqstand

Is something similar necessary for MongoMapper? Any advice would be extremely appreciated, and I'm happy to implement the solution myself, if someone could point me in the right direction.

Thanks!

John Nunemaker

unread,
May 14, 2013, 11:20:30 AM5/14/13
to mongo...@googlegroups.com
I would assume MM would need the same type of stuff as Mongoid, but I'm not familiar with how sidekiq works beyond that it uses celluloid.

--
--
You received this message because you are subscribed to the Google
Groups "MongoMapper" group.
For more options, visit this group at
http://groups.google.com/group/mongomapper?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups "MongoMapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongomapper...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Michael Lawlor

unread,
May 14, 2013, 11:30:56 AM5/14/13
to mongo...@googlegroups.com
Thanks John,

Right, Sidekiq uses Celluloid and threads to run its workers. It has a middleware concept similar to Rack that lets you wrap middleware around thread creation/destruction. Kiqstand is one such middleware.

I'm not familiar with Mongoid at all, but it apparently has a concept of "sessions" which appear to be mongodb connections that have to be disconnected when a thread finishes. If you have a look at the kiqstand code, it's really just two lines that run after a thread completes:

::Mongoid::IdentityMap.clear
::Mongoid.disconnect_sessions

Is there anything analogous in MongoMapper?



You received this message because you are subscribed to a topic in the Google Groups "MongoMapper" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongomapper/HYT1UZgcXPg/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to mongomapper...@googlegroups.com.

Michael Lawlor

unread,
May 14, 2013, 11:34:31 AM5/14/13
to mongo...@googlegroups.com
another example is the Sidekiq ActiveRecord middleware that calls ::ActiveRecord::Base.clear_active_connections! after a worker thread finishes.

John Nunemaker

unread,
May 14, 2013, 11:37:07 AM5/14/13
to mongo...@googlegroups.com
MM just wraps the mongo connection class. I imagine it has connect/disconnect methods. The connection is stored in MongoMapper.connection.

Michael Lawlor

unread,
May 14, 2013, 11:43:55 AM5/14/13
to mongo...@googlegroups.com
okay I will dig into the mongo gem a bit then. Thanks.
Reply all
Reply to author
Forward
0 new messages