Class: MultiThink
- Inherits:
-
Object
show all
- Defined in:
- lib/multithink.rb,
lib/multithink/version.rb
Defined Under Namespace
Classes: Connection, PoolShuttingDownError, TimedStack
Constant Summary
collapse
- DEFAULTS =
{
size: 5,
timeout: 5,
servers: [
{
host: '127.0.0.1',
port: 28015
}
],
conn_options: {}
}
- VERSION =
"0.1.4"
Instance Method Summary
collapse
Constructor Details
#initialize(options = {}) ⇒ MultiThink
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/multithink.rb', line 17
def initialize(options = {})
options = DEFAULTS.merge(options)
@size = options.fetch(:size)
@timeout = options.fetch(:timeout)
servers = options.fetch(:servers)
conn_options = options.fetch(:conn_options)
conn_options[:servers] = servers
@available = TimedStack.new(@size, conn_options)
@key = :"current-#{@available.object_id}"
end
|
Instance Method Details
#checkin ⇒ Object
53
54
55
56
57
58
59
60
|
# File 'lib/multithink.rb', line 53
def checkin
stack = ::Thread.current[@key]
conn = stack.pop
if stack.empty?
@available << conn
end
nil
end
|
#checkout(options = {}) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/multithink.rb', line 39
def checkout(options = {})
stack = ::Thread.current[@key] ||= []
if stack.empty?
timeout = options[:timeout] || @timeout
conn = @available.pop(timeout)
else
conn = stack.last
end
stack.push conn
conn
end
|
#shutdown ⇒ Object
62
63
64
|
# File 'lib/multithink.rb', line 62
def shutdown
@available.shutdown
end
|
#with(options = {}) ⇒ Object
30
31
32
33
34
35
36
37
|
# File 'lib/multithink.rb', line 30
def with(options = {})
conn = checkout(options)
begin
yield conn
ensure
checkin
end
end
|