module

IO::Buffered

Opt-in buffer pooling for IO::Buffered, enabled with #buffer_pool=.

  • Write buffer: acquired on first write, released after each flush
  • Read buffer: released when all buffered data has been consumed. Sockets only acquire a buffer once data is available, so idle sockets waiting for data hold no read buffer at all.

Instance methods

buffer_fills

Number of reads from the underlying IO into the read buffer, wraps around

Source
buffer_pool
Source
buffer_pool=(pool : IO::BufferPool | Nil)

Use buffers from pool (also sets buffer_size to the pool's buffer size). Must be set before any buffer has been allocated.

Source
buffer_size=(value)

Pooled buffers are released while not in use, so the stdlib's check for allocated buffers doesn't stop a change to a size the pool's buffers don't have

Source
close

Flushes through the pool, so that the write buffer is detached and returned like on any flush, then detaches the IO from the pool. A reader that still holds the read buffer returns it when its read fails.

Source
discard_write_buffer

Drops data buffered for writing, so that a following close doesn't flush it. Used when abandoning a connection: after a failed write the buffer still holds data that may already have been partly sent.

Source
flush

Flushes any buffered data and the underlying IO. Returns self.

Source
read(slice : Bytes) : Int32

Buffered implementation of IO#read(slice).

Source
write(slice : Bytes) : Nil

A write that doesn't fit what's left of the buffer tops the buffer up first, so that the buffered bytes go out with the start of it instead of as a small write of their own. Those writes happen before the caller's flush, and with TCP_NODELAY each would be sent as a TCP segment of its own, so a TCP socket is corked until that flush, which sends whatever is left right away. A write that fits the buffer doesn't cork.

Source