Skip to content

Instantly share code, notes, and snippets.

@markmeeus
Last active August 31, 2017 12:28
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markmeeus/6412088 to your computer and use it in GitHub Desktop.
Save markmeeus/6412088 to your computer and use it in GitHub Desktop.
A patch to reuse Moped connections in Mongoid inside Celluliod Actors. If you use Sidekiq with loads of work, you may want reuse your connections. Do not use this toghether with kiqstand as they try to achieve opposite results.
module Celluloid
class Thread < ::Thread
def []= key, value
if key_on_thread? key
thread_vars[key] = value
else
super
end
end
def [] key
if key_on_thread? key
thread_vars[key]
else
super
end
end
private
def thread_vars
@thread_vars ||= {}
end
def key_on_thread? key
key == "[mongoid]:sessions"
end
end
end
@jonhyman
Copy link

jonhyman commented Dec 2, 2013

In Ruby 2.0, you can use thread_variable_get/thread_variable_set instead of keeping the hash around yourself.

@khoan
Copy link

khoan commented Apr 5, 2014

how do you use this patch in Rails 4 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment