Skip to content

Container ​

container is the central dependency-injection mechanism that your app creates. It holds references to every resource: configurations, auth keys, data sources, logging, metrics, and external HTTP services.

Overview ​

Access Workflow ​

The context holds a reference to the container. Through it, the context reaches every resource and runs the operations each incoming request needs.

PubSub covers Kafka, MQTT, and NATS. KVStore (Redis / NATS KV / memory / SQLite) and FileStore (local / ftp / sftp) are the unified key-value and blob backends. Register them with app.addKVStore(...) and app.addFileStore(...) (see KV Store and File Store).

Health checks ​

The container aggregates component health behind two well-known endpoints:

  • GET /.well-known/health: returns the aggregated status as JSON ({ "<name>": "up" }). If any check fails the response body reports the failing component as down and the endpoint returns a non-200 status.

  • GET /.well-known/live: liveness probe, always returns 200 (the process is up).

Two probes are registered automatically when the corresponding datasource is configured:

  • sql: acquires and releases a pooled connection (Postgres); SQLite is reported healthy once loaded.
  • redis: a PING round-trip.

Register your own check with app.addHealthCheck:

zig
try app.addHealthCheck("my-dep", myDepCheck);

fn myDepCheck(c: *zero.container) anyerror!void {
    // return normally when healthy; any error marks the component "down"
    if (!try c.myDep.ping()) return error.Unhealthy;
}

The handler receives the *container, so it can reach any registered resource (c.SQL, c.kv, c.pubsub, …) to validate it.

From 0.5.4, a background thread runs these checks and publishes a snapshot. The health endpoint reads the last published result instead of probing on every request. HEALTH_SNAPSHOT_REFRESH_MS sets the interval (default 5000). Set it to 0 to probe inline again. A hung check stays down without blocking the other checks.

The Redis probe also heals the shared connection: a failed PING triggers one reconnect before the component is marked down. The KV store and the Redis-backed rate limiter share that connection, so they recover with it.

At shutdown, the signal handler only sets a flag. The main thread then stops accepting requests, stops cron jobs, and drains in-flight requests for up to SHUTDOWN_DRAIN_TIMEOUT_MS (default 10000).