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:
parent
05772177f7
commit
117ce2389c
|
@ -857,12 +857,17 @@ module Mongo
|
||||||
# Low-level method for receiving data from socket.
|
# Low-level method for receiving data from socket.
|
||||||
# Requires length and an available socket.
|
# Requires length and an available socket.
|
||||||
def receive_message_on_socket(length, socket)
|
def receive_message_on_socket(length, socket)
|
||||||
message = ""
|
message = new_binary_string
|
||||||
begin
|
begin
|
||||||
while message.length < length do
|
socket.read(length, message)
|
||||||
chunk = socket.recv(length - message.length)
|
raise ConnectionFailure, "connection closed" unless message.length > 0
|
||||||
raise ConnectionFailure, "connection closed" unless chunk.length > 0
|
if message.length < length
|
||||||
message += chunk
|
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
|
end
|
||||||
rescue => ex
|
rescue => ex
|
||||||
close
|
close
|
||||||
|
@ -870,5 +875,17 @@ module Mongo
|
||||||
end
|
end
|
||||||
message
|
message
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue