Class: ZK::Pool::Base

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/zk/pool.rb

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

Bounded

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase



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

#method_missing(meth, *args, &block) ⇒ Object

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

#connectionsObject (readonly)

:nodoc:



23
24
25
# File 'lib/zk/pool.rb', line 23

def connections
  @connections
end

Instance Method Details

#assert_open!Object (protected)



141
142
143
# File 'lib/zk/pool.rb', line 141

def assert_open!
  raise Exceptions::PoolIsShuttingDownException unless open? 
end

#close_all!Object

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

#closed?Boolean

has close_all! been called on this ConnectionPool ?



40
41
42
# File 'lib/zk/pool.rb', line 40

def closed?
  @state == :closed
end

#closing?Boolean

is the pool shutting down?



45
46
47
# File 'lib/zk/pool.rb', line 45

def closing?
  @state == :closing
end

#forced?Boolean

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

#locker(path) ⇒ Object

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

#open?Boolean

is the pool initialized and in normal operation?



50
51
52
# File 'lib/zk/pool.rb', line 50

def open?
  @state == :open
end

#pool_stateObject

:nodoc:



132
133
134
# File 'lib/zk/pool.rb', line 132

def pool_state #:nodoc:
  @state
end

#sizeObject

:nodoc:



128
129
130
# File 'lib/zk/pool.rb', line 128

def size #:nodoc:
  @mutex.synchronize { @pool.size }
end

#synchronizeObject (protected)



137
138
139
# File 'lib/zk/pool.rb', line 137

def synchronize
  @mutex.synchronize { yield }
end

#with_connectionObject

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

#with_lock(name, opts = {}, &block) ⇒ Object

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