Class: Telegram::ConnectionPool
- Inherits:
-
Array
- Object
- Array
- Telegram::ConnectionPool
- Includes:
- Logging
- Defined in:
- lib/telegram/connection_pool.rb
Overview
Connection Pool
Instance Attribute Summary collapse
-
#size ⇒ Integer
readonly
Connection pool size, will be set when initialized.
Instance Method Summary collapse
-
#acquire(&callback) {|connection| ... } ⇒ Object
Acquire available connection.
-
#communicate(*messages, &block) ⇒ Object
Communicate with acquired connection.
-
#initialize(size = 10, &block) ⇒ ConnectionPool
constructor
Initialize ConnectionPool.
Methods included from Logging
configure_logger_for, #logger, logger_for
Constructor Details
#initialize(size = 10, &block) ⇒ ConnectionPool
Initialize ConnectionPool
16 17 18 19 20 |
# File 'lib/telegram/connection_pool.rb', line 16 def initialize(size=10, &block) size.times do self << block.call if block_given? end end |
Instance Attribute Details
#size ⇒ Integer (readonly)
Returns Connection pool size, will be set when initialized.
10 11 12 |
# File 'lib/telegram/connection_pool.rb', line 10 def size @size end |
Instance Method Details
#acquire(&callback) {|connection| ... } ⇒ Object
Acquire available connection
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/telegram/connection_pool.rb', line 43 def acquire(&callback) acq = Proc.new { conn = self.find { |conn| conn.available? } if not conn.nil? and conn.connected? callback.call(conn) else logger.warn("Failed to acquire available connection, retry after 0.1 second") EM.add_timer(0.1, &acq) end } EM.add_timer(0, &acq) end |
#communicate(*messages, &block) ⇒ Object
Communicate with acquired connection
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/telegram/connection_pool.rb', line 27 def communicate(*, &block) begin acquire do |conn| conn.communicate(*, &block) end rescue Exception => e logger.error("Error occurred during the communicating: #{e.inspect} #{e.backtrace}") end end |