Class: Vertx::Pump

Inherits:
Object
  • Object
show all
Defined in:
lib/vertx/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

#initialize(read_stream, write_stream) ⇒ Pump

Returns a new instance of Pump.



140
141
142
143
144
145
146
# File 'lib/vertx/streams.rb', line 140

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

#bytes_pumpedFixNum

Return the total number of bytes pumped by this pump.

Returns:

  • (FixNum)

    Return the total number of bytes pumped by this pump.



168
169
170
# File 'lib/vertx/streams.rb', line 168

def bytes_pumped
  @j_pump.bytesPumped
end

#startObject

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



156
157
158
159
# File 'lib/vertx/streams.rb', line 156

def start
  @j_pump.start
  self
end

#stopObject

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



162
163
164
165
# File 'lib/vertx/streams.rb', line 162

def stop
  @j_pump.stop
  self
end

#write_queue_max_size=(val) ⇒ Object

Set the write queue max size

Parameters:

  • The (FixNum)

    write queue max size



150
151
152
153
# File 'lib/vertx/streams.rb', line 150

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