class Telegram::ConnectionPool

Attributes

size[R]

Public Class Methods

new(size=10, &block) click to toggle source
# File lib/telegram/connection_pool.rb, line 7
def initialize(size=10, &block)
  size.times do 
    self << block.call if block_given?
  end
end

Public Instance Methods

acquire(&callback) click to toggle source
# File lib/telegram/connection_pool.rb, line 24
def acquire(&callback)
  acq = Proc.new {
    conn = self.find { |conn| conn.available? }
    if not conn.nil? and conn.connected?
      callback.call(conn)
    else
      logger.warning("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) click to toggle source
# File lib/telegram/connection_pool.rb, line 13
def communicate(*messages, &block)
  begin
    acquire do |conn|
      conn.communicate(*messages, &block)
    end
  rescue Exception => e
    logger.error("Error occurred during the communicating: #{e.inspect} #{e.backtrace}")
  end

end