Class: Pandemic::ConnectionPool
- Inherits:
-
Object
- Object
- Pandemic::ConnectionPool
show all
- Includes:
- Util
- Defined in:
- lib/pandemic/connection_pool.rb
Defined Under Namespace
Classes: CreateConnectionUndefinedException, TimedOutWaitingForConnectionException
Instance Method Summary
collapse
Methods included from Util
#host_port, #logger, #with_mutex
Constructor Details
Returns a new instance of ConnectionPool.
6
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/pandemic/connection_pool.rb', line 6
def initialize(options = {})
@connected = false
@mutex = Monitor.new
@queue = @mutex.new_cond
@available = []
@connections = []
@max_connections = options[:max_connections] || 10
@min_connections = options[:min_connections] || 1
@connect_at_define = options.include?(:connect_at_define) ? options[:connect_at_define] : true
@timeout = MONITOR_TIMEOUT_AVAILABLE ? options[:timeout] || 3 : nil
end
|
Instance Method Details
#add_connection! ⇒ Object
18
19
20
21
22
23
24
|
# File 'lib/pandemic/connection_pool.rb', line 18
def add_connection!
@mutex.synchronize do
conn = create_connection
@available << conn if conn && !conn.closed?
end
end
|
#available_count ⇒ Object
86
87
88
|
# File 'lib/pandemic/connection_pool.rb', line 86
def available_count
@available.size
end
|
#connect ⇒ Object
79
80
81
82
83
84
|
# File 'lib/pandemic/connection_pool.rb', line 79
def connect
if !connected?
@min_connections.times { add_connection! }
grim_reaper
end
end
|
#connected? ⇒ Boolean
75
76
77
|
# File 'lib/pandemic/connection_pool.rb', line 75
def connected?
@connected
end
|
#connections_count ⇒ Object
90
91
92
|
# File 'lib/pandemic/connection_pool.rb', line 90
def connections_count
@connections.size
end
|
#create_connection(&block) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/pandemic/connection_pool.rb', line 26
def create_connection(&block)
if block.nil?
if @create_connection
conn = @create_connection.call
if conn && !conn.closed?
@connections << conn
@connected = true
conn
end
else
raise CreateConnectionUndefinedException.new("You must specify a block to create connections")
end
else
@create_connection = block
connect if @connect_at_define
end
end
|
#destroy_connection(connection = nil, &block) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/pandemic/connection_pool.rb', line 44
def destroy_connection(connection = nil, &block)
if block.nil?
if @destroy_connection
@destroy_connection.call(connection)
else
if connection && !connection.closed?
connection.close
end
end
@connections.delete(connection)
@available.delete(connection)
@connected = false if @connections.empty?
else
@destroy_connection = block
end
end
|
#disconnect ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/pandemic/connection_pool.rb', line 94
def disconnect
@mutex.synchronize do
return if @disconnecting
@disconnecting = true
@connected = false @grim_reaper.kill if @grim_reaper && @grim_reaper.alive?
@available.dup.each do |conn|
destroy_connection(conn)
end
while @connections.size > 0 && @queue.wait
@available.dup.each do |conn|
destroy_connection(conn)
end
end
@disconnecting = false
end
end
|
#status_check(connection = nil, &block) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/pandemic/connection_pool.rb', line 63
def status_check(connection = nil, &block)
if block.nil?
if @status_check
@status_check.call(connection)
else
connection && !connection.closed?
end
else
@status_check = block
end
end
|
#with_connection(&block) ⇒ Object
114
115
116
117
118
119
120
121
122
|
# File 'lib/pandemic/connection_pool.rb', line 114
def with_connection(&block)
connection = nil
begin
connection = checkout
block.call(connection)
ensure
checkin(connection) if connection
end
end
|