Sha256: d40a8393e84948c3e573ea8c8e040539109c2de4f3420eaebd307c3542983860

Contents?: true

Size: 1016 Bytes

Versions: 4

Compression:

Stored size: 1016 Bytes

Contents

begin
  require "mongo"
rescue LoadError => error
  raise "Missing EM-Synchrony dependency: gem install mongo"
end

# monkey-patch Mongo to use em-synchrony's socket and thread classs
old_verbose = $VERBOSE
begin
  $VERBOSE = nil
  class Mongo::Connection
    TCPSocket = ::EventMachine::Synchrony::TCPSocket
    Mutex = ::EventMachine::Synchrony::Thread::Mutex
    ConditionVariable = ::EventMachine::Synchrony::Thread::ConditionVariable
  end

  class Mongo::Pool
    TCPSocket = ::EventMachine::Synchrony::TCPSocket
    Mutex = ::EventMachine::Synchrony::Thread::Mutex
    ConditionVariable = ::EventMachine::Synchrony::Thread::ConditionVariable
  end

  class EventMachine::Synchrony::MongoTimeoutHandler
    def self.timeout(op_timeout, ex_class, &block)
      f = Fiber.current
      timer = EM::Timer.new(op_timeout) { f.resume(nil) }
      res = block.call
      timer.cancel
      res
    end
  end

  Mongo::TimeoutHandler = EventMachine::Synchrony::MongoTimeoutHandler
ensure
  $VERBOSE = old_verbose
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
em-synchrony-1.0.6 lib/em-synchrony/mongo.rb
em-synchrony-1.0.5 lib/em-synchrony/mongo.rb
em-synchrony-1.0.4 lib/em-synchrony/mongo.rb
em-synchrony-1.0.3 lib/em-synchrony/mongo.rb