Skip to main content

Module driver

Module driver 

Source
Expand description

A completion-style I/O driver, one per event-loop thread.

The model is the one io_uring and IOCP provide natively, and that epoll and kqueue emulate inside Driver::turn with readiness events plus non-blocking calls:

  • Receive into buffers the driver owns. The caller hands buffers to the driver with Driver::provide; the driver takes one only when data has actually arrived (io_uring’s provided-buffer ring, a read after readiness on epoll and kqueue, a read after a zero-byte WSARecv on IOCP). So an idle connection holds no buffer. Data comes back as Event::Data carrying the filled Vec<u8>; the caller owns it from then on.
  • Send owned buffers with Driver::send. Sends on one connection go out in order and are batched: everything queued during a turn is written at the start of the next Driver::turn, with one vectored call (or one submission) per connection. Each buffer comes back as Event::Sent once written, so it can be reused.
  • Accept on listeners registered with Driver::listen: Event::Accepted carries the new non-blocking socket, which the caller registers with Driver::stream (possibly on another loop’s driver).
  • Close with Driver::close: the driver cancels what is in flight, returns every buffer, closes the socket and reports Event::Closed; only then may the caller reuse the key.
  • Wake a driver blocked in Driver::turn from any thread with a Waker.

The driver never calls back into user code: Driver::turn appends Events to a vector.

Structs§

Driver
A completion-style I/O driver for one thread. See the module docs.
DriverConfig
Driver settings. The defaults suit a server loop.
DriverStats
System calls and events counted by a driver (plain counters; read them on the loop thread).
Id
A handle to a socket registered with a Driver. Stale handles (after the socket was closed) are detected by a generation count and ignored.
Waker
Wakes a driver blocked in Driver::turn from any thread. Cheap to clone; repeated wakes before the driver runs collapse into one OS signal.

Enums§

Backend
Which I/O back end a driver uses.
Event
Something that happened, reported by Driver::turn.
OutBuf
A buffer to send: owned (returned by Event::Sent for reuse) or static.