about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-12-11 08:47:39 +0000
committerEric Wong <normalperson@yhbt.net>2011-12-11 08:55:29 +0000
commit3f342091cec9fcd5f99ba934444f17cd0c77de71 (patch)
treea6a40207d1938206b88c5fe3bd1757095ac67d54
parent9d5f3208dab86837106b56f105af8bbb121f438a (diff)
downloadmogilefs-client-3f342091cec9fcd5f99ba934444f17cd0c77de71.tar.gz
This defaults to 1 hour (which matches the expiry time of a row
in the MogileFS internal tempfile table).
-rw-r--r--lib/mogilefs/mogilefs.rb8
-rw-r--r--lib/mogilefs/new_file/common.rb5
2 files changed, 10 insertions, 3 deletions
diff --git a/lib/mogilefs/mogilefs.rb b/lib/mogilefs/mogilefs.rb
index 8a35168..005bb19 100644
--- a/lib/mogilefs/mogilefs.rb
+++ b/lib/mogilefs/mogilefs.rb
@@ -37,12 +37,16 @@ class MogileFS::MogileFS < MogileFS::Client
   # The timeout for get_file_data.  Defaults to five seconds.
   attr_accessor :get_file_data_timeout
 
+  # The maximum allowed time for creating a new_file.  Defaults to 1 hour.
+  attr_accessor :new_file_max_time
+
   # Creates a new MogileFS::MogileFS instance.  +args+ must include a key
   # :domain specifying the domain of this client.
   def initialize(args = {})
     @domain = args[:domain]
 
-    @get_file_data_timeout = 5
+    @get_file_data_timeout = args[:get_file_data_timeout] || 5
+    @new_file_max_time = args[:new_file_max_time] || 3600.0
 
     raise ArgumentError, "you must specify a domain" unless @domain
 
@@ -178,6 +182,8 @@ class MogileFS::MogileFS < MogileFS::Client
     opts[:backend] = @backend
     opts[:fid] = res['fid']
     opts[:content_length] ||= bytes if bytes
+    opts[:new_file_max_time] ||= @new_file_max_time
+    opts[:start_time] = Time.now
 
     case (dests[0][1] rescue nil)
     when %r{\Ahttp://}
diff --git a/lib/mogilefs/new_file/common.rb b/lib/mogilefs/new_file/common.rb
index 38d0fd3..e959a21 100644
--- a/lib/mogilefs/new_file/common.rb
+++ b/lib/mogilefs/new_file/common.rb
@@ -19,8 +19,9 @@ module MogileFS::NewFile::Common
   MD5_TRAILER_NODES = {} # :nodoc: # EXPERIMENTAL
 
   def read_response(sock)
-    # mostly relying on SO_KEEPALIVE to timeout
-    case line = sock.timed_read(23, "", 7200)
+    tout = @opts[:new_file_max_time] || 3600.0
+    start_time = @opts[:start_time] and tout -= Time.now - start_time
+    case line = sock.timed_read(23, "", tout > 0.0 ? tout : 0)
     when %r{^HTTP/\d\.\d\s+(2\d\d)\s} # success!
     when nil
       raise EmptyResponseError, 'Unable to read response line from server'