Class: EM::PG::Sequel::ConnectionPool

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

Constant Summary collapse

DEFAULT_SIZE =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, opts = {}) ⇒ ConnectionPool

Returns a new instance of ConnectionPool.



9
10
11
12
13
14
15
16
17
# File 'lib/em-pg-sequel/connection_pool.rb', line 9

def initialize(db, opts = {})
  super
  @available = []
  @allocated = {}
  @pending = []

  @max_size = opts[:max_connections] || DEFAULT_SIZE
  hold {}
end

Instance Attribute Details

#allocatedObject (readonly)

Returns the value of attribute allocated.



7
8
9
# File 'lib/em-pg-sequel/connection_pool.rb', line 7

def allocated
  @allocated
end

#availableObject (readonly)

Returns the value of attribute available.



7
8
9
# File 'lib/em-pg-sequel/connection_pool.rb', line 7

def available
  @available
end

#max_sizeObject (readonly)

Returns the value of attribute max_size.



7
8
9
# File 'lib/em-pg-sequel/connection_pool.rb', line 7

def max_size
  @max_size
end

Instance Method Details

#disconnect(server = nil) ⇒ Object



47
48
49
50
# File 'lib/em-pg-sequel/connection_pool.rb', line 47

def disconnect(server = nil)
  @available.each{ |conn| db.disconnect_connection(conn) }
  @available.clear
end

#hold(server = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/em-pg-sequel/connection_pool.rb', line 23

def hold(server = nil)
  fiber = Fiber.current
  fiber_id = fiber.object_id

  if conn = @allocated[fiber_id]
    skip_release = true
  else
    conn = acquire(fiber) until conn
  end

  begin
    yield conn

  rescue ::Sequel::DatabaseDisconnectError => e
    db.disconnect_connection(conn)
    drop_failed(fiber_id)
    skip_release = true

    raise
  ensure
    release(fiber_id) unless skip_release
  end
end

#sizeObject



19
20
21
# File 'lib/em-pg-sequel/connection_pool.rb', line 19

def size
  @available.length + @allocated.length
end