Expand description
A request-scoped configuration snapshot for Actix Web.
use actix_web::{get, App, HttpServer};
use dynamic_config_actix::{Config, DynamicConfig};
use dynamic_config_web_core::sections;
#[get("/")]
async fn index(server: Config<Server>, features: Config<Features>) -> String {
// Both came out of one snapshot, taken when the request began.
// `Sections::take` retries if a reload lands mid-read, so these
// two cannot be different generations.
format!("{} {}", server.port, features.cache)
}
HttpServer::new(|| {
App::new()
.wrap(DynamicConfig::new(sections![Server, Features]))
.service(index)
});§What this is for
Actix runs handlers across several worker threads, and Server::current()
is an atomic load that every worker can make without a lock. That part is
already right. What it does not give you is two sections that agree: a
reload landing between two reads lets one response mix generations.
DynamicConfig reads every listed section once, before the handler
runs, and puts the result in the request’s extensions. Config<T>
reads it back out.
Note what is not here: no web::Data<ServerConfig>. Handing a
snapshot to the app factory freezes it at start-up, and each worker then
serves the configuration that existed when it was built.
§What this is not
It does not load configuration, watch files, or own a WatchHandle.
That stays in the startup code that calls init() and holds the handles
for the life of the process.
Macros§
- sections
- The sections a request reads, by type.
Structs§
- Config
- One section of this request’s configuration.
- Config
Sections - The sections a request reads, and how to read each one.
- Dynamic
Config - Takes one snapshot per request and puts it in the request’s extensions.
- Snapshot
Middleware - The service
DynamicConfigwraps an application in.
Enums§
- OutOf
Scope - Why a section is not in this request’s snapshot.
- Snapshot
Missing - Why a
Configextractor could not answer.
Functions§
- snapshot
- The snapshot this request began with, for code holding an
HttpRequestrather than using the extractor.