rb: Switch from read_nonblock to readpartial to make jruby happy
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@669024 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/rb/benchmark/fairness.rb b/lib/rb/benchmark/fairness.rb
index a032013..11a9f04 100644
--- a/lib/rb/benchmark/fairness.rb
+++ b/lib/rb/benchmark/fairness.rb
@@ -142,7 +142,7 @@
next if rd.nil?
rd.each do |fd|
begin
- @buffers[fd] << fd.read_nonblock(4096)
+ @buffers[fd] << fd.readpartial(4096)
rescue EOFError
@pool.delete fd
end
diff --git a/lib/rb/lib/thrift/server/nonblockingserver.rb b/lib/rb/lib/thrift/server/nonblockingserver.rb
index 054c4d7..7868bab 100644
--- a/lib/rb/lib/thrift/server/nonblockingserver.rb
+++ b/lib/rb/lib/thrift/server/nonblockingserver.rb
@@ -136,12 +136,7 @@
end
def read_connection(fd)
- buffer = ''
- begin
- buffer << fd.read_nonblock(4096) while true
- rescue Errno::EAGAIN, EOFError
- @buffers[fd] << buffer
- end
+ @buffers[fd] << fd.readpartial(1048576)
frame = slice_frame!(@buffers[fd])
if frame
@worker_queue.push [:frame, fd, frame]
@@ -167,13 +162,12 @@
def read_signals
# clear the signal pipe
- begin
- @signal_pipes[0].read_nonblock(1024) while true
- rescue Errno::EAGAIN
- end
+ # note that since read_nonblock is broken in jruby,
+ # we can only read up to a set number of signals at once
+ sigstr = @signal_pipes[0].readpartial(1024)
# now read the signals
begin
- loop do
+ sigstr.length.times do
signal, obj = @signal_queue.pop(true)
case signal
when :connection
@@ -185,6 +179,10 @@
end
rescue ThreadError
# out of signals
+ # note that in a perfect world this would never happen, since we're
+ # only reading the number of signals pushed on the pipe, but given the lack
+ # of locks, in theory we could clear the pipe/queue while a new signal is being
+ # placed on the pipe, at which point our next read_signals would hit this error
end
end
diff --git a/lib/rb/lib/thrift/transport/socket.rb b/lib/rb/lib/thrift/transport/socket.rb
index 3d540e4..8d6f1ed 100644
--- a/lib/rb/lib/thrift/transport/socket.rb
+++ b/lib/rb/lib/thrift/transport/socket.rb
@@ -42,10 +42,10 @@
end
end
- def read(sz, nonblock=false)
+ def read(sz, partial=false)
begin
- if nonblock
- data = @handle.read_nonblock(sz)
+ if partial
+ data = @handle.readpartial(sz)
else
data = @handle.read(sz)
end
@@ -63,7 +63,7 @@
data
end
- def read_nonblock(sz)
+ def readpartial(sz)
read(sz, true)
end