☀️ State
To recap, a Service::serve method has the following signature:
fn serve(
&self,
req: Request,
) -> impl Future<Output = Result<Self::Response, Self::Error>> + Send + '_;
&selfallows services to access sharedSend + Sync + 'staticstate internal and specific to thatService;Requestis the input used to produce aResult.Requestalso containsExtensionswhich can be used to store dynamic state
Types of state
rama supports two kinds of states:
- static state: this state can be a part of the service struct or captured by a closure
- dynamic state: these can be injected as
Extensionss using methods such asExtensions::insertorrequest.extensions_mut().insert
Any state that is optional, and especially optional state injected by middleware, can be inserted using extensions.
It is however important to try as much as possible to then also consume this state in an approach that deals
gracefully with its absence. Good examples of this are header-related inputs. Headers might be set or not,
and so absence of Extensionss that might be created as a result of these might reasonably not exist.
It might of course still mean the app returns an error response when it is absent, but it should not unwrap/panic.
Extensions
Extensions is what this chapter is about, and its documemtation can be consumed at https://ramaproxy.org/docs/rama/extensions/struct.Extensions.html.
- access
Extensionsthat can be used to dynamically get and set extra (optional) data, to be passed for usage by inner service(s).