Hyperfile Reactor
A lightweight task execution framework built on top of Tokio's
LocalSet.
A Reactor owns a dedicated OS thread running a current_thread tokio
runtime plus a LocalSet. You register typed tasks on the reactor; each
task listens on one or more user-defined channels, each with its own
priority and capacity, and processes incoming contexts via a
single-threaded handler. Channel dispatch is governed by a pluggable
[Scheduler].
When to use
- You need single-threaded handlers (e.g. owning
!Sendstate, a file handle, an in-memory cache, a state machine) but still want to fan in work from many producer threads. - You want priority-aware dispatch with optional anti-starvation, without
rolling your own
select_biased!loop. - You want backpressure on bounded channels, without giving up the option of unbounded ones for low-volume control paths.
When not to use
- Your handler does CPU-heavy or blocking work — that will stall every
channel on the reactor thread. Offload such work via
tokio::task::spawn,spawn_blocking, or a separate worker pool. - You need multi-consumer dispatch (worker pool semantics). This crate is one consumer per task by design.
Quickstart
Cargo.toml:
[]
= "0.3"
= { = "1", = ["rt", "sync", "macros", "time"] }
use ;
;
Channels and backpressure
Each channel registered on a TaskBuilder is independent and has its own
[Capacity]:
use ;
let mut builder = new;
// Bounded: producers experience backpressure when the queue is full.
let critical = builder.add_bounded;
// Unbounded: producers never block, at the cost of no memory safety net.
let bulk = builder.add_unbounded;
// Or use the explicit form:
let other = builder.add_channel;
Producers pick the right send method based on what they need:
| Method | Behaviour | Returns |
|---|---|---|
try_send / send |
Non-blocking. On a full bounded channel, returns Err(TrySendError::Full(ctx)). |
Result<(), TrySendError<Ctx>> |
send_async |
On a bounded channel, awaits free capacity. | Result<(), SendError<Ctx>> |
send and try_send are the same call (both non-blocking). send_async
is the only way to apply backpressure on a bounded channel without
discarding work.
Schedulers
The default scheduler is [FairPriority]: strict priority with
anti-starvation. After a configurable budget of consecutive top-priority
items, one item from the lowest non-empty priority band is served.
Pick a different scheduler with spawn_with:
use ;
// Strict priority: low-priority work may starve under sustained high-prio load.
let handler = reactor
.spawn_with
.unwrap;
// Fair priority with custom budget.
let handler = reactor
.spawn_with
.unwrap;
Built-ins:
- [
StrictPriority] — always serves the highest-priority non-empty channel. - [
FairPriority::new(budget)] — strict priority with anti-starvation.FairPriority::default()usesbudget = 16.
You can implement your own [Scheduler] if you need weighted round-robin,
token-bucket, deadline-based, or any other policy.
Spawning from inside a tokio runtime
Reactor::spawn blocks the current thread until the task starts. Don't
call it from inside an existing tokio runtime — use the _async
variants:
let handler = reactor.spawn_async.await.unwrap;
let handler = reactor
.spawn_with_async
.await
.unwrap;
Shutdown
A Reactor owns its background thread. The thread joins when:
- all
TaskHandlerclones for every spawned task have been dropped, and Reactor::shutdown()is called or theReactoris dropped.
Each task's scheduler loop exits once all its senders are gone. If you call
shutdown() while producers are still alive, it will block waiting for
them. Drop the handlers first:
drop; // release this task's senders
reactor.shutdown.unwrap; // join the background thread
Handler discipline
The handler runs single-threaded inside a LocalSet. Two rules:
- Never block. No
std::thread::sleep, no synchronous IO, no busy loops. Usetokio::time::sleep, async IO, or hand work totokio::task::spawn/spawn_blocking. - Be fast in the synchronous portion. Anything you
awaiton insidehandlepauses the entire reactor for this task. Long-running async work should usually be detached viatokio::task::spawn.
Example
A runnable example demonstrating bounded channels, scheduler choice, and
send_async backpressure:
Stability
Pre-1.0; minor versions may include breaking API changes. Public enums are
#[non_exhaustive] to keep adding variants from being a breaking change.
License
Apache-2.0. See LICENSE.
Contributing
Issues and pull requests are welcome.