BufferScheduler class

This class accepts a Node.js Readable stream as input, and keeps reading data from the stream into the internal buffer structure, until it reaches maxBuffers. Every available buffer will try to trigger outgoingHandler. The internal buffer structure includes an incoming buffer array, and a outgoing buffer array. The incoming buffer array includes the "empty" buffers can be filled with new incoming data. The outgoing array includes the filled buffers to be handled by outgoingHandler. Every above buffer size is defined by parameter bufferSize.

NUM_OF_ALL_BUFFERS = BUFFERS_IN_INCOMING + BUFFERS_IN_OUTGOING + BUFFERS_UNDER_HANDLING

NUM_OF_ALL_BUFFERS <= maxBuffers

PERFORMANCE IMPROVEMENT TIPS:

  1. Input stream highWaterMark is better to set a same value with bufferSize parameter, which will avoid Buffer.concat() operations.
  2. Parallelism should set a smaller value than maxBuffers, which is helpful to reduce the possibility when a outgoing handler waits for the stream data. in this situation, outgoing handlers are blocked. Outgoing queue shouldn't be empty.

Constructors

BufferScheduler(Readable, number, number, OutgoingHandler, number, undefined | string)

Creates an instance of BufferScheduler.

Methods

do()

Start the scheduler, will return error when stream of any of the outgoingHandlers returns error.

Constructor Details

BufferScheduler(Readable, number, number, OutgoingHandler, number, undefined | string)

Creates an instance of BufferScheduler.

new BufferScheduler(readable: Readable, bufferSize: number, maxBuffers: number, outgoingHandler: OutgoingHandler, parallelism: number, encoding?: undefined | string)

Parameters

readable

Readable

A Node.js Readable stream

bufferSize

number

Buffer size of every maintained buffer

maxBuffers

number

How many buffers can be allocated

outgoingHandler
OutgoingHandler

An async function scheduled to be triggered when a buffer fully filled with stream data

parallelism

number

Concurrency of executing outgoingHandlers (>0)

encoding

undefined | string

Method Details

do()

Start the scheduler, will return error when stream of any of the outgoingHandlers returns error.

function do()

Returns

Promise<void>