Class: Telegram::ConnectionPool

Inherits:
Array
  • Object
show all
Includes:
Logging
Defined in:
lib/telegram/connection_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, #logger, logger_for

Constructor Details

#initialize(size = 10, &block) ⇒ ConnectionPool

Returns a new instance of ConnectionPool.



7
8
9
10
11
# File 'lib/telegram/connection_pool.rb', line 7

def initialize(size=10, &block)
  size.times do 
    self << block.call if block_given?
  end
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



5
6
7
# File 'lib/telegram/connection_pool.rb', line 5

def size
  @size
end

Instance Method Details

#acquire(&callback) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# 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) ⇒ Object



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