From d74fad403f23c1ee36b9fbf276d12b12ef5d92d1 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 6 Dec 2011 13:25:22 -0800 Subject: [PATCH] rely on SO_KEEPALIVE for upload timeouts Users will be able to tweak these themselves in OS-dependent ways. It's probably better to rely on protocol-level timeouts when the HTTP server should be on a trusted network. --- lib/mogilefs/http_file.rb | 37 ++++--------------------------------- 1 file changed, 4 insertions(+), 33 deletions(-) diff --git a/lib/mogilefs/http_file.rb b/lib/mogilefs/http_file.rb index 4d88c78..b8bfd48 100644 --- a/lib/mogilefs/http_file.rb +++ b/lib/mogilefs/http_file.rb @@ -22,34 +22,7 @@ class MogileFS::HTTPFile < StringIO end class NonRetryableError < MogileFS::Error; end - class HTTPSock < MogileFS::Socket - attr_accessor :start - - # Increase timeout as we become more invested in uploading with - # this socket. The server could be experiencing I/O delays - # from large uploads because the sysadmin forgot to tune the - # VM sysctls for handling large files. - def write(buf) - timed_write(buf, Time.now - @start + 5.0) - end - end - - # :stopdoc: MD5_TRAILER_NODES = {} # :nodoc: # EXPERIMENTAL - class << self - attr_accessor :response_timeout_cb - end - - # temporary directories (nginx) may not be configured on the - # same device, necessitating a time-consuming full file copy - # instead of a quick rename(2)/link(2) operation - @response_timeout_cb = lambda do |elapsed_time, bytes_uploaded| - mbytes_uploaded = bytes_uploaded / (1024.0 * 1024.0) - # assumes worst case is 10M/s on the remote storage disk - t = mbytes_uploaded * 10 + elapsed_time - t < 5 ? 5 : t - end - # :startdoc: ## # The big_io name in case we have file > 256M @@ -132,9 +105,8 @@ class MogileFS::HTTPFile < StringIO # Writes an HTTP PUT request to +sock+ to upload the file and # returns file size if the socket finished writing def upload(devid, uri) # :nodoc: - start = Time.now - sock = HTTPSock.tcp(uri.host, uri.port) - sock.start = start + sock = MogileFS::Socket.tcp(uri.host, uri.port) + sock.setsockopt(Socket::IPPROTO_TCP, Socket::SO_KEEPALIVE, 1) file_size = length if @streaming_io @@ -159,9 +131,8 @@ class MogileFS::HTTPFile < StringIO request_put(sock, uri, file_size, self) end - tout = self.class.response_timeout_cb.call(Time.now - start, file_size) - - case line = sock.timed_read(23, "", tout) + # mostly relying on SO_KEEPALIVE to timeout + case line = sock.timed_read(23, "", 7200) when %r{^HTTP/\d\.\d\s+(2\d\d)\s} # success! file_size when nil -- 2.11.4.GIT