arya-pandemic 0.3.2 → 0.3.3
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.3.
|
5
|
+
Echoe.new('pandemic', '0.3.3') do |p|
|
6
6
|
p.description = "Distribute MapReduce to any of the workers and it will spread, like a pandemic."
|
7
7
|
p.url = "https://github.com/arya/pandemic/"
|
8
8
|
p.author = "Arya Asemanfar"
|
@@ -40,19 +40,28 @@ module Pandemic
|
|
40
40
|
@connection_proxies[key]
|
41
41
|
end
|
42
42
|
|
43
|
-
def request(body, key = nil)
|
43
|
+
def request(body, key = nil, options = {})
|
44
44
|
with_connection(key) do |socket|
|
45
45
|
begin
|
46
|
-
|
46
|
+
flags = []
|
47
|
+
if options[:async]
|
48
|
+
flags << "a"
|
49
|
+
end
|
50
|
+
flags = flags.empty? ? "" : " #{flags.join("")}"
|
51
|
+
|
52
|
+
socket.write("#{body.size}#{flags}\n#{body}")
|
47
53
|
socket.flush
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
socket.
|
53
|
-
|
54
|
-
|
55
|
-
|
54
|
+
|
55
|
+
unless options[:async]
|
56
|
+
is_ready = IO.select([socket], nil, nil, @response_timeout)
|
57
|
+
raise NodeTimedOut if is_ready.nil?
|
58
|
+
response_size = socket.gets
|
59
|
+
if response_size
|
60
|
+
socket.read(response_size.strip.to_i)
|
61
|
+
else
|
62
|
+
# nil response size
|
63
|
+
raise LostConnectionToNode
|
64
|
+
end
|
56
65
|
end
|
57
66
|
rescue Errno::ECONNRESET
|
58
67
|
raise LostConnectionToNode
|
@@ -1,6 +1,8 @@
|
|
1
1
|
module Pandemic
|
2
2
|
module ServerSide
|
3
3
|
class Client
|
4
|
+
REQUEST_FLAGS = {:async => 'a'}
|
5
|
+
REQUEST_REGEXP = /^([0-9]+)(?: ([#{REQUEST_FLAGS.values.join('')}]*))?$/
|
4
6
|
class DisconnectClient < Exception; end
|
5
7
|
include Util
|
6
8
|
|
@@ -30,21 +32,24 @@ module Pandemic
|
|
30
32
|
@connection.close
|
31
33
|
@connection = nil
|
32
34
|
break
|
33
|
-
elsif request.strip! =~
|
34
|
-
size = $1.to_i
|
35
|
+
elsif request.strip! =~ REQUEST_REGEXP
|
36
|
+
size, flags = $1.to_i, $2.to_s.split("")
|
35
37
|
debug("Reading request body (size #{size})")
|
36
38
|
body = @connection.read(size)
|
37
39
|
debug("Finished reading request body")
|
40
|
+
if flags.include?(REQUEST_FLAGS[:async])
|
41
|
+
Thread.new { handle_request(body) }
|
42
|
+
else
|
43
|
+
response = handle_request(body)
|
38
44
|
|
39
|
-
|
45
|
+
debug("Writing response to client")
|
40
46
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
47
|
+
# the connection could be closed, we'll let it be rescued if it is.
|
48
|
+
@connection.write("#{response.size}\n#{response}")
|
49
|
+
@connection.flush
|
50
|
+
debug("Finished writing response to client")
|
51
|
+
end
|
46
52
|
@responded_requests += 1
|
47
|
-
debug("Finished writing response to client")
|
48
53
|
end
|
49
54
|
end
|
50
55
|
rescue DisconnectClient
|
data/pandemic.gemspec
CHANGED