protobuf 2.6.6-java → 2.7.0-java
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/bin/rpc_server +0 -1
- data/lib/protobuf/rpc/connectors/socket.rb +0 -2
- data/lib/protobuf/rpc/servers/zmq/broker.rb +44 -19
- data/lib/protobuf/rpc/servers/zmq/util.rb +2 -0
- data/lib/protobuf/rpc/servers/zmq/worker.rb +17 -4
- data/lib/protobuf/version.rb +1 -1
- data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +7 -16
- metadata +1 -1
data/bin/rpc_server
CHANGED
@@ -6,29 +6,38 @@ module Protobuf
|
|
6
6
|
module Zmq
|
7
7
|
class Broker
|
8
8
|
include ::Protobuf::Rpc::Zmq::Util
|
9
|
-
attr_reader :frontend, :backend, :poller, :context
|
9
|
+
attr_reader :frontend, :backend, :poller, :context, :available_workers
|
10
10
|
|
11
11
|
##
|
12
12
|
# Constructor
|
13
13
|
#
|
14
14
|
def initialize(options = {})
|
15
|
+
@available_workers = []
|
15
16
|
@context = ::ZMQ::Context.new
|
16
17
|
@frontend = setup_frontend(options)
|
17
18
|
@backend = setup_backend(options)
|
18
|
-
@poller =
|
19
|
+
@poller = ::ZMQ::Poller.new
|
20
|
+
@poller.register(frontend, ::ZMQ::POLLIN)
|
21
|
+
@poller.register(backend, ::ZMQ::POLLIN)
|
19
22
|
end
|
20
23
|
|
21
24
|
##
|
22
25
|
# Instance Methods
|
23
26
|
#
|
24
27
|
def poll
|
28
|
+
if available_workers.size > 0
|
29
|
+
poller.register(frontend, ::ZMQ::POLLIN) if poller.size < 2
|
30
|
+
else
|
31
|
+
poller.delete(frontend)
|
32
|
+
end
|
33
|
+
|
25
34
|
poller.poll(1000)
|
26
35
|
poller.readables.each do |socket|
|
27
36
|
case socket
|
28
37
|
when frontend then
|
29
|
-
|
38
|
+
move_to_backend(socket)
|
30
39
|
when backend then
|
31
|
-
|
40
|
+
move_to_frontend(socket)
|
32
41
|
end
|
33
42
|
end
|
34
43
|
end
|
@@ -41,14 +50,37 @@ module Protobuf
|
|
41
50
|
|
42
51
|
private
|
43
52
|
|
44
|
-
def
|
45
|
-
|
53
|
+
def move_to_backend(socket)
|
54
|
+
message_array = []
|
55
|
+
zmq_error_check(socket.recv_strings(message_array))
|
56
|
+
|
57
|
+
backend_message_set = [
|
58
|
+
available_workers.shift, # Worker UUID for router
|
59
|
+
"",
|
60
|
+
message_array[0], # Client UUID for return value
|
61
|
+
"",
|
62
|
+
message_array[2] # Client Message payload (request)
|
63
|
+
]
|
64
|
+
|
65
|
+
zmq_error_check(backend.send_strings(backend_message_set))
|
66
|
+
end
|
67
|
+
|
68
|
+
def move_to_frontend(socket)
|
69
|
+
message_array = []
|
70
|
+
zmq_error_check(socket.recv_strings(message_array))
|
71
|
+
|
72
|
+
# Push UUID of socket on the available workers queue
|
73
|
+
available_workers << message_array[0]
|
74
|
+
|
75
|
+
# messages should be [ "uuid of socket", "", "READY_MESSAGE || uuid of client socket"]
|
76
|
+
unless message_array[2] == ::Protobuf::Rpc::Zmq::WORKER_READY_MESSAGE
|
77
|
+
frontend_message_set = [
|
78
|
+
message_array[2], # client UUID
|
79
|
+
"",
|
80
|
+
message_array[4] # Reply payload
|
81
|
+
]
|
46
82
|
|
47
|
-
|
48
|
-
socket.recv_string(data = "")
|
49
|
-
more_data = socket.more_parts?
|
50
|
-
more_data_flag = (more_data ? ::ZMQ::SNDMORE : 0)
|
51
|
-
frontend_or_backend.send_string(data, more_data_flag)
|
83
|
+
zmq_error_check(frontend.send_strings(frontend_message_set))
|
52
84
|
end
|
53
85
|
end
|
54
86
|
|
@@ -57,7 +89,7 @@ module Protobuf
|
|
57
89
|
host = dealer_options[:host]
|
58
90
|
port = dealer_options[:port]
|
59
91
|
|
60
|
-
zmq_backend = context.socket(::ZMQ::
|
92
|
+
zmq_backend = context.socket(::ZMQ::ROUTER)
|
61
93
|
zmq_error_check(zmq_backend.bind(bind_address(host, port)))
|
62
94
|
zmq_backend
|
63
95
|
end
|
@@ -74,13 +106,6 @@ module Protobuf
|
|
74
106
|
def bind_address(host, port)
|
75
107
|
"tcp://#{resolve_ip(host)}:#{port}"
|
76
108
|
end
|
77
|
-
|
78
|
-
def setup_poller
|
79
|
-
zmq_poller = ::ZMQ::Poller.new
|
80
|
-
zmq_poller.register(frontend, ::ZMQ::POLLIN)
|
81
|
-
zmq_poller.register(backend, ::ZMQ::POLLIN)
|
82
|
-
zmq_poller
|
83
|
-
end
|
84
109
|
end
|
85
110
|
end
|
86
111
|
end
|
@@ -16,19 +16,25 @@ module Protobuf
|
|
16
16
|
port = options[:port]
|
17
17
|
|
18
18
|
@zmq_context = ::ZMQ::Context.new
|
19
|
-
@socket = @zmq_context.socket(::ZMQ::
|
19
|
+
@socket = @zmq_context.socket(::ZMQ::REQ)
|
20
20
|
zmq_error_check(@socket.connect("tcp://#{resolve_ip(host)}:#{port}"))
|
21
21
|
|
22
22
|
@poller = ::ZMQ::Poller.new
|
23
23
|
@poller.register(@socket, ::ZMQ::POLLIN)
|
24
|
+
|
25
|
+
# Send request to broker telling it we are ready
|
26
|
+
zmq_error_check(@socket.send_string(::Protobuf::Rpc::Zmq::WORKER_READY_MESSAGE))
|
24
27
|
end
|
25
28
|
|
26
29
|
##
|
27
30
|
# Instance Methods
|
28
31
|
#
|
29
32
|
def handle_request(socket)
|
30
|
-
|
31
|
-
zmq_error_check(socket.
|
33
|
+
message_array = []
|
34
|
+
zmq_error_check(socket.recv_strings(message_array))
|
35
|
+
|
36
|
+
@request_data = message_array[2]
|
37
|
+
@client_address = message_array[0]
|
32
38
|
log_debug { sign_message("handling request") } unless @request_data.nil?
|
33
39
|
end
|
34
40
|
|
@@ -50,8 +56,15 @@ module Protobuf
|
|
50
56
|
|
51
57
|
def send_data
|
52
58
|
response_data = @response.to_s # to_s is aliases as serialize_to_string in Message
|
59
|
+
|
60
|
+
response_message_set = [
|
61
|
+
@client_address, # client uuid address
|
62
|
+
"",
|
63
|
+
response_data
|
64
|
+
]
|
65
|
+
|
53
66
|
@stats.response_size = response_data.size
|
54
|
-
zmq_error_check(@socket.
|
67
|
+
zmq_error_check(@socket.send_strings(response_message_set))
|
55
68
|
end
|
56
69
|
end
|
57
70
|
|
data/lib/protobuf/version.rb
CHANGED
@@ -3,29 +3,20 @@ require 'spec_helper'
|
|
3
3
|
describe ::Protobuf::Rpc::Zmq::Worker do
|
4
4
|
before(:each) do
|
5
5
|
load 'protobuf/zmq.rb'
|
6
|
-
end
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
fake_socket = double
|
8
|
+
fake_socket.should_receive(:connect).and_return(0)
|
9
|
+
fake_socket.should_receive(:send_string).and_return(0)
|
10
|
+
|
11
|
+
fake_context = double
|
12
|
+
fake_context.should_receive(:socket).and_return( fake_socket )
|
13
|
+
::ZMQ::Context.should_receive(:new).and_return( fake_context )
|
11
14
|
end
|
12
15
|
|
13
16
|
subject do
|
14
17
|
described_class.new({ :host => '127.0.0.1', :port => 9400 })
|
15
18
|
end
|
16
19
|
|
17
|
-
it 'sets the context' do
|
18
|
-
subject.instance_variable_get(:@zmq_context).should be_a(::ZMQ::Context)
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'sets the poller' do
|
22
|
-
subject.instance_variable_get(:@socket).should be_a(::ZMQ::Socket)
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'sets the socket' do
|
26
|
-
subject.instance_variable_get(:@poller).should be_a(::ZMQ::Poller)
|
27
|
-
end
|
28
|
-
|
29
20
|
describe '#run' do
|
30
21
|
# not tested via unit tests
|
31
22
|
end
|