Class: EventMachine::FileStreamer

Inherits:
Object
  • Object
show all
Includes:
Deferrable
Defined in:
lib/em/streamer.rb

Overview

Streams a file over a given connection. Streaming begins once the object is instantiated. Typically FileStreamer instances are not reused.

Streaming uses buffering for files larger than 16K and uses so-called fast file reader (a C++ extension) if available (it is part of eventmachine gem itself).

Examples:


module FileSender
  def post_init
    streamer = EventMachine::FileStreamer.new(self, '/tmp/bigfile.tar')
    streamer.callback{
      # file was sent successfully
      close_connection_after_writing
    }
  end
end

Author:

  • Francis Cianfrocca

Constant Summary collapse

MappingThreshold =

Use mapped streamer for files bigger than 16k

16384
BackpressureLevel =

Wait until next tick to send more data when 50k is still in the outgoing buffer

50000
ChunkSize =

Send 16k chunks at a time

16384

Constants included from Deferrable

Deferrable::Pool

Instance Method Summary collapse

Methods included from Deferrable

#callback, #cancel_callback, #cancel_errback, #cancel_timeout, #errback, #fail, future, #set_deferred_status, #succeed, #timeout

Constructor Details

#initialize(connection, filename, args = {}) ⇒ FileStreamer

Returns a new instance of FileStreamer.

Parameters:

  • connection (EventMachine::Connection)
  • filename (String)

    File path

  • args (Hash) (defaults to: {})

    a customizable set of options

Options Hash (args):

  • :http_chunks (Boolean) — default: false

    Use HTTP 1.1 style chunked-encoding semantics.


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/em/streamer.rb', line 36

def initialize connection, filename, args = {}
  @connection = connection
  @http_chunks = args[:http_chunks]

  if File.exist?(filename)
    @size = File.size(filename)
    if @size <= MappingThreshold
      stream_without_mapping filename
    else
      stream_with_mapping filename
    end
  else
    fail "file not found"
  end
end