Skip to content

Commit

Permalink
RUBY-236 Create timeout block only when timeout is set
Browse files Browse the repository at this point in the history
  • Loading branch information
banker committed Mar 29, 2011
1 parent 025e567 commit f317455
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions lib/mongo/connection.rb
Expand Up @@ -837,20 +837,14 @@ def send_message_on_socket(packed_message, socket)
# Requires length and an available socket.
def receive_message_on_socket(length, socket)
begin
message = new_binary_string
Mongo::TimeoutHandler.timeout(@op_timeout, OperationTimeout) do
socket.read(length, message)
raise ConnectionFailure, "connection closed" unless message && message.length > 0
if message.length < length
chunk = new_binary_string
while message.length < length
socket.read(length - message.length, chunk)
raise ConnectionFailure, "connection closed" unless chunk.length > 0
message << chunk
end
if @op_timeout
Mongo::TimeoutHandler.timeout(@op_timeout, OperationTimeout) do
message = receive_data(length, socket)
end
else
message = receive_data(length, socket)
end
rescue => ex
rescue => ex
close

if ex.class == OperationTimeout
Expand All @@ -862,6 +856,21 @@ def receive_message_on_socket(length, socket)
message
end

def receive_data(length, socket)
message = new_binary_string
socket.read(length, message)
raise ConnectionFailure, "connection closed" unless message && message.length > 0
if message.length < length
chunk = new_binary_string
while message.length < length
socket.read(length - message.length, chunk)
raise ConnectionFailure, "connection closed" unless chunk.length > 0
message << chunk
end
end
message
end

if defined?(Encoding)
BINARY_ENCODING = Encoding.find("binary")

Expand Down

0 comments on commit f317455

Please sign in to comment.