ResponsePool
struct ResponsePoolStack of recycled Response objects for keep-alive reuse.
The pool holds at most capacity items. Acquiring from an
empty pool constructs a fresh Response; releasing into a
full pool drops the response on the floor (Mojo destructor
runs).
Fields:
_slots: Top-of-stack-grows-up storage of heap-allocated
Response cell addresses (returned by
Pool[Response].alloc_move). Each cell holds
a Response whose backing buffers (header lists,
body bytes) have been reset() empty but
retain their allocated capacity.
_capacity: Maximum number of items the pool will hold.
Releases past this cap drop the released
response.
Implements Movable
__init__
def __init__(out self) Construct an empty pool with the default capacity (8).
__deinit__
def __deinit__(deinit self) Free every retained cell when the pool is dropped.
Without this, dropping a non-empty pool would leak every cell + its body / header backings.
with_capacity
def with_capacity(capacity: Int) -> Self Construct an empty pool with a custom maximum capacity.
- Args
capacity: Maximum number of pooledResponseobjects. Must be > 0.
acquire
def acquire(mut self, status: Int = Int(200), var reason: String = "") -> Response Return a reusable Response from the top of the pool, or construct a fresh one if the pool is empty.
On a hit, the returned Response has empty headers + body
but retains its prior backing capacity, so the next
headers.append / body.append call reuses the same
malloc'd backing as long as the new payload fits.
- Args
status: Status code for the returned response (default 200).reason: Reason phrase (default empty — the serializer will fill from the status code at write time).
release
def release(mut self, var response: ResponseImpl) Return response to the pool for future acquire calls.
If the pool is at capacity the response is destroyed
(Mojo's var destructor runs at the end of this
function); the pool itself never grows past
_capacity.
- Args
response: OwnedResponseto recycle.
size
def size(self) -> Int Return the number of recycled responses currently sitting in the pool.
Useful for tests + telemetry.
capacity
def capacity(self) -> Int Return the maximum number of items the pool will hold.