API · Flow

ratelimit()

Emit no more than a fixed number of values per time window, delaying rather than dropping excess input.

Signature

ratelimit(num: number, milliseconds: number): Exstream<T, C>

Example

const apiCalls = exstream(requests).ratelimit(100, 60_000)

Parameters

num

Type positive integerRequired

Maximum values emitted in one window.

milliseconds

Type non-negative finite numberRequired

Window duration. Zero effectively allows continuous delivery.

Window behavior

The first num values in a window pass immediately. The next value waits for the remainder of the window, is emitted as the first value of a new window, and only then is upstream asked for more. No successful values are dropped or reordered.

At most one excess value and one timer are retained, so memory is constant and delay becomes upstream backpressure. Existing record errors pass immediately and do not count against the rate. Branch end clears the timer.

Errors

Invalid counts and durations throw when the operator is created. The JavaScript runtime performs numeric coercion; TypeScript accepts numbers only.

Forms

stream.ratelimit(100, 60_000)
exstream.pipeline().ratelimit(100, 60_000)
exstream.ratelimit(100, 60_000, stream)
stream.through(exstream.ratelimit(100, 60_000))

throttle(), makeAsync(), mapAsync()