Skip to content
This repository has been archived by the owner on Nov 23, 2018. It is now read-only.

Commit

Permalink
Some improvements and testing to net-http adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Kieltyka committed Jul 6, 2012
1 parent fdda03c commit c5fe32d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
39 changes: 28 additions & 11 deletions lib/uber-s3/connection/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,30 @@ def request(verb, url, headers={}, body=nil)
self.uri = URI.parse(url)

# Init and open a HTTP connection
self.http ||= Net::HTTP.new(uri.host, uri.port)
if !http.started? || !http.active?
http.start

if Socket.const_defined?(:TCP_NODELAY)
socket = http.instance_variable_get(:@socket)
socket.io.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true)
end
end
http_connect! if http.nil? || !http.started?

req_klass = instance_eval("Net::HTTP::"+verb.to_s.capitalize)
req = req_klass.new(uri.to_s, headers)

req.body = body if !body.nil? && !body.empty?

# Make HTTP request
r = http.request(req)
retries = 2
begin
r = http.request(req)
rescue EOFError, Errno::EPIPE
# Something happened to our connection, lets try this again
socket = http.instance_variable_get(:@socket)
if socket.io.closed?
$stderr.puts "UberS3 - connection flopped.. socket.io is closed"
http_connect!
end

# $stderr.puts "active? " + http.active?.to_s
$stderr.puts "UberS3 - retrying.. attempts left: #{retries}"

retries -= 1
retry if retries >= 0
end

# Auto-decode any gzipped objects
if verb == :get && r.header['Content-Encoding'] == 'gzip'
Expand All @@ -47,6 +52,18 @@ def request(verb, url, headers={}, body=nil)
:raw => r
})
end

private

def http_connect!
self.http = Net::HTTP.new(uri.host, uri.port)
http.start

if Socket.const_defined?(:TCP_NODELAY)
socket = http.instance_variable_get(:@socket)
socket.io.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true)
end
end

end
end
4 changes: 3 additions & 1 deletion spec/uber-s3/bucket_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require 'spec_helper'

describe UberS3::Bucket do
[:net_http, :em_http_fibered].each do |connection_adapter|
# [:net_http, :em_http_fibered].each do |connection_adapter|
[:net_http].each do |connection_adapter|


context "#{connection_adapter}: Bucket" do
let(:s3) do
Expand Down

0 comments on commit c5fe32d

Please sign in to comment.