Class: Vertx::Pump

Inherits:
Object
  • Object
show all
Defined in:
src/main/ruby_scripts/core/streams.rb

Overview

Pumps data from a ReadStream to a WriteStream and performs flow control where necessary to prevent the write stream from getting overloaded.

Instances of this class read bytes from a ReadStream and write them to a WriteStream. If data can be read faster than it can be written this could result in the write queue of the WriteStream growing without bound, eventually causing it to exhaust all available RAM. To prevent this, after each write, instances of this class check whether the write queue of the WriteStream is full, and if so, the ReadStream is paused, and a WriteStream#drain_handler is set on the WriteStream. When the WriteStream has processed half of its backlog, the drain_handler will be called, which results in the pump resuming the ReadStream.

This class can be used to pump from any ReadStream to any { WriteStream}, e.g. from an HttpServerRequest to an AsyncFile, or from NetSocket to a WebSocket.

Author:

Instance Method Summary (collapse)

Constructor Details

- (Pump) initialize(read_stream, write_stream)

A new instance of Pump



127
128
129
130
131
132
133
# File 'src/main/ruby_scripts/core/streams.rb', line 127

def initialize(read_stream, write_stream)
  raise "read_stream is not a ReadStream" if !read_stream.is_a? ReadStream
  raise "write_stream is not a WriteStream" if !write_stream.is_a? WriteStream
  j_rs = read_stream._to_read_stream
  j_ws = write_stream._to_write_stream
  @j_pump = org.vertx.java.core.streams.Pump.createPump(j_rs, j_ws)
end

Instance Method Details

- (FixNum) bytes_pumped

Return the total number of bytes pumped by this pump.

Returns:

  • (FixNum)

    Return the total number of bytes pumped by this pump.



152
153
154
# File 'src/main/ruby_scripts/core/streams.rb', line 152

def bytes_pumped
  @j_pump.getBytesPumped
end

- (Object) start

Start the Pump. The Pump can be started and stopped multiple times.



142
143
144
# File 'src/main/ruby_scripts/core/streams.rb', line 142

def start
  @j_pump.start
end

- (Object) stop

Stop the Pump. The Pump can be started and stopped multiple times.



147
148
149
# File 'src/main/ruby_scripts/core/streams.rb', line 147

def stop
  @j_pump.stop
end

- (Object) write_queue_max_size=(val)

Set the write queue max size

Parameters:

  • The (FixNum)

    write queue max size



137
138
139
# File 'src/main/ruby_scripts/core/streams.rb', line 137

def write_queue_max_size=(val)
  @j_pump.setWriteQueueMaxSize(val)
end