Class: Vault::ConnectionPool
- Inherits:
-
Object
- Object
- Vault::ConnectionPool
show all
- Defined in:
- lib/vault/vendor/connection_pool.rb,
lib/vault/vendor/connection_pool/version.rb
Defined Under Namespace
Classes: Error, PoolShuttingDownError, TimedStack, Wrapper
Constant Summary
collapse
- DEFAULTS =
{size: 5, timeout: 5}
- VERSION =
"2.2.0"
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(options = {}, &block) ⇒ ConnectionPool
Returns a new instance of ConnectionPool.
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/vault/vendor/connection_pool.rb', line 45
def initialize(options = {}, &block)
raise ArgumentError, 'Connection pool requires a block' unless block
options = DEFAULTS.merge(options)
@size = options.fetch(:size)
@timeout = options.fetch(:timeout)
@available = TimedStack.new(@size, &block)
@key = :"current-#{@available.object_id}"
end
|
Class Method Details
.wrap(options, &block) ⇒ Object
41
42
43
|
# File 'lib/vault/vendor/connection_pool.rb', line 41
def self.wrap(options, &block)
Wrapper.new(options, &block)
end
|
Instance Method Details
#checkin ⇒ Object
99
100
101
102
103
104
|
# File 'lib/vault/vendor/connection_pool.rb', line 99
def checkin
conn = pop_connection
@available.push(conn) if stack.empty?
nil
end
|
#checkout(options = {}) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/vault/vendor/connection_pool.rb', line 87
def checkout(options = {})
conn = if stack.empty?
timeout = options[:timeout] || @timeout
@available.pop(timeout: timeout)
else
stack.last
end
stack.push conn
conn
end
|
#shutdown(&block) ⇒ Object
106
107
108
|
# File 'lib/vault/vendor/connection_pool.rb', line 106
def shutdown(&block)
@available.shutdown(&block)
end
|
#with(options = {}) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/vault/vendor/connection_pool.rb', line 60
def with(options = {})
Thread.handle_interrupt(Exception => :never) do
conn = checkout(options)
begin
Thread.handle_interrupt(Exception => :immediate) do
yield conn
end
ensure
checkin
end
end
end
|