Optimize receive_message_on_socket for the optimistic average case where 1 socket.read() operation receives all requested data. Also fix some Ruby 1.9 encoding issues in that function while we're at it.

This commit is contained in:
Hongli Lai (Phusion) 2010-09-11 12:22:51 +02:00 committed by Kyle Banker
parent 05772177f7
commit 117ce2389c
1 changed files with 22 additions and 5 deletions

View File

@ -857,12 +857,17 @@ module Mongo
# Low-level method for receiving data from socket.
# Requires length and an available socket.
def receive_message_on_socket(length, socket)
message = ""
message = new_binary_string
begin
while message.length < length do
chunk = socket.recv(length - message.length)
raise ConnectionFailure, "connection closed" unless chunk.length > 0
message += chunk
socket.read(length, message)
raise ConnectionFailure, "connection closed" unless 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
rescue => ex
close
@ -870,5 +875,17 @@ module Mongo
end
message
end
if defined?(Encoding)
BINARY_ENCODING = Encoding.find("binary")
def new_binary_string
"".force_encoding(BINARY_ENCODING)
end
else
def new_binary_string
""
end
end
end
end