Class: ThreadConnectionPool

Inherits:
ConnectionPool show all
Defined in:
lib/thrift_client/pool/thread_connection_pool.rb

Instance Method Summary collapse

Methods inherited from ConnectionPool

#status

Constructor Details

#initialize(opts, &block) ⇒ ThreadConnectionPool

Returns a new instance of ThreadConnectionPool.



5
6
7
8
9
# File 'lib/thrift_client/pool/thread_connection_pool.rb', line 5

def initialize(opts, &block)
  super
  @mutex = Mutex.new
  @condition = ConditionVariable.new
end

Instance Method Details

#destroyObject

Destroy all connections in this pool. It will waiting until all connections are idle and than close them one by one.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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

#executeObject

block. This will block indefinitely until there is an idle connection to service the request.



14
15
16
17
18
19
20
21
22
# 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