eve.rt.fiber.pool
Fiber Pool — Reusable Fiber Stacks for High-Concurrency Servers
This module provides FiberPool, a class that maintains a pool of reusable fibers. Creating and destroying fibers is expensive because each allocates a stack (typically 64 KB on POSIX). The pool amortises this cost by keeping finished fibers alive and re-dispatching them through a trampoline loop.
Lifecycle of a pooled fiber:
spawn(work)creates a fiber whose trampoline readsslot.work, executes it, returns the slot to the free list, and then parks (yield without re-queue).- The next
spawn()call takes the slot from the free list,sets
slot.work, and un-parks the fiber so the trampoline runs again.
Thread affinity: one pool per FiberScheduler, one scheduler per event loop, one event loop per thread.
Example:
auto loop = EventLoop.create();
auto sched = new FiberScheduler(loop);
sched.attachToLoop();
auto pool = new FiberPool(sched, 16, 1024);
scope (exit) pool.dispose();
server.onAccept = (ref TcpListener s, Handle h) {
pool.spawn(() @trusted {
auto sock = FiberSocket.adopt(sched, *sched.eventLoop, h);
handleRequest(sock);
sock.dispose();
});
};
loop.run();Copyright
Types 1
A pool of reusable fibers.
Fibers are created on demand (lazy allocation) and reused via a per-slot trampoline. Each trampoline is an infinite loop that:
- Reads and clears its slot's work delegate.
- Executes the delegate (if any).
- Returns the slot to the free list.
- Parks and yields — waits for
unpark()from the nextspawn()call.
The pool integrates with a FiberScheduler for execution but manages fiber lifecycle independently.
Slot[] slots_size_t[] freeSlots_FiberScheduler sched_size_t maxSize_size_t activeCount_size_t totalCreated_void delegate(Throwable) errorHandler_void delegate() makeTrampoline(size_t idx) @trustedvoid createSlot(size_t idx) @trustedvoid onError(void delegate(Throwable) handler) @safe nothrowSet the error handler for unhandled exceptions in pooled fibers.size_t activeCount() @property const pure @safe nothrow @nogcNumber of fibers currently executing work.size_t freeCount() @property const pure @safe nothrow @nogcNumber of fibers available in the free list.size_t totalCount() @property const pure @safe nothrow @nogcTotal number of fibers created by this pool.this(FiberScheduler sched, size_t initialSize = 0,
size_t maxSize = 0)Create a fiber pool.Slot