Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce memory usage when reading response body #451

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/http/connection.rb
Expand Up @@ -35,6 +35,7 @@ def initialize(req, options)
@pending_request = false
@pending_response = false
@failed_proxy_connect = false
@buffer = "".b

@parser = Response::Parser.new

Expand Down Expand Up @@ -210,7 +211,7 @@ def set_keep_alive
def read_more(size)
return if @parser.finished?

value = @socket.readpartial(size)
value = @socket.readpartial(size, @buffer)
if value == :eof
@parser << ""
:eof
Expand Down
12 changes: 6 additions & 6 deletions lib/http/timeout/global.rb
Expand Up @@ -46,8 +46,8 @@ def connect_ssl
end

# Read from the socket
def readpartial(size)
perform_io { read_nonblock(size) }
def readpartial(size, buffer = nil)
perform_io { read_nonblock(size, buffer) }
end

# Write to the socket
Expand All @@ -60,16 +60,16 @@ def write(data)
private

if RUBY_VERSION < "2.1.0"
def read_nonblock(size)
@socket.read_nonblock(size)
def read_nonblock(size, buffer = nil)
@socket.read_nonblock(size, buffer)
end

def write_nonblock(data)
@socket.write_nonblock(data)
end
else
def read_nonblock(size)
@socket.read_nonblock(size, :exception => false)
def read_nonblock(size, buffer = nil)
@socket.read_nonblock(size, buffer, :exception => false)
end

def write_nonblock(data)
Expand Down
4 changes: 2 additions & 2 deletions lib/http/timeout/null.rb
Expand Up @@ -41,8 +41,8 @@ def start_tls(host, ssl_socket_class, ssl_context)
end

# Read from the socket
def readpartial(size)
@socket.readpartial(size)
def readpartial(size, buffer = nil)
@socket.readpartial(size, buffer)
rescue EOFError
:eof
end
Expand Down
8 changes: 4 additions & 4 deletions lib/http/timeout/per_operation.rb
Expand Up @@ -37,9 +37,9 @@ def connect_ssl
# NIO with exceptions
if RUBY_VERSION < "2.1.0"
# Read data from the socket
def readpartial(size)
def readpartial(size, buffer = nil)
rescue_readable do
@socket.read_nonblock(size)
@socket.read_nonblock(size, buffer)
end
rescue EOFError
:eof
Expand All @@ -57,10 +57,10 @@ def write(data)
# NIO without exceptions
else
# Read data from the socket
def readpartial(size)
def readpartial(size, buffer = nil)
timeout = false
loop do
result = @socket.read_nonblock(size, :exception => false)
result = @socket.read_nonblock(size, buffer, :exception => false)

return :eof if result.nil?
return result if result != :wait_readable
Expand Down