English
Units and the service registry
The contract is crates/api/src/module.rs (Unit, Stage, Build, Done) and crates/api/src/services.rs (Services); usage — build() and Instance in crates/kernel/src/instance.rs.
Why units
Configuration components (inbound, outbound, rule-set) are recreated on reload. But some modules have state that must live longer than one configuration: a certificate store watching files; a cache file; an NTP client with a clock offset. This is the part of a module that lives "for the whole lifetime of the instance" — a unit (Provide::Unit(UnitFactory)).
Lifecycle
text
Instance::new modules.units() — one instance per Provide::Unit, once
every build() unit.build(&mut Build) — in module order; publishes services,
returns Box<dyn Stage>
start / reload stage.commit() — on the first shard
Instance::close unit.close() — in reverse orderUnit::buildbuilds its part of the configuration before the components and opens nothing that would have to be rolled back on a failed reload. The services the components need from it it puts intobuild.services.Stage::commit— the configuration has gone live: on the first start — after DNS and before the inbounds; on reload — after the new inbounds have opened (the service being replaced keeps working until its successor has started). If reload fails, theStageis simply discarded.Doneis aStagewith no actions.
Build provides: sections (the decoded sections; build.section::<T>(key)), plan, reload (whether there is a running configuration), checking (a build for check: the unit opens no files and no network, e.g. cache does not open the cache file), default_mark, dns: &Arc<DnsCell>, host, services, and context() — a Context for building the unit's own components.
Module units
| Module | Unit | Publishes | Commit |
|---|---|---|---|
dial | Namespaces | Arc<ours_net_sys::netns::Namespaces> (network_namespaces, checked on every configuration) | — |
tls | Store | ours_tls_options::Tls, the TLS factory over the store from the certificate section (one per instance), and with the quic feature ours_quic::tls::Factory, QUIC TLS over the same store | installs the loaded certificates |
cache | Cache | Arc<dyn ours_api::cache::Cache>, if there is a cache section; reopens when the options change | makes the file current |
ntp | Unit | Arc<Ntp> (reuses the running one according to the plan) | starts/replaces the client |
router | DnsRules | the DNS rule compiler ours_dns_types::rules::Compiler; with rule-set-remote — the download HTTP clients | — |
dns | Ranges | Arc<FakeRanges> (fake-ip ranges from the dns section) | — |
adblock | Unit | with an adblock section: Arc<Shared>, Hook, Vetoes, Admin (the same object across all reloads) | replaces the filter in place, starts and stops list downloads |
subscriptions | Follow | — (takes DialTable and Tls) | hands the subscription manager the new list and the way to download |
(The exact types are in the modules' code: modules/system/*/src, modules/routing/router/src/lib.rs, modules/dns/dns/src/module.rs, modules/config/subscriptions/src.)
The service registry (Services)
At most one value per type (TypeId): insert replaces, get::<T>() returns Option<Arc<T>>, require::<T>() — the error "T is not available in this build". Modules find each other by type, not by name. The type lives in the contract or a shared crate; a module that needs another module's service lists it in requires, so that the service is published first.
The service layers of one configuration:
- the kernel:
Arc<DialTable>andArc<Extensions>— before the units; - units in module order — each sees what was published earlier (
ntptakesDialTable); - the resulting
Servicesgoes intoContext::servicesof the inbound/outbound/rule-set factories, intoDnsContext::servicesand intoattach_servicesof DNS, outbounds and rule-sets (this is how they get the cache file of the current configuration).
Separately — what the dispatcher factory published in Instance::new (Arc<Tracker>, Arc<Dispatcher>): it lives for the whole lifetime of the instance. Instance::service::<T>() looks first in the services of the running configuration, then there.
More about the type — Services.