Class: OverSIP::Modules::Mysql::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/oversip-mod-mysql.rb

Instance Method Summary collapse

Constructor Details

#initialize(pool_size, options, block) ⇒ Pool

Returns a new instance of Pool.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/oversip-mod-mysql.rb', line 51

def initialize pool_size, options, block
  @em_synchrony_connectionpool = ::EM::Synchrony::ConnectionPool.new(size: pool_size) do
    # Avoid the hash to be modified internally.
    options = options.clone
    # Force DB autoreconnect.
    options[:reconnect] = true

    conn = ::Mysql2::EM::Client.new(options)

    # Call the given block by passing conn as argument.
    block.call(conn)  if block
    conn
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object



66
67
68
# File 'lib/oversip-mod-mysql.rb', line 66

def method_missing method, *args, &blk
  @em_synchrony_connectionpool.__send__ method, *args, &blk
end

Instance Method Details

#query(*args, &blk) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/oversip-mod-mysql.rb', line 70

def query *args, &blk
  # If we are not in the OverSIP Root Fiber then do nothing special.
  if ::Fiber.current != ::OverSIP.root_fiber
    @em_synchrony_connectionpool.__send__ :query, *args, &blk
  # Otherwise run the query within a new Fiber.
  else
    ::Fiber.new do
      @em_synchrony_connectionpool.__send__ :query, *args, &blk
    end.resume
  end
end