Class: Bundler::ConnectionPool::TimedStack
- Inherits:
-
Object
- Object
- Bundler::ConnectionPool::TimedStack
- Defined in:
- lib/bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb
Overview
Examples:
ts = TimedStack.new(1) { MyConnection.new }
# fetch a connection
conn = ts.pop
# return a connection
ts.push conn
conn = ts.pop
ts.pop timeout: 5
#=> raises Bundler::ConnectionPool::TimeoutError after 5 seconds
Direct Known Subclasses
Instance Attribute Summary collapse
-
#max ⇒ Object
readonly
Returns the value of attribute max.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Returns
true
if there are no available connections. -
#initialize(size = 0, &block) ⇒ TimedStack
constructor
Creates a new pool with
size
connections that are created from the givenblock
. -
#length ⇒ Object
The number of connections available on the stack.
-
#pop(timeout = 0.5, options = {}) ⇒ Object
Retrieves a connection from the stack.
-
#push(obj, options = {}) ⇒ Object
(also: #<<)
Returns
obj
to the stack. -
#shutdown(reload: false, &block) ⇒ Object
Shuts down the TimedStack by passing each connection to
block
and then removing it from the pool.
Constructor Details
#initialize(size = 0, &block) ⇒ TimedStack
Creates a new pool with size
connections that are created from the given block
.
27 28 29 30 31 32 33 34 35 |
# File 'lib/bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb', line 27 def initialize(size = 0, &block) @create_block = block @created = 0 @que = [] @max = size @mutex = Thread::Mutex.new @resource = Thread::ConditionVariable.new @shutdown_block = nil end |
Instance Attribute Details
#max ⇒ Object (readonly)
Returns the value of attribute max.
21 22 23 |
# File 'lib/bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb', line 21 def max @max end |
Instance Method Details
#empty? ⇒ Boolean
Returns true
if there are no available connections.
104 105 106 |
# File 'lib/bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb', line 104 def empty? (@created - @que.length) >= @max end |
#length ⇒ Object
The number of connections available on the stack.
111 112 113 |
# File 'lib/bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb', line 111 def length @max - @created + @que.length end |
#pop(timeout = 0.5, options = {}) ⇒ Object
Retrieves a connection from the stack. If a connection is available it is immediately returned. If no connection is available within the given timeout a Bundler::ConnectionPool::TimeoutError is raised.
:timeout
is the only checked entry in options
and is preferred over the timeout
argument (which will be removed in a future release). Other options may be used by subclasses that extend TimedStack.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb', line 63 def pop(timeout = 0.5, = {}) , timeout = timeout, 0.5 if Hash === timeout timeout = .fetch :timeout, timeout deadline = current_time + timeout @mutex.synchronize do loop do raise Bundler::ConnectionPool::PoolShuttingDownError if @shutdown_block return fetch_connection() if connection_stored?() connection = try_create() return connection if connection to_wait = deadline - current_time raise Bundler::ConnectionPool::TimeoutError, "Waited #{timeout} sec" if to_wait <= 0 @resource.wait(@mutex, to_wait) end end end |
#push(obj, options = {}) ⇒ Object Also known as: <<
Returns obj
to the stack. options
is ignored in TimedStack but may be used by subclasses that extend TimedStack.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb', line 41 def push(obj, = {}) @mutex.synchronize do if @shutdown_block @shutdown_block.call(obj) else store_connection obj, end @resource.broadcast end end |
#shutdown(reload: false, &block) ⇒ Object
Shuts down the TimedStack by passing each connection to block
and then removing it from the pool. Attempting to checkout a connection after shutdown will raise Bundler::ConnectionPool::PoolShuttingDownError
unless :reload
is true
.
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb', line 89 def shutdown(reload: false, &block) raise ArgumentError, "shutdown must receive a block" unless block_given? @mutex.synchronize do @shutdown_block = block @resource.broadcast shutdown_connections @shutdown_block = nil if reload end end |