Skip to content

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 order
  • Unit::build builds 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 into build.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, the Stage is simply discarded.
  • Done is a Stage with 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 ​

ModuleUnitPublishesCommit
dialNamespacesArc<ours_net_sys::netns::Namespaces> (network_namespaces, checked on every configuration)—
tlsStoreours_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 storeinstalls the loaded certificates
cacheCacheArc<dyn ours_api::cache::Cache>, if there is a cache section; reopens when the options changemakes the file current
ntpUnitArc<Ntp> (reuses the running one according to the plan)starts/replaces the client
routerDnsRulesthe DNS rule compiler ours_dns_types::rules::Compiler; with rule-set-remote — the download HTTP clients—
dnsRangesArc<FakeRanges> (fake-ip ranges from the dns section)—
adblockUnitwith an adblock section: Arc<Shared>, Hook, Vetoes, Admin (the same object across all reloads)replaces the filter in place, starts and stops list downloads
subscriptionsFollow— (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:

  1. the kernel: Arc<DialTable> and Arc<Extensions> — before the units;
  2. units in module order — each sees what was published earlier (ntp takes DialTable);
  3. the resulting Services goes into Context::services of the inbound/outbound/rule-set factories, into DnsContext::services and into attach_services of 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.

GPL-3.0-or-later