jacha 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/jacha/connection.rb +19 -16
- data/lib/jacha/connection_pool.rb +20 -12
- data/lib/jacha/version.rb +1 -1
- metadata +2 -2
data/lib/jacha/connection.rb
CHANGED
@@ -3,36 +3,34 @@ require 'xmpp4r'
|
|
3
3
|
module Jacha
|
4
4
|
class Connection
|
5
5
|
|
6
|
-
attr_reader :jabber
|
7
|
-
attr_accessor :pool
|
6
|
+
attr_reader :jabber, :pool
|
8
7
|
|
9
|
-
def initialize(jid, password)
|
8
|
+
def initialize(jid, password, pool=nil)
|
10
9
|
@password = password
|
11
10
|
@jabber = Jabber::Client.new "#{jid}/#{Time.now.to_f}"
|
12
|
-
@
|
11
|
+
@pool = pool
|
12
|
+
@jabber.on_exception do |ex, stream, place|
|
13
13
|
unless broken?
|
14
14
|
broken!
|
15
|
-
logger.warn "#{Time.now}: broken XmppConnection: #{self}"
|
15
|
+
logger.warn "#{Time.now}: broken XmppConnection: #{self}: #{ex} at #{place}"
|
16
16
|
destroy
|
17
|
-
pool.respawn
|
17
|
+
@pool.respawn if @pool
|
18
18
|
end
|
19
19
|
end
|
20
|
-
|
20
|
+
end
|
21
|
+
|
22
|
+
def connect!
|
23
|
+
jabber_connect!
|
21
24
|
@pinger = Thread.new do
|
22
25
|
while true
|
23
26
|
if connected?
|
24
27
|
sleep 180
|
25
28
|
online!
|
26
29
|
else
|
27
|
-
|
30
|
+
jabber_connect!
|
28
31
|
end
|
29
32
|
end
|
30
33
|
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def connect!
|
34
|
-
@jabber.connect
|
35
|
-
@jabber.auth @password
|
36
34
|
online!
|
37
35
|
end
|
38
36
|
|
@@ -77,8 +75,8 @@ module Jacha
|
|
77
75
|
|
78
76
|
def destroy
|
79
77
|
broken!
|
80
|
-
@pinger.kill
|
81
|
-
@jabber.close
|
78
|
+
@pinger.kill if @pinger
|
79
|
+
@jabber.close if @jabber
|
82
80
|
end
|
83
81
|
|
84
82
|
def broken!
|
@@ -90,7 +88,12 @@ module Jacha
|
|
90
88
|
end
|
91
89
|
|
92
90
|
def logger
|
93
|
-
pool.logger
|
91
|
+
@pool && @pool.logger || (@logger ||= Logger.new(STDOUT))
|
92
|
+
end
|
93
|
+
|
94
|
+
def jabber_connect!
|
95
|
+
@jabber.connect
|
96
|
+
@jabber.auth @password
|
94
97
|
end
|
95
98
|
end
|
96
99
|
end
|
@@ -2,11 +2,7 @@ module Jacha
|
|
2
2
|
class ConnectionPool
|
3
3
|
include Singleton
|
4
4
|
|
5
|
-
attr_accessor :jid, :password, :size, :logger
|
6
|
-
|
7
|
-
def pool
|
8
|
-
@connections ||= []
|
9
|
-
end
|
5
|
+
attr_accessor :jid, :password, :size, :logger, :retry_delay, :connect_timeout
|
10
6
|
|
11
7
|
def size
|
12
8
|
@size ||= 3
|
@@ -16,6 +12,18 @@ module Jacha
|
|
16
12
|
@logger ||= Logger.new(STDOUT)
|
17
13
|
end
|
18
14
|
|
15
|
+
def retry_delay
|
16
|
+
@retry_delay ||= 7
|
17
|
+
end
|
18
|
+
|
19
|
+
def connect_timeout
|
20
|
+
@connect_timeout ||= 7
|
21
|
+
end
|
22
|
+
|
23
|
+
def pool
|
24
|
+
@connections ||= []
|
25
|
+
end
|
26
|
+
|
19
27
|
def get_connection
|
20
28
|
pool.sample
|
21
29
|
end
|
@@ -25,22 +33,22 @@ module Jacha
|
|
25
33
|
logger.warn "#{Time.now}: Spawning XmppConnection"
|
26
34
|
spawner = Thread.new do
|
27
35
|
begin
|
28
|
-
connection = Connection.new @jid, @password
|
36
|
+
connection = Connection.new @jid, @password, self
|
37
|
+
connection.connect!
|
29
38
|
spawner[:connection] = connection
|
30
39
|
rescue => ex
|
31
40
|
logger.warn "#{Time.now}: Error on XmppConnection spawn: #{ex}"
|
32
41
|
end
|
33
42
|
end
|
34
|
-
spawner.join
|
43
|
+
spawner.join connect_timeout
|
35
44
|
connection = spawner[:connection]
|
36
45
|
spawner.kill
|
37
46
|
if connection && connection.connected?
|
38
|
-
|
39
|
-
pool << connection
|
47
|
+
pool.push connection
|
40
48
|
logger.warn "#{Time.now}: XmppConnection spawned: #{connection}"
|
41
49
|
else
|
42
|
-
logger.warn "#{Time.now}: XmppConnection spawn failed. Retrying in
|
43
|
-
sleep
|
50
|
+
logger.warn "#{Time.now}: XmppConnection spawn failed. Retrying in #{retry_delay} seconds."
|
51
|
+
sleep retry_delay
|
44
52
|
spawn 1
|
45
53
|
end
|
46
54
|
end
|
@@ -48,7 +56,7 @@ module Jacha
|
|
48
56
|
|
49
57
|
def respawn
|
50
58
|
pool.delete_if &:broken?
|
51
|
-
spawn
|
59
|
+
spawn size - pool.size
|
52
60
|
end
|
53
61
|
|
54
62
|
def destroy
|
data/lib/jacha/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jacha
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-06-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: xmpp4r
|