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.2') do |p|
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
- socket.write("#{body.size}\n#{body}")
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
- is_ready = IO.select([socket], nil, nil, @response_timeout)
49
- raise NodeTimedOut if is_ready.nil?
50
- response_size = socket.gets
51
- if response_size
52
- socket.read(response_size.strip.to_i)
53
- else
54
- # nil response size
55
- raise LostConnectionToNode
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
@@ -7,9 +7,9 @@ module Pandemic
7
7
  @key, @cluster = key, cluster
8
8
  end
9
9
 
10
- def request(body)
11
- @cluster.request(body, @key)
12
- end
10
+ def request(body, options = {})
11
+ @cluster.request(body, @key, options)
12
+ end
13
13
  end
14
14
  end
15
15
  end
@@ -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! =~ /^([0-9]+)$/ # currently only asking for request size
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
- response = handle_request(body)
45
+ debug("Writing response to client")
40
46
 
41
- debug("Writing response to client")
42
-
43
- # the connection could be closed, we'll let it be rescued if it is.
44
- @connection.write("#{response.size}\n#{response}")
45
- @connection.flush
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{pandemic}
5
- s.version = "0.3.2"
5
+ s.version = "0.3.3"
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.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arya Asemanfar