|
|
@ -78,11 +78,9 @@ module GRPC |
|
|
|
# @param requests the Enumerable of requests to send |
|
|
|
# @param requests the Enumerable of requests to send |
|
|
|
# @return an Enumerator of requests to yield |
|
|
|
# @return an Enumerator of requests to yield |
|
|
|
def run_on_client(requests, &blk) |
|
|
|
def run_on_client(requests, &blk) |
|
|
|
@enq_th = start_write_loop(requests) |
|
|
|
@enq_th = Thread.new { write_loop(requests) } |
|
|
|
@loop_th = start_read_loop |
|
|
|
@loop_th = start_read_loop |
|
|
|
replies = each_queued_msg |
|
|
|
each_queued_msg(&blk) |
|
|
|
return replies if blk.nil? |
|
|
|
|
|
|
|
replies.each { |r| blk.call(r) } |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
# Begins orchestration of the Bidi stream for a server generating replies. |
|
|
|
# Begins orchestration of the Bidi stream for a server generating replies. |
|
|
@ -98,9 +96,8 @@ module GRPC |
|
|
|
# @param gen_each_reply [Proc] generates the BiDi stream replies. |
|
|
|
# @param gen_each_reply [Proc] generates the BiDi stream replies. |
|
|
|
def run_on_server(gen_each_reply) |
|
|
|
def run_on_server(gen_each_reply) |
|
|
|
replys = gen_each_reply.call(each_queued_msg) |
|
|
|
replys = gen_each_reply.call(each_queued_msg) |
|
|
|
@enq_th = start_write_loop(replys, is_client: false) |
|
|
|
|
|
|
|
@loop_th = start_read_loop |
|
|
|
@loop_th = start_read_loop |
|
|
|
@enq_th.join if @enq_th.alive? |
|
|
|
write_loop(replys, is_client: false) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
private |
|
|
|
private |
|
|
@ -126,37 +123,32 @@ module GRPC |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
# during bidi-streaming, read the requests to send from a separate thread |
|
|
|
def write_loop(requests, is_client: true) |
|
|
|
# read so that read_loop does not block waiting for requests to read. |
|
|
|
GRPC.logger.debug('bidi-write-loop: starting') |
|
|
|
def start_write_loop(requests, is_client: true) |
|
|
|
write_tag = Object.new |
|
|
|
Thread.new do # TODO: run on a thread pool |
|
|
|
count = 0 |
|
|
|
GRPC.logger.debug('bidi-write-loop: starting') |
|
|
|
requests.each do |req| |
|
|
|
begin |
|
|
|
GRPC.logger.debug("bidi-write-loop: #{count}") |
|
|
|
write_tag = Object.new |
|
|
|
count += 1 |
|
|
|
count = 0 |
|
|
|
payload = @marshal.call(req) |
|
|
|
requests.each do |req| |
|
|
|
@call.run_batch(@cq, write_tag, INFINITE_FUTURE, |
|
|
|
GRPC.logger.debug("bidi-write-loop: #{count}") |
|
|
|
SEND_MESSAGE => payload) |
|
|
|
count += 1 |
|
|
|
end |
|
|
|
payload = @marshal.call(req) |
|
|
|
GRPC.logger.debug("bidi-write-loop: #{count} writes done") |
|
|
|
@call.run_batch(@cq, write_tag, INFINITE_FUTURE, |
|
|
|
if is_client |
|
|
|
SEND_MESSAGE => payload) |
|
|
|
GRPC.logger.debug("bidi-write-loop: client sent #{count}, waiting") |
|
|
|
end |
|
|
|
batch_result = @call.run_batch(@cq, write_tag, INFINITE_FUTURE, |
|
|
|
GRPC.logger.debug("bidi-write-loop: #{count} writes done") |
|
|
|
SEND_CLOSE_FROM_CLIENT => nil, |
|
|
|
if is_client |
|
|
|
RECV_STATUS_ON_CLIENT => nil) |
|
|
|
GRPC.logger.debug("bidi-write-loop: client sent #{count}, waiting") |
|
|
|
@call.status = batch_result.status |
|
|
|
@call.run_batch(@cq, write_tag, INFINITE_FUTURE, |
|
|
|
batch_result.check_status |
|
|
|
SEND_CLOSE_FROM_CLIENT => nil) |
|
|
|
GRPC.logger.debug("bidi-write-loop: done status #{@call.status}") |
|
|
|
batch_result = @call.run_batch(@cq, write_tag, INFINITE_FUTURE, |
|
|
|
|
|
|
|
RECV_STATUS_ON_CLIENT => nil) |
|
|
|
|
|
|
|
batch_result.check_status |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
rescue StandardError => e |
|
|
|
|
|
|
|
GRPC.logger.warn('bidi-write-loop: failed') |
|
|
|
|
|
|
|
GRPC.logger.warn(e) |
|
|
|
|
|
|
|
raise e |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
GRPC.logger.debug('bidi-write-loop: finished') |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
GRPC.logger.debug('bidi-write-loop: finished') |
|
|
|
|
|
|
|
rescue StandardError => e |
|
|
|
|
|
|
|
GRPC.logger.warn('bidi-write-loop: failed') |
|
|
|
|
|
|
|
GRPC.logger.warn(e) |
|
|
|
|
|
|
|
raise e |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
# starts the read loop |
|
|
|
# starts the read loop |
|
|
|