class ThreadConnectionPool
Public Class Methods
new(opts, &block)
click to toggle source
Calls superclass method
ConnectionPool::new
# File lib/thrift_client/pool/thread_connection_pool.rb, line 5 def initialize(opts, &block) super @mutex = Mutex.new @condition = ConditionVariable.new end
Public Instance Methods
destroy()
click to toggle source
Destroy all connections in this pool. It will waiting until all connections are idle and than close them one by one.
# File lib/thrift_client/pool/thread_connection_pool.rb, line 26 def destroy while @idle.size < @size @mutex.synchronize do @condition.wait(@mutex) end end @idle.each do | conn | begin conn.disconnect rescue Exception => e puts "close connection error! #{e.backtrace}" end end end
execute() { |conn| ... }
click to toggle source
Choose first idle connection and pass it to the supplied block. This will block indefinitely until there is an idle connection to service the request.
# File lib/thrift_client/pool/thread_connection_pool.rb, line 14 def execute t = Thread.current begin conn = acquire(t) yield conn ensure release(t) end end
Private Instance Methods
acquire(thread)
click to toggle source
Acquire a lock on a connection and assign it to executing thread
-
if connection is idle, pass it back to the calling block
-
if pool is full, stop the current thread until connection is idle
# File lib/thrift_client/pool/thread_connection_pool.rb, line 46 def acquire(thread) @mutex.synchronize do if conn = @idle.pop @active[thread.object_id] = conn return conn else @pending.push thread @condition.wait(@mutex) @pending.shift end end acquire(thread) end
release(thread)
click to toggle source
Release connection assigned to the supplied thread and wakeup any other pending connections (which will immediately try to run acquire on the pool)
# File lib/thrift_client/pool/thread_connection_pool.rb, line 63 def release(thread) @mutex.synchronize do @idle.push(@active.delete(thread.object_id)) @condition.broadcast end end