class MogileFS::HTTPReader

This class is needed because Net::HTTP streaming is still inefficient for reading huge response bodies over fast LANs.

Attributes

content_length[RW]
mogilefs_size[RW]
uri[RW]

Public Class Methods

first(paths, timeout, range = nil) click to toggle source
# File lib/mogilefs/http_reader.rb, line 28
def self.first(paths, timeout, range = nil)
  errors = nil
  range = "Range: bytes=#{range[0]}-#{range[1]}\r\n" if range

  paths.each do |path|
    begin
      sock = try(path, timeout, range) and return sock
    rescue => e
      errors ||= []
      errors << "#{path} - #{e.message} (#{e.class})"
    end
  end
  raise MogileFS::Error,
        "all paths failed with GET: #{errors.join(', ')}", []
end

Public Instance Methods

stream_to(dest) click to toggle source
# File lib/mogilefs/http_reader.rb, line 21
def stream_to(dest)
  rv = MogileFS.io.copy_stream(self, dest)
  return rv if rv == @content_length
  raise MogileFS::SizeMismatchError,
        "read=#{rv} bytes, expected=#@content_length from #@uri", []
end
to_s() click to toggle source

this may OOM your system on large files

# File lib/mogilefs/http_reader.rb, line 12
def to_s
  buf = ""
  read(@content_length, buf)
  return buf if buf.size == @content_length

  raise MogileFS::SizeMismatchError,
        "read=#{buf.size} bytes, expected=#@content_length from #@uri", []
end