State
Since Runnables are completely unaware of one another when being executed, there needs to be a way to pass data between them. Atmo uses a shared object called the request state to accomplish this. Request state is a key/value map that is updated automatically after each step in a handler.
Let's take a look at a handler from the Directive:
1
handlers:
2
- type: request
3
resource: /hello
4
method: POST
5
steps:
6
- fn: verify-request
7
- group:
8
- fn: modify-url
9
- fn: cache-get
10
- fn: fetch
Copied!
Here we can see a request with three steps. The first and third are single functions being called, and the second is a function group.
After each step in the handler, the request state is updated from the output of the functions in that step.
For example, after the first step, the state will look like this:
1
{
2
"verify-request": "ok"
3
}
Copied!
And then after the second step:
1
{
2
"verify-request": "ok"
3
"modify-url": "https://github.com/suborbital"
4
"cache-get": {"Auth-Header": "nuw45tpjno998w3un10nfwe8h"}
5
}
Copied!
When each step executes, the current request state is made available to the Runnable using Runnable API functions.
Request state is updated after each step, so it is important to note that multiple functions in a group will all receive the same state from the beginning of the step, and all of their outputs will be added to state after they've all completed executing.
You can access request state like the following Runnable example written in Rust.
1
use suborbital::req;
2
3
[...]
4
5
let url = req::state("modify-url");
Copied!
There are several clauses that allow you to control how the request state is set up (for example, choosing the key that a function's output is stored in), which will be covered later.
Last modified 2mo ago
Export as PDF
Copy link