From 689e89c2e6e7a118046f5bc14aea92e3655e3dfb Mon Sep 17 00:00:00 2001 From: Alexander Polcyn Date: Mon, 19 Sep 2016 18:36:33 -0700 Subject: dont create extra thread on bidi call in benchmark, and change suggested code for ruby bidi calls --- examples/ruby/route_guide/route_guide_server.rb | 65 +++++++++---------------- 1 file changed, 24 insertions(+), 41 deletions(-) (limited to 'examples/ruby') diff --git a/examples/ruby/route_guide/route_guide_server.rb b/examples/ruby/route_guide/route_guide_server.rb index a5a73a8bac..41b9174b1d 100755 --- a/examples/ruby/route_guide/route_guide_server.rb +++ b/examples/ruby/route_guide/route_guide_server.rb @@ -100,28 +100,6 @@ class RectangleEnum end end -# A EnumeratorQueue wraps a Queue to yield the items added to it. -class EnumeratorQueue - extend Forwardable - def_delegators :@q, :push - - def initialize(sentinel) - @q = Queue.new - @sentinel = sentinel - @received_notes = {} - end - - def each_item - return enum_for(:each_item) unless block_given? - loop do - r = @q.pop - break if r.equal?(@sentinel) - fail r if r.is_a? Exception - yield r - end - end -end - # ServerImpl provides an implementation of the RouteGuide service. class ServerImpl < RouteGuide::Service # @param [Hash] feature_db {location => name} @@ -166,28 +144,33 @@ class ServerImpl < RouteGuide::Service end def route_chat(notes) - q = EnumeratorQueue.new(self) - # run a separate thread that processes the incoming requests - t = Thread.new do - begin - notes.each do |n| - key = { - 'latitude' => n.location.latitude, - 'longitude' => n.location.longitude - } - earlier_msgs = @received_notes[key] - @received_notes[key] << n.message - # send back the earlier messages at this point - earlier_msgs.each do |r| - q.push(RouteNote.new(location: n.location, message: r)) - end + RouteChatEnumerator.new(notes, @received_notes).each_item + end +end + +class RouteChatEnumerator + def initialize(notes, received_notes) + @notes = notes + @received_notes = received_notes + end + def each_item + return enum_for(:each_item) unless block_given? + begin + @notes.each do |n| + key = { + 'latitude' => n.location.latitude, + 'longitude' => n.location.longitude + } + earlier_msgs = @received_notes[key] + @received_notes[key] << n.message + # send back the earlier messages at this point + earlier_msgs.each do |r| + yield RouteNote.new(location: n.location, message: r) end - q.push(self) # signal completion - rescue StandardError => e - q.push(e) # signal completion via an error end + rescue StandardError => e + fail e # signal completion via an error end - q.each_item end end -- cgit v1.2.3