Class: Jacha::ConnectionPool
- Inherits:
-
Object
- Object
- Jacha::ConnectionPool
- Includes:
- Singleton
- Defined in:
- lib/jacha/connection_pool.rb
Instance Attribute Summary collapse
-
#connect_timeout ⇒ Object
Returns the value of attribute connect_timeout.
-
#jid ⇒ Object
Returns the value of attribute jid.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#password ⇒ Object
Returns the value of attribute password.
-
#retry_delay ⇒ Object
Returns the value of attribute retry_delay.
-
#size ⇒ Object
Returns the value of attribute size.
Class Method Summary collapse
Instance Method Summary collapse
- #destroy ⇒ Object
- #fix_charset! ⇒ Object
- #get_connection ⇒ Object
- #pool ⇒ Object
- #respawn ⇒ Object
- #spawn(number = nil) ⇒ Object
Instance Attribute Details
#connect_timeout ⇒ Object
Returns the value of attribute connect_timeout.
5 6 7 |
# File 'lib/jacha/connection_pool.rb', line 5 def connect_timeout @connect_timeout end |
#jid ⇒ Object
Returns the value of attribute jid.
5 6 7 |
# File 'lib/jacha/connection_pool.rb', line 5 def jid @jid end |
#logger ⇒ Object
Returns the value of attribute logger.
5 6 7 |
# File 'lib/jacha/connection_pool.rb', line 5 def logger @logger end |
#password ⇒ Object
Returns the value of attribute password.
5 6 7 |
# File 'lib/jacha/connection_pool.rb', line 5 def password @password end |
#retry_delay ⇒ Object
Returns the value of attribute retry_delay.
5 6 7 |
# File 'lib/jacha/connection_pool.rb', line 5 def retry_delay @retry_delay end |
#size ⇒ Object
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
#destroy ⇒ Object
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_connection ⇒ Object
27 28 29 |
# File 'lib/jacha/connection_pool.rb', line 27 def get_connection pool.sample end |
#pool ⇒ Object
23 24 25 |
# File 'lib/jacha/connection_pool.rb', line 23 def pool @connections ||= [] end |
#respawn ⇒ Object
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 |