Class: ZK::Pool::Base
Overview
Base class for a ZK connection pool. There are some applications that may require high synchronous throughput, which would be a suitable use for a connection pool. The ZK::Client::Threaded class is threadsafe, so it’s not a problem accessing it from multiple threads, but it is limited to one outgoing synchronous request at a time, which could cause throughput issues for apps that are making very heavy use of zookeeper.
The problem with using a connection pool is the added complexity when you
try to use watchers. It may be possible to register a watch with one
connection, and then call :watch => true on a different connection if
you’re not careful. Events delivered as part of an event handler have a
zk attribute which can be used to access the connection that the
callback is registered with.
Unless you’re sure you need a connection pool, then avoid the added complexity.
Direct Known Subclasses
Instance Attribute Summary (collapse)
-
- (Object) connections
readonly
:nodoc:.
Instance Method Summary (collapse)
-
- (Object) assert_open!
protected
-
- (Object) close_all!
close all the connections on the pool.
-
- (Boolean) closed?
has close_all! been called on this ConnectionPool ?.
-
- (Boolean) closing?
is the pool shutting down?.
-
- (Boolean) forced?
has the pool entered the take-no-prisoners connection closing part of shutdown?.
-
- (Base) initialize
constructor
A new instance of Base.
-
- (Object) locker(path)
lock lives on past the connection checkout.
-
- (Object) method_missing(meth, *args, &block)
handle all.
-
- (Boolean) open?
is the pool initialized and in normal operation?.
-
- (Object) pool_state
:nodoc:.
-
- (Object) size
:nodoc:.
-
- (Object) synchronize
protected
-
- (Object) with_connection
yields next available connection to the block.
-
- (Object) with_lock(name, opts = {}, &block)
prefer this method if you can (keeps connection checked out).
Constructor Details
- (Base) initialize
A new instance of Base
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/zk/pool.rb', line 25 def initialize @state = :init @mutex = Monitor.new @checkin_cond = @mutex.new_cond @connections = [] # all connections we control @pool = [] # currently available connections # this is required for 1.8.7 compatibility @on_connected_subs = {} @on_connected_subs.extend(MonitorMixin) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(meth, *args, &block)
handle all
122 123 124 125 126 |
# File 'lib/zk/pool.rb', line 122 def method_missing(meth, *args, &block) with_connection do |connection| connection.__send__(meth, *args, &block) end end |
Instance Attribute Details
- (Object) connections (readonly)
:nodoc:
23 24 25 |
# File 'lib/zk/pool.rb', line 23 def connections @connections end |
Instance Method Details
- (Object) assert_open! (protected)
141 142 143 |
# File 'lib/zk/pool.rb', line 141 def assert_open! raise Exceptions::PoolIsShuttingDownException unless open? end |
- (Object) close_all!
close all the connections on the pool
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/zk/pool.rb', line 60 def close_all! @mutex.synchronize do return unless open? @state = :closing @checkin_cond.wait_until { (@pool.size == @connections.length) or closed? } force_close! end end |
- (Boolean) closed?
has close_all! been called on this ConnectionPool ?
40 41 42 |
# File 'lib/zk/pool.rb', line 40 def closed? @state == :closed end |
- (Boolean) closing?
is the pool shutting down?
45 46 47 |
# File 'lib/zk/pool.rb', line 45 def closing? @state == :closing end |
- (Boolean) forced?
has the pool entered the take-no-prisoners connection closing part of shutdown?
55 56 57 |
# File 'lib/zk/pool.rb', line 55 def forced? @state == :forced end |
- (Object) locker(path)
lock lives on past the connection checkout
108 109 110 111 112 |
# File 'lib/zk/pool.rb', line 108 def locker(path) with_connection do |connection| connection.locker(path) end end |
- (Boolean) open?
is the pool initialized and in normal operation?
50 51 52 |
# File 'lib/zk/pool.rb', line 50 def open? @state == :open end |
- (Object) pool_state
:nodoc:
132 133 134 |
# File 'lib/zk/pool.rb', line 132 def pool_state #:nodoc: @state end |
- (Object) size
:nodoc:
128 129 130 |
# File 'lib/zk/pool.rb', line 128 def size #:nodoc: @mutex.synchronize { @pool.size } end |
- (Object) synchronize (protected)
137 138 139 |
# File 'lib/zk/pool.rb', line 137 def synchronize @mutex.synchronize { yield } end |
- (Object) with_connection
yields next available connection to the block
raises PoolIsShuttingDownException immediately if close_all! has been called on this pool
98 99 100 101 102 103 104 105 |
# File 'lib/zk/pool.rb', line 98 def with_connection assert_open! cnx = checkout(true) yield cnx ensure checkin(cnx) end |
- (Object) with_lock(name, opts = {}, &block)
prefer this method if you can (keeps connection checked out)
115 116 117 118 119 |
# File 'lib/zk/pool.rb', line 115 def with_lock(name, opts={}, &block) with_connection do |connection| connection.with_lock(name, opts, &block) end end |