Skip to content

Refactor/async runtime#16

Merged
phaethix merged 5 commits into
masterfrom
refactor/async-runtime
Apr 30, 2026
Merged

Refactor/async runtime#16
phaethix merged 5 commits into
masterfrom
refactor/async-runtime

Conversation

@phaethix
Copy link
Copy Markdown
Owner

No description provided.

phaethix added 5 commits May 1, 2026 00:27
Rewrite the accept loop and per-connection handler on top of a
multi-threaded tokio runtime while keeping the public API of
run_listener/start synchronous, so the existing std::net::TcpListener
tests and client code work unchanged.

- Shutdown: add an async notifier alongside the atomic flag so the
  accept loop can park on notified() via tokio::select! instead of the
  old self-connect wake-up trick.
- Server: introduce ServerConfig::worker_threads, build a scoped
  runtime inside run_listener, and drive an async accept/handler loop
  that uses tokio::io for read/write and tokio::time::timeout for the
  per-client idle timeout.
- Tests: drop Shutdown::wake_listener from shutdown_test (no longer
  needed) and spread ..Default::default() in max_clients_test so it
  survives the new field.
Add a new io-threads directive (CLI: --io-threads, file: io-threads)
that controls how many worker threads back the tokio runtime driving
the TCP server. Zero or unset means "let tokio pick" — one worker per
logical CPU in practice — which is the right default for the vast
majority of deployments; an explicit value lets operators cap the
server's CPU footprint on shared hosts.

- config/file.rs: new FileConfig::io_threads + parser directive.
- cli.rs: new --io-threads flag with precedence CLI > file > default;
  box RawFlags inside ScanOutcome to keep clippy happy now that the
  enum has grown a bit.
- main.rs: wire the resolved value into ServerConfig::worker_threads
  and log it at startup.
- ferrum.conf.example: document the new directive.
Fan 500 simultaneous connections into the server with only two tokio
worker threads and PING each of them end-to-end. The test proves the
async accept loop multiplexes hundreds of sockets with a handful of
threads — the workload that would have cost 500 OS threads under the
pre-tokio design.
Re-ran redis-benchmark on the exact same hardware as the Week 7
baseline so v0.3.0 (sync) and v0.4.0 (tokio) numbers sit side by side
in the same file for easy git-diff regression spotting. Key findings:

- Single-client throughput +5% on average (async read/write batches
  more efficiently than blocking syscalls).
- Pipelined numbers stay inside run-to-run noise — the hot path is
  now engine bound, not I/O bound.
- A new c=500 scenario documents the real win of Phase 8: hundreds of
  clients run on a handful of worker threads instead of exploding the
  kernel thread count.
@phaethix phaethix merged commit 25d4f1b into master Apr 30, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant