class Backchat::ConnectionPool

Constants

DEFAULT_SETTINGS

Attributes

settings[R]
queue[R]

Public Class Methods

configure(settings = {}, &block) click to toggle source
# File lib/backchat/connection_pool.rb, line 45
def configure(settings = {}, &block)
  if block_given?
    instance_eval(&block)
  else
    @settings.merge!(settings)
  end
end
connection(credentials = {}) { |connection| ... } click to toggle source
# File lib/backchat/connection_pool.rb, line 53
def connection(credentials = {})
  connection = XMPPConnection.new(settings[:host])
  connection.connect
  if (credentials.keys & [:username, :password]).count == 2
    connection.login(*credentials.values_at(:username, :password))
  end

  yield connection
ensure
  connection.disconnect
end
new(count = self.class.settings[:pool_size]) click to toggle source
# File lib/backchat/connection_pool.rb, line 14
def initialize(count = self.class.settings[:pool_size])
  @queue = fill(SizedQueue.new(count))
end

Public Instance Methods

add(connection) click to toggle source
# File lib/backchat/connection_pool.rb, line 26
def add(connection)
  queue.push(connection)
end
count() click to toggle source
# File lib/backchat/connection_pool.rb, line 18
def count
  queue.length
end
take() click to toggle source
# File lib/backchat/connection_pool.rb, line 22
def take
  queue.pop
end
with_connection() { |connection| ... } click to toggle source
# File lib/backchat/connection_pool.rb, line 30
def with_connection
  connection = take
  connection.connect unless connection.is_connected?
  connection.login(self.class.settings[:username],
                   self.class.settings[:password],
                   "resource_#{connection.connection_id}") unless connection.is_authenticated?
  yield connection
ensure
  connection.disconnect unless self.class.settings[:persistent]
  add(connection)
end

Private Instance Methods

fill(queue) click to toggle source
# File lib/backchat/connection_pool.rb, line 77
def fill(queue)
  while queue.length < queue.max
    conn = XMPPConnection.new(self.class.settings[:host])
    if self.class.settings[:persistent]
      conn.connect
      conn.login(self.class.settings[:username],
                 self.class.settings[:password],
                 "resource_#{conn.connection_id}")
    end

    queue.push(conn)
  end

  queue
end