Class: ZK::Pool::Base

Inherits:
Object
  • Object
show all
Includes:
Logger
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

Methods included from Logger

#logger, wrapped_logger, wrapped_logger=

Constructor Details

#initializeBase

Returns 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

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

handle all


129
130
131
132
133
# File 'lib/zk/pool.rb', line 129

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

#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 ?

Returns:

  • (Boolean)

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

def closed?
  @state == :closed
end

#closing?Boolean

is the pool shutting down?

Returns:

  • (Boolean)

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?

Returns:

  • (Boolean)

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


115
116
117
118
119
# File 'lib/zk/pool.rb', line 115

def locker(path)
  with_connection do |connection|
    connection.locker(path)
  end
end

#open?Boolean

is the pool initialized and in normal operation?

Returns:

  • (Boolean)

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

def open?
  @state == :open
end

#pool_stateObject

:nodoc:


139
140
141
# File 'lib/zk/pool.rb', line 139

def pool_state #:nodoc:
  @state
end

#sizeObject

:nodoc:


135
136
137
# File 'lib/zk/pool.rb', line 135

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

#with_connectionObject

yields next available connection to the block

raises PoolIsShuttingDownException immediately if close_all! has been called on this pool


105
106
107
108
109
110
111
112
# File 'lib/zk/pool.rb', line 105

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)


122
123
124
125
126
# File 'lib/zk/pool.rb', line 122

def with_lock(name, opts={}, &block)
  with_connection do |connection|
    connection.with_lock(name, opts, &block)
  end
end