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

#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

#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



63
64
65
66
67
68
69
# File 'lib/jacha/connection_pool.rb', line 63

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



54
55
56
57
# File 'lib/jacha/connection_pool.rb', line 54

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

#fix_charset!Object



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

def fix_charset!
  require_relative '../xmpp4r_monkeypatch'
end

#get_connectionObject



19
20
21
# File 'lib/jacha/connection_pool.rb', line 19

def get_connection
  pool.sample
end

#poolObject



7
8
9
# File 'lib/jacha/connection_pool.rb', line 7

def pool
  @connections ||= []
end

#respawnObject



49
50
51
52
# File 'lib/jacha/connection_pool.rb', line 49

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

#spawn(number = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/jacha/connection_pool.rb', line 23

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