Skip to content

Units and the dispatcher ​

Provide::Unit, Provide::Dispatcher — crates/api/src/module.rs; the dispatcher traits — crates/api/src/dispatch.rs.

Unit ​

rust
Provide::Unit(fn() -> Box<dyn Unit>)

trait Unit: Send + Sync {
    fn build(&self, build: &mut Build<'_>) -> Result<Box<dyn Stage>, String>;
    fn close(&self) {}
}
trait Stage: Send { fn commit(self: Box<Self>) {} }

The part of a module that lives for the whole lifetime of the instance, across reloads: the certificate store, the cache file, NTP, the subscription manager. There is no slot: a module may provide several units. The lifecycle, the order and the table of module units — Units and services.

The rule for build: open nothing that would have to be rolled back if reload fails; everything with side effects goes into Stage::commit.

Dispatcher ​

rust
Provide::Dispatcher(fn(routing: Routing, services: &mut Services) -> Arc<dyn DispatchControl>)

Exactly one module of the build (checked by Modules::new). The factory is called once per Instance with the first routing; what it puts into services the instance hands out by type for its whole lifetime (dispatch puts in Arc<Tracker> and Arc<Dispatcher>).

Dispatch — what inbounds see ​

MethodWhat
route_stream(stream, metadata)route a stream and pump it to the end
route_packet(conn, metadata, idle)route a packet stream; idle — the idle timeout (a rule may shorten it)
connect(metadata)route and connect, return Connected { stream, metadata, tracked: Guard } without relay
inject(tag, stream, source)hand the stream to inbound tag (its detour)
inject_packet(tag, conn, source)hand the packet flow to inbound tag (its detour)

Guard keeps the connection accounted for while the stream handed to the caller is alive; Release — whom to notify of the end.

DispatchControl — what the kernel drives ​

MethodWhat
reload(routing)new connections — by routing; live ones — by the policies; return (closed, kept)
preview(&routing)ConnectionImpact { closed, kept, moved, examples } without changes
rule_set_updated(tag)reconsider the connections whose rules looked at the rule-set
rule_sets_changed()a Notify woken after a rule-set update

Routing ​

The snapshot used for routing: rules, outbounds by tag, default (route.final or the first one), policies (Policies: reload.connections and the reload blocks of inbounds and outbounds), dns, host, inbounds by tag (for inject and inject_packet), veto (the Vetoes service: a module's last word after the rules), half_close_timeout (route.tcp_half_close_timeout). Built by the kernel (Instance); how the policies are applied — Reload.

UDP_TIMEOUT (300 s) — the packet stream timeout, if the inbound did not set its own.

The implementation is the dispatch module (description, the connection's path — Data path).

GPL-3.0-or-later