Class: Jacha::ConnectionPool

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/jacha/connection_pool.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connect_timeoutObject

Returns the value of attribute connect_timeout.



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

def connect_timeout
  @connect_timeout
end

#jidObject

Returns the value of attribute jid.



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

def jid
  @jid
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#retry_delayObject

Returns the value of attribute retry_delay.



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

def retry_delay
  @retry_delay
end

#sizeObject

Returns the value of attribute size.



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

def size
  @size
end

Class Method Details

.method_missing(sym, *args, &block) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/jacha/connection_pool.rb', line 71

def self.method_missing(sym, *args, &block)
  if instance.respond_to? sym
    instance.send(sym, *args, &block)
  else
    super
  end
end

Instance Method Details

#destroyObject



62
63
64
65
# File 'lib/jacha/connection_pool.rb', line 62

def destroy
  pool.map &:destroy
  pool.clear
end

#fix_charset!Object



67
68
69
# File 'lib/jacha/connection_pool.rb', line 67

def fix_charset!
  require_relative '../xmpp4r_monkeypatch'
end

#get_connectionObject



27
28
29
# File 'lib/jacha/connection_pool.rb', line 27

def get_connection
  pool.sample
end

#poolObject



23
24
25
# File 'lib/jacha/connection_pool.rb', line 23

def pool
  @connections ||= []
end

#respawnObject



57
58
59
60
# File 'lib/jacha/connection_pool.rb', line 57

def respawn
  pool.delete_if &:broken?
  spawn size - pool.size
end

#spawn(number = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jacha/connection_pool.rb', line 31

def spawn(number=nil)
  (number || size).times do
    logger.warn "#{Time.now}: Spawning XmppConnection"
    spawner = Thread.new do
      begin
        connection = Connection.new @jid, @password, self
        connection.connect!
        spawner[:connection] = connection
      rescue => ex
        logger.warn "#{Time.now}: Error on XmppConnection spawn: #{ex}"
      end
    end
    spawner.join connect_timeout
    connection = spawner[:connection]
    spawner.kill
    if connection && connection.connected?
      pool.push connection
      logger.warn "#{Time.now}: XmppConnection spawned: #{connection}"
    else
      logger.warn "#{Time.now}: XmppConnection spawn failed. Retrying in #{retry_delay} seconds."
      sleep retry_delay
      spawn 1
    end
  end
end