Skip to content

Commit

Permalink
Merge pull request #213 from localshred/abrandoned/zmq_queue_work
Browse files Browse the repository at this point in the history
allow queue size to be configurable and use an array as local queue as t...
  • Loading branch information
abrandoned committed Aug 26, 2014
2 parents 753b677 + 492f950 commit 2867745
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/protobuf/rpc/connectors/zmq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ def lookup_server_uri
def host_alive?(host)
return true unless ping_port_enabled?
socket = TCPSocket.new(host, ping_port.to_i)
socket.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, 1)
socket.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_LINGER, [1,0].pack('ii'))

true
rescue
Expand Down
14 changes: 9 additions & 5 deletions lib/protobuf/rpc/servers/zmq/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def init_frontend_socket
end

def init_local_queue
@local_queue = ::Queue.new
@local_queue = []
end

def init_poller
Expand All @@ -88,6 +88,10 @@ def inproc?
!!@server.try(:inproc?)
end

def local_queue_max_size
@local_queue_max_size ||= [ENV["PB_ZMQ_SERVER_QUEUE_MAX_SIZE"].to_i, 5].max
end

def process_backend
worker, ignore, *frames = read_from_backend

Expand All @@ -102,16 +106,16 @@ def process_frontend
address, _, message, *frames = read_from_frontend

if message == ::Protobuf::Rpc::Zmq::CHECK_AVAILABLE_MESSAGE
if @idle_workers.any? || local_queue.size < 5 # Should make queue a SizedQueue and allow users to configure queue size
if local_queue.size < local_queue_max_size
write_to_frontend([address, "", ::Protobuf::Rpc::Zmq::WORKERS_AVAILABLE])
else
write_to_frontend([address, "", ::Protobuf::Rpc::Zmq::NO_WORKERS_AVAILABLE])
end
else
if @idle_workers.any?
write_to_backend([@idle_workers.shift, ""] + [address, "", message ] + frames)
else
if @idle_workers.empty?
local_queue.push([address, "", message ] + frames)
else
write_to_backend([@idle_workers.shift, ""] + [address, "", message ] + frames)
end
end
end
Expand Down

0 comments on commit 2867745

Please sign in to comment.