about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-12-11 22:25:12 +0000
committerEric Wong <normalperson@yhbt.net>2011-12-11 22:28:34 +0000
commit8a54653184536b99bf74bcaedb8cf84ea0f4f693 (patch)
treea94cb4df7c057d706869533f1f578fcbe7dd8354
parentb1e3b9f570c04100f73f63ceaff6ab07f938476a (diff)
downloadmogilefs-client-8a54653184536b99bf74bcaedb8cf84ea0f4f693.tar.gz
Performance with Content-Range uploads sucks either way, so it's
not noticeably worse off on a LAN /without/ persistent
connections.
-rw-r--r--lib/mogilefs/new_file/content_range.rb31
1 files changed, 19 insertions, 12 deletions
diff --git a/lib/mogilefs/new_file/content_range.rb b/lib/mogilefs/new_file/content_range.rb
index 5893980..8dd4725 100644
--- a/lib/mogilefs/new_file/content_range.rb
+++ b/lib/mogilefs/new_file/content_range.rb
@@ -1,22 +1,29 @@
 # -*- encoding: binary -*-
 # here are internal implementation details, do not rely on them in your code
-begin
-  require 'net/http/persistent'
-rescue LoadError
-  raise LoadError,
-        'net-http-persistent required for :largefile => :content_range', []
-end
-
+require 'net/http'
 require 'mogilefs/new_file/writer'
 
 # an IO-like object
 class MogileFS::NewFile::ContentRange
   include MogileFS::NewFile::Writer
   include MogileFS::NewFile::Common
-
-  NHP = Net::HTTP::Persistent.new('mogilefs')
   attr_reader :md5
 
+  # :stopdoc:
+  begin
+    require 'net/http/persistent'
+    NHP = Net::HTTP::Persistent.new('mogilefs')
+
+    def hit(uri, req)
+      NHP.request(uri, req).value
+    end
+  rescue LoadError
+    def hit(uri, req)
+      Net::HTTP.start(uri.host, uri.port) { |h| h.request(req).value }
+    end
+  end
+  # :startdoc:
+
   def initialize(dests, opts) # :nodoc:
     @dests = dests
     @opts = opts
@@ -59,7 +66,7 @@ class MogileFS::NewFile::ContentRange
     devid, uri = get_dest
     put = request_for(uri, buf)
     begin
-      NHP.request(uri, put).value # raises on error
+      hit(uri, put) # raises on error
     rescue => e
       raise if @bytes_uploaded > 0
 
@@ -87,7 +94,7 @@ class MogileFS::NewFile::ContentRange
     @devid, @uri = get_dest
     put = request_for(@uri, "")
     begin
-      NHP.request(@uri, put).value # raises on error
+      hit(@uri, put) # raises on error
     rescue => e
       @errors << "#{@uri.to_s} - #{e.message} (#{e.class})"
       @devid, @uri = get_dest
@@ -95,4 +102,4 @@ class MogileFS::NewFile::ContentRange
       retry
     end
   end
-end if defined?(Net::HTTP::Persistent)
+end