BunDrainWaiter
Defined in: protocol/src/bun-drain-waiter.ts:43
Singleton drain waiter for Bun sockets.
Allows multiple concurrent requests to wait for drain with a simple callback-based pattern.
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new BunDrainWaiter():
BunDrainWaiter
Returns
Section titled “Returns”BunDrainWaiter
Accessors
Section titled “Accessors”needsDrain
Section titled “needsDrain”Get Signature
Section titled “Get Signature”get needsDrain():
boolean
Defined in: protocol/src/bun-drain-waiter.ts:80
Check if drain is needed.
Returns
Section titled “Returns”boolean
Methods
Section titled “Methods”clear()
Section titled “clear()”clear():
void
Defined in: protocol/src/bun-drain-waiter.ts:162
Clear all pending waiters and mark the socket closed. Call this on socket close/disconnect.
Pending waiters are REJECTED (matching Node’s DrainWaiter): a sender suspended on backpressure must not “succeed” against a dead socket.
Returns
Section titled “Returns”void
markNeedsDrain()
Section titled “markNeedsDrain()”markNeedsDrain():
void
Defined in: protocol/src/bun-drain-waiter.ts:73
Mark that drain is needed (socket.write returned false).
Returns
Section titled “Returns”void
onDrain()
Section titled “onDrain()”onDrain():
void
Defined in: protocol/src/bun-drain-waiter.ts:61
Call this from the socket’s drain handler. Resolves all pending waiters.
Returns
Section titled “Returns”void
waitForDrain()
Section titled “waitForDrain()”waitForDrain():
Promise<void>
Defined in: protocol/src/bun-drain-waiter.ts:91
Wait for socket drain.
Call markNeedsDrain() before calling this if socket.write returned false.
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”If socket is closed while waiting
writeAll()
Section titled “writeAll()”writeAll(
socket,data):Promise<void>
Defined in: protocol/src/bun-drain-waiter.ts:124
Write a buffer FULLY to a Bun socket, honoring partial writes.
socket.write() returns how many bytes were written: a partial result (including 0) means the kernel buffer is full and the unwritten tail must be re-sent after the next drain event; -1 means the socket is closed. Treating the number as a boolean drops frame tails and corrupts the wire protocol under backpressure.
Calls on the same waiter are serialized in FIFO order so concurrent senders cannot interleave bytes inside one frame. Fast path (no backpressure, no contention) costs a single write() call plus one resolved-promise hop - no extra allocation.
Parameters
Section titled “Parameters”socket
Section titled “socket”Buffer
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”If the socket closes before all bytes are written