From 427122c98a671437e44f6c6cb6359ffc2b7cb36a Mon Sep 17 00:00:00 2001 From: Matt Aimonetti Date: Wed, 28 Jan 2009 18:37:45 -0800 Subject: [PATCH] added some monkey patches to improve the http connection speed. (by keeping the http connection open) --- lib/couchrest/monkeypatches.rb | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/lib/couchrest/monkeypatches.rb b/lib/couchrest/monkeypatches.rb index 3147ff3..bad327d 100644 --- a/lib/couchrest/monkeypatches.rb +++ b/lib/couchrest/monkeypatches.rb @@ -23,6 +23,24 @@ class Time # end end +# Monkey patch for faster net/http io +if RUBY_VERSION.to_f < 1.9 + class Net::BufferedIO #:nodoc: + alias :old_rbuf_fill :rbuf_fill + def rbuf_fill + begin + @rbuf << @io.read_nonblock(65536) + rescue Errno::EWOULDBLOCK + if IO.select([@io], nil, nil, @read_timeout) + @rbuf << @io.read_nonblock(65536) + else + raise Timeout::TimeoutError + end + end + end + end +end + module RestClient def self.copy(url, headers={}) Request.execute(:method => :copy, @@ -35,4 +53,47 @@ module RestClient :url => url, :headers => headers) end + + class Request + def transmit(uri, req, payload) + setup_credentials(req) + + Thread.current[:host] ||= uri.host + Thread.current[:port] ||= uri.port + + net = net_http_class.new(uri.host, uri.port) + + if Thread.current[:connection].nil? || Thread.current[:host] != uri.host + Thread.current[:connection].finish if (Thread.current[:connection] && Thread.current[:connection].started?) + net.use_ssl = uri.is_a?(URI::HTTPS) + net.verify_mode = OpenSSL::SSL::VERIFY_NONE + Thread.current[:connection] = net + Thread.current[:connection].start + end + + display_log request_log + http = Thread.current[:connection] + + http.read_timeout = @timeout if @timeout + begin + res = http.request(req, payload) + rescue + # p "Net::HTTP connection failed, reconnecting" + Thread.current[:connection].finish + http = Thread.current[:connection] = net + Thread.current[:connection].start + res = http.request(req, payload) + display_log response_log(res) + process_result res + else + display_log response_log(res) + process_result res + end + + rescue EOFError + raise RestClient::ServerBrokeConnection + rescue Timeout::Error + raise RestClient::RequestTimeout + end + end end \ No newline at end of file