Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Development

Information about developing the project. If you plan on contributing, see the contributor’s guide. If you are only interested in using it, you can safely ignore this page.

Rust Documentation

Tuwunel’s rustdocs are hosted within this book under the /docs directory. Developers may build the same documentation locally using cargo doc.

Tuwunel project layout

Tuwunel uses a collection of sub-crates, packages, or workspace members that indicate what each general area of code is for. All of the workspace members are under src/. The workspace definition is at the top level Cargo.toml. See the Rust documentation on Workspaces for general questions and information on Cargo workspaces.

Tuwunel’s crates form a directed acyclic-graph without circular dependencies. Listed here from the top are the most abstract down to the most dependent at the bottom. Crates only have visibility into other crates listed above them; they cannot see structs or call functions in any crate listed below them.

  • tuwunel_macros are Tuwunel Rust macros like general helper macros, logging and error handling macros, and syn and procedural macros.

  • tuwunel_core is core Tuwunel functionality like config loading, error definitions, global utilities, logging infrastructure, etc.

  • tuwunel_matrix is the Matrix data model shared by the crates below it: the event and PDU types, room version rules, and the state resolution algorithm. It depends only on tuwunel_core.

  • tuwunel_database is RocksDB encapsulation, interface wrappers, configurations, and our opinionated asynchronous database frontend.

  • tuwunel_service is stateful runtime functionality at the heart of the application. This crate is divided into “services” each with “workers” and queues and all of the moving parts that attend to the tasks of sending messages and notifications, etc. Each service attempts to encapsulate any database tables it requires for its persistent state. Services call other services and they do not form an acyclic graph, for now.

  • tuwunel_api is the contract shared by the HTTP API crates below it: the Ruma request extractor, request authentication, and the response types. It holds no endpoint handlers itself.

  • tuwunel_api_client, tuwunel_api_federation, tuwunel_api_admin, tuwunel_api_oidc and tuwunel_api_mas are the stateless runtime functionality, one crate per API surface: the Matrix Client-Server API, the Server-Server (federation) API, the administration endpoints (the Synapse-compatible admin API and the client admin endpoints), the native OpenID Connect provider, and the Matrix Authentication Service integration. Their http handlers call various services to query or update their state as necessary. They do not interface with raw data or database functions except through a service.

  • tuwunel_admin is a module that implements the admin room as a broad set of command API handlers. Similar to tuwunel_api these handlers also interface with various services as necessary. Currently the admin crate does not call into the API crates as a dependency, but this is not intentional and subject to change.

  • tuwunel_router is the webserver and request handling bits, using axum, tower, tower-http, hyper, etc, and the server state to drive the handlers of the API crates.

  • main is the binary executable. This is where the main() function lives, tokio worker and async initialisation, Sentry initialisation, clap init, and signal handling. If you are adding new Rust features, they must go here. This crate is also capable of compiling as a library for integration testing and embedding.

Notes

It is highly unlikely you will ever need to add a new workspace member, instead look to create a new Service to implement distinct or unique functionality. If you truly find yourself needing another crate, we recommend reaching out to us in the Matrix room for discussions about it beforehand.

The primary inspiration for this design was apart of hot reloadable development, to support “Tuwunel as a library” where specific parts can simply be swapped out. There is evidence Conduit wanted to go this route too as axum is technically an optional feature in Conduit, and can be compiled without the binary or axum library for handling inbound web requests; but it was never completed or worked.

Adding compile-time features

To add a compile-time feature:

  1. Define it in the Cargo.toml of each workspace crate whose code or dependencies it changes. Crates in between do not define it.
  2. Define it in src/main/Cargo.toml, forwarding it to each of those crates. For example, a feature named woof in the api crate is defined there as woof = [] and in main as woof = ["tuwunel-api/woof"].
  3. If the feature is on by default, add it to the default list of main and of each crate defining it.
  4. Add a row to Build Features, with its default membership and any runtime requirement.

A crate’s default also enables the default of each workspace dependency that defines one. This gives the crate the same features when built alone as in the server. See Crate defaults.

Rust / Cargo does not support “workspace level features”, so main is the one place that defines the public features and their defaults, and the crate defaults only mirror it. Our Nix flake collects the default feature set from main’s default alone. It takes that crate’s own feature names and skips the forwarded crate defaults.

List of forked dependencies

During Tuwunel development, we have had to fork some dependencies to support our use-cases in some areas. This ranges from things said upstream project won’t accept for any reason, faster-paced development (unresponsive or slow upstream), Tuwunel-specific usecases, or lack of time to upstream some things.

Debugging with tokio-console

tokio-console can be a useful tool for debugging and profiling. To make a tokio-console-enabled build of Tuwunel, enable the tokio_console feature, disable the default release_max_log_level feature, and set the --cfg tokio_unstable flag to enable experimental tokio APIs. A build might look like this:

RUSTFLAGS="--cfg tokio_unstable" cargo +nightly build \
    --release \
    --no-default-features \
    --features=systemd,element_hacks,gzip_compression,brotli_compression,zstd_compression,tokio_console

You will also need to enable the tokio_console config option in Tuwunel when starting it. This was due to tokio-console causing gradual memory leak/usage if left enabled.