arya-pandemic 0.4.7 → 0.4.8

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('pandemic', '0.4.7') do |p|
5
+ Echoe.new('pandemic', '0.4.8') do |p|
6
6
  p.description = "A framework for distributing work for real-time services and offline tasks."
7
7
  p.url = "https://github.com/arya/pandemic/"
8
8
  p.author = "Arya Asemanfar"
@@ -26,15 +26,11 @@ module Pandemic
26
26
  @connection_proxies[key] = ConnectionProxy.new(key, self)
27
27
  host, port = host_port(server_addr)
28
28
  Config.min_connections_per_server.times do
29
- connection = create_connection(key)
30
- if connection.alive?
31
- @connections << connection
32
- @available << connection
33
- @grouped_connections[key] << connection
34
- @grouped_available[key] << connection
35
- end
29
+ add_connection_for_key(key)
36
30
  end
37
31
  end
32
+
33
+ maintain_minimum_connections!
38
34
  end
39
35
 
40
36
 
@@ -98,7 +94,11 @@ module Pandemic
98
94
  if select_from.size > 0
99
95
  connection = select_from.pop
100
96
  connection.ensure_alive!
101
- break unless connection.alive?
97
+ if !connection.alive?
98
+ # it's dead
99
+ delete_connection(connection)
100
+ next
101
+ end
102
102
 
103
103
  if key.nil?
104
104
  @grouped_available[key].delete(connection)
@@ -149,7 +149,42 @@ module Pandemic
149
149
  Connection.new(host, port, key)
150
150
  end
151
151
 
152
- #TODO: a thread to manage killing and reviving connections
152
+ def add_connection_for_key(key)
153
+ connection = create_connection(key)
154
+ if connection.alive?
155
+ @connections << connection
156
+ @available << connection
157
+ @grouped_connections[key] << connection
158
+ @grouped_available[key] << connection
159
+ end
160
+ end
161
+
162
+ def delete_connection(connection)
163
+ @connections.delete(connection)
164
+ @available.delete(connection)
165
+ @grouped_connections[connection.key].delete(connection)
166
+ @grouped_available[connection.key].delete(connection)
167
+ end
168
+
169
+ def maintain_minimum_connections!
170
+ return if @maintain_minimum_connections_thread
171
+ @maintain_minimum_connections_thread = Thread.new do
172
+ loop do
173
+ sleep 60 #arbitrary
174
+ @mutex.synchronize do
175
+ @grouped_connections.keys.each do |key|
176
+ currently_exist = @grouped_connections[key].size
177
+ if currently_exist < Config.min_connections_per_server
178
+ (Config.min_connections_per_server - currently_exist).times do
179
+ add_connection_for_key(key)
180
+ end
181
+ end
182
+ end
183
+ end
184
+ end
185
+ end
186
+ end
187
+
153
188
  end
154
189
  end
155
190
  end
data/pandemic.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{pandemic}
5
- s.version = "0.4.7"
5
+ s.version = "0.4.8"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Arya Asemanfar"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arya-pandemic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.7
4
+ version: 0.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arya Asemanfar