Class: EM::Sequel::FiberedConnectionPool

Inherits:
Sequel::ConnectionPool
  • Object
show all
Defined in:
lib/em-postgresql-sequel/fibered_connection_pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ FiberedConnectionPool



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/em-postgresql-sequel/fibered_connection_pool.rb', line 5

def initialize(opts={}, &block)
  super
  @available = []
  @waiting = []
  
  Integer(opts[:max_connections] || 4).times do
    @available << make_new(DEFAULT_SERVER)
  end
  
  @mutex = Mutex.new
end

Instance Method Details

#disconnect(opts = {}, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/em-postgresql-sequel/fibered_connection_pool.rb', line 17

def disconnect(opts={}, &block)
  @mutex.synchronize do
    block ||= @disconnection_proc
    if block
      m = Mutex.new
      @available.each do |conn| 
        m.synchronize do
          block.call(conn)
        end
      end
    end
    @available.clear
  end
end

#hold(server = nil, &blk) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/em-postgresql-sequel/fibered_connection_pool.rb', line 36

def hold(server=nil, &blk)
  if @available.empty?
    @waiting << Fiber.current
    Fiber.yield
  end
  
  @waiting.delete Fiber.current
  conn = @available.pop
  
  begin
    blk.call(conn)
  rescue ::Sequel::DatabaseDisconnectError
    _conn = conn
    conn = nil
    @mutex.synchronize do
      @disconnection_proc.call(_conn) if @disconnection_proc && _conn
      @available << make_new(DEFAULT_SERVER)
    end
    raise
  ensure
    @available << conn if conn
    if waiting = @waiting.shift
      waiting.resume
    end
  end
end

#sizeObject



32
33
34
# File 'lib/em-postgresql-sequel/fibered_connection_pool.rb', line 32

def size
  @available.length
end