Skip to main content

Module reqctx

Module reqctx 

Source
Expand description

Request Context: typed fields that travel with a request to every service it reaches, such as the user it is made for.

A field is declared once in the app, as a module-level static, with a name and the type of its value. Encore’s parser reads the declaration, so the name must be a string literal:

use encore::reqctx::Field;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone)]
pub struct User {
    pub id: String,
    pub email: String,
}

/// The user a request is made for, set by the gateway.
pub static CURRENT_USER: Field<User> = Field::new("user");

Gateway Middleware sets fields on the requests it lets through, with set_field on what on_request returns (encore::gateway), and a service reads them while handling one:

async fn orders() -> Result<(), encore::Error> {
    let Some(user) = CURRENT_USER.get() else {
        return Err(encore::Error::unauthenticated("not signed in"));
    };
    // …the orders of user.id…
    Ok(())
}

A service may set any field, or clear one, for the code it runs and the calls it makes:

let system = User { id: "system".into() };
CURRENT_USER.scope(&system, billing_invoices())?.await;

Another service, possibly in another language, binds a field by name, with the type of its value: Field::named("user").

§Where fields are

The fields are in the opentelemetry::Context, with the trace, so they follow what carries it: the current context, as FutureExt::with_context has it across .await. A task spawned with tokio::spawn starts without them, as it starts without the span: spawn fut.with_current_context() to keep both.

§Trust

No client can set a field: Encore strips the headers fields travel in wherever a request enters the app, and signs them between containers. So a service can trust what it reads, as far as it trusts the app’s other services.

§Receiving and sending fields

extract takes the fields a request a service receives carries into its context. inject sets the fields of a context on a request for another of the app’s services, and only on those: anything else would be sent who the request is made for. A Pub/Sub message carries the fields declared with Field::pubsub that are set where it is published, into its handler’s context.

§Limits

A field travels as the base64url of its value’s JSON, in a header of its own, so it must be small: its encoding may be at most 4 KiB, and those of all of a request’s fields at most 8 KiB; a field carried on Pub/Sub messages, at most 1 KiB. Setting a value beyond that is an error, at the code that set it, rather than the request failing some hops later.

§Errors

Setting a field fails with this module’s Error: the value doesn’t serialize, or it is beyond the limits. It is the module’s own and doesn’t convert into encore::Error: where a function returns one, map it with the code that fits the caller.

Structs§

Field
A Request Context field holding a T, which travels as JSON.

Enums§

Error
Why a field can’t be set to a value. See the module docs.
SizeError
A value too big to set.

Functions§

extract
A copy of cx holding the fields a request’s headers carry, in place of those it held: the first value of each header named for a field.
inject
Sets the fields of cx on a request’s headers, in place of any it carried.