From bd7c46a7aa8b4f44ef683e22f469033b96d3dd5f Mon Sep 17 00:00:00 2001 From: naruse Date: Wed, 6 Jun 2018 08:03:47 +0000 Subject: Introduce write_timeout to Net::HTTP [Feature #13396] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63587 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/net/http.rb | 17 ++++++++++++++++- lib/net/protocol.rb | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/net/http.rb b/lib/net/http.rb index 2d575aeecc..bfd0001422 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -575,7 +575,7 @@ module Net #:nodoc: # # _opt_ sets following values by its accessor. # The keys are ca_file, ca_path, cert, cert_store, ciphers, - # close_on_empty_response, key, open_timeout, read_timeout, ssl_timeout, + # close_on_empty_response, key, open_timeout, read_timeout, write_timeout, ssl_timeout, # ssl_version, use_ssl, verify_callback, verify_depth and verify_mode. # If you set :use_ssl as true, you can use https and default value of # verify_mode is set as OpenSSL::SSL::VERIFY_PEER. @@ -673,6 +673,7 @@ module Net #:nodoc: @started = false @open_timeout = 60 @read_timeout = 60 + @write_timeout = 60 @continue_timeout = nil @max_retries = 1 @debug_output = nil @@ -741,6 +742,12 @@ module Net #:nodoc: # it raises a Net::ReadTimeout exception. The default value is 60 seconds. attr_reader :read_timeout + # Number of seconds to wait for one block to be write (via one write(2) + # call). Any number may be used, including Floats for fractional + # seconds. If the HTTP object cannot write data in this many seconds, + # it raises a Net::WriteTimeout exception. The default value is 60 seconds. + attr_reader :write_timeout + # Maximum number of times to retry an idempotent request in case of # Net::ReadTimeout, IOError, EOFError, Errno::ECONNRESET, # Errno::ECONNABORTED, Errno::EPIPE, OpenSSL::SSL::SSLError, @@ -763,6 +770,12 @@ module Net #:nodoc: @read_timeout = sec end + # Setter for the write_timeout attribute. + def write_timeout=(sec) + @socket.write_timeout = sec if @socket + @write_timeout = sec + end + # Seconds to wait for 100 Continue response. If the HTTP object does not # receive a response in this many seconds it sends the request body. The # default value is +nil+. @@ -944,6 +957,7 @@ module Net #:nodoc: if use_ssl? if proxy? plain_sock = BufferedIO.new(s, read_timeout: @read_timeout, + write_timeout: @write_timeout, continue_timeout: @continue_timeout, debug_output: @debug_output) buf = "CONNECT #{@address}:#{@port} HTTP/#{HTTPVersion}\r\n" @@ -985,6 +999,7 @@ module Net #:nodoc: D "SSL established" end @socket = BufferedIO.new(s, read_timeout: @read_timeout, + write_timeout: @write_timeout, continue_timeout: @continue_timeout, debug_output: @debug_output) on_connect diff --git a/lib/net/protocol.rb b/lib/net/protocol.rb index 7ec636b384..cf9fc3d434 100644 --- a/lib/net/protocol.rb +++ b/lib/net/protocol.rb @@ -77,11 +77,18 @@ module Net # :nodoc: class ReadTimeout < Timeout::Error; end + ## + # WriteTimeout, a subclass of Timeout::Error, is raised if a chunk of the + # response cannot be read within the read_timeout. + + class WriteTimeout < Timeout::Error; end + class BufferedIO #:nodoc: internal use only - def initialize(io, read_timeout: 60, continue_timeout: nil, debug_output: nil) + def initialize(io, read_timeout: 60, write_timeout: 60, continue_timeout: nil, debug_output: nil) @io = io @read_timeout = read_timeout + @write_timeout = write_timeout @continue_timeout = continue_timeout @debug_output = debug_output @rbuf = ''.b @@ -89,6 +96,7 @@ module Net # :nodoc: attr_reader :io attr_accessor :read_timeout + attr_accessor :write_timeout attr_accessor :continue_timeout attr_accessor :debug_output @@ -237,9 +245,32 @@ module Net # :nodoc: def write0(*strs) @debug_output << strs.map(&:dump).join if @debug_output - len = @io.write(*strs) - @written_bytes += len - len + case len = @io.write_nonblock(*strs, exception: false) + when Integer + orig_len = len + strs.each_with_index do |str, i| + @written_bytes += str.bytesize + len -= str.bytesize + if len == 0 + if strs.size == i+1 + return orig_len + else + strs = strs[i+1..] # rest + break + end + elsif len < 0 + strs = strs[i..] # str and rest + strs[0] = str[len, -len] + break + else # len > 0 + # next + end + end + # continue looping + when :wait_writable + @io.to_io.wait_writable(@write_timeout) or raise Net::WriteTimeout + # continue looping + end while true end # -- cgit v1.2.3