chadtmiller.com/monolith
An atproto PDS
0
0
PDS_BIND_ADDRESS: the server listens where it is told, 127.0.0.1 by default
crates/pds/src/main.rs+4−3
| @@ -11,9 +11,10 @@ | ||
static ALLOCATOR: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; | ||
| ||
const SERVE_HELP: &str = "Usage: pds serve\n\n\ | ||
Run the server, configured by its environment: PDS_PORT, PDS_DATA_DIR, PDS_HOSTNAME,\n\ | ||
PDS_PUBLIC_URL, PDS_HANDLE_DOMAINS, PDS_PLC_URL, PDS_PLC_ROTATION_KEY_HEX and,\n\ | ||
if set, PDS_RECOVERY_DID_KEY; PDS_EMAIL_SMTP_URL and PDS_EMAIL_FROM, both or\n\ | ||
Run the server, configured by its environment: PDS_PORT, PDS_BIND_ADDRESS\n\ | ||
(127.0.0.1 if unset), PDS_DATA_DIR, PDS_HOSTNAME, PDS_PUBLIC_URL,\n\ | ||
PDS_HANDLE_DOMAINS, PDS_PLC_URL, PDS_PLC_ROTATION_KEY_HEX and, if set,\n\ | ||
PDS_RECOVERY_DID_KEY; PDS_EMAIL_SMTP_URL and PDS_EMAIL_FROM, both or\n\ | ||
neither; PDS_DEV_MAIL_DIR, for development only; its log, metrics and traces\n\ | ||
by PDS_LOG_LEVEL, PDS_METRICS_PORT and the OTEL_* variables. SIGTERM or\n\ | ||
SIGINT shuts it down.\n"; | ||
crates/pds-server/src/lib.rs+13−2
| @@ -655,6 +655,17 @@ pub fn run() { | ||
.parse::<u16>() | ||
.unwrap_or_else(|_| fail("PDS_PORT is not a port".into())); | ||
let dir = var("PDS_DATA_DIR"); | ||
let address = match std::env::var("PDS_BIND_ADDRESS") | ||
.ok() | ||
.filter(|a| !a.is_empty()) | ||
{ | ||
None => std::net::IpAddr::from([127, 0, 0, 1]), | ||
Some(a) => a.parse().unwrap_or_else(|_| { | ||
fail(format!( | ||
"PDS_BIND_ADDRESS is {a:?}, not an IPv4 or IPv6 address" | ||
)) | ||
}), | ||
}; | ||
let config = Config::from_env().unwrap_or_else(|e| fail(e)); | ||
ops::start( | ||
ops::Settings::from_env(port).unwrap_or_else(|e| fail(e)), | ||
| @@ -671,9 +682,9 @@ pub fn run() { | ||
let stop = ops::signals(); | ||
ops::serve_beside(&svc).await.unwrap_or_else(|e| fail(e)); | ||
federation::tell_crawlers(&svc); | ||
let listener = tokio::net::TcpListener::bind(("127.0.0.1", port)) | ||
let listener = tokio::net::TcpListener::bind((address, port)) | ||
.await | ||
.unwrap_or_else(|e| fail(format!("listening on port {port}: {e}"))); | ||
.unwrap_or_else(|e| fail(format!("listening on {address} port {port}: {e}"))); | ||
// Handle changes in flight when the server stopped are settled while it serves. | ||
identity::settle_unfinished(&svc); | ||
blobs::sweep_every(&svc); | ||
harness/checks/operations.mjs+49−6
| @@ -3,14 +3,16 @@ | ||
// sent with no `traceparent`, a bad one or an unsampled one; the metrics | ||
// held to the log and to what the harness sees; every span held to the | ||
// request it is of; a shutdown, a start at another level with the PLC | ||
// directory failing, a start after a kill, with traces off, and starts with | ||
// variables that do not parse; and last the log (checks/logging.mjs). And, | ||
// in the middle of a run of random steps, the OTLP receiver hanging and | ||
// then gone, while the requests still answer. | ||
// directory failing, a start after a kill, with traces off, starts on other | ||
// addresses (spec/PDS.md), and starts with variables that do not parse; and | ||
// last the log (checks/logging.mjs). And, in the middle of a run of random | ||
// steps, the OTLP receiver hanging and then gone, while the requests still | ||
// answer. | ||
| ||
import { createHash } from 'node:crypto' | ||
import { createServer, request as httpRequest } from 'node:http' | ||
import { readFileSync, readdirSync, statSync } from 'node:fs' | ||
import { networkInterfaces } from 'node:os' | ||
import { join } from 'node:path' | ||
import { cborDecodeMulti } from '@atproto/common' | ||
import { LIMITS, ONLY, SEED, STEPS } from '../lib/args.mjs' | ||
| @@ -524,11 +526,49 @@ async function restart(changes) { | ||
await waitReady(server, cand.url) | ||
} | ||
| ||
/// The machine's first IPv4 address on an interface other than the | ||
/// loopback, or null; and whether it has IPv6's loopback. | ||
function addresses() { | ||
const all = Object.values(networkInterfaces()).flat() | ||
return { | ||
outside: all.find((a) => a.family === 'IPv4' && !a.internal)?.address ?? null, | ||
v6Loopback: all.some((a) => a.family === 'IPv6' && a.internal && a.address === '::1'), | ||
} | ||
} | ||
| ||
/// Whether the server answers `_health` at `host` on its port, within two | ||
/// seconds. | ||
const answers = (host) => | ||
fetch(`http://${host}:${server.env.PDS_PORT}${HEALTH}`, { signal: AbortSignal.timeout(2000) }).then( | ||
(r) => r.arrayBuffer().then(() => r.ok), | ||
() => false, | ||
) | ||
| ||
/// The server started with `PDS_BIND_ADDRESS` `address` (spec/PDS.md), | ||
/// answering on each host of `on` once it answers on the first, on none of | ||
/// `off`, then shut down. | ||
async function bound(keep, address, on, off) { | ||
Object.assign(server.env, keep, { PDS_BIND_ADDRESS: address }) | ||
server.start() | ||
let up = false | ||
for (let i = 0; i < 300 && !server.exited && !(up = await answers(on[0])); i++) await wait(100) | ||
const at = { PDS_BIND_ADDRESS: address } | ||
if (!up) await broke('listener', { ...at, host: on[0] }, 'an answer to _health', server.exited ?? 'none within 30 s') | ||
for (const host of on.slice(1)) if (!(await answers(host))) await broke('listener', { ...at, host }, 'an answer to _health', 'none') | ||
for (const host of off) if (await answers(host)) await broke('listener', { ...at, host }, 'no answer: it listens on that address alone', 'an answer') | ||
const stopped = await server.stop('SIGTERM', 15000) | ||
if (!stopped || stopped.code !== 0) await broke('shutdown', { signal: 'SIGTERM', ...at }, 'exit 0 within 15 s', stopped ?? 'still running') | ||
count(`listening on ${address}`, true) | ||
} | ||
| ||
/// A shutdown, then the server at `warn` with a failing PLC directory and | ||
/// the default service name; killed, then started with traces off; then | ||
/// starts with variables that do not parse. | ||
/// on other addresses; then starts with variables that do not parse. | ||
async function lifecycle() { | ||
const keep = { ...server.env } | ||
const { outside, v6Loopback } = addresses() | ||
// As the run started it, with no PDS_BIND_ADDRESS: on the loopback alone. | ||
if (outside && (await answers(outside))) await broke('listener', { PDS_BIND_ADDRESS: 'unset', host: outside }, 'no answer: it listens on 127.0.0.1 alone', 'an answer') | ||
const ended = await server.stop('SIGTERM', 15000) | ||
if (!ended || ended.code !== 0) await broke('shutdown', { signal: 'SIGTERM' }, 'exit 0 within 15 s', ended ?? 'still running') | ||
await checkSpans(now()) | ||
| @@ -576,7 +616,10 @@ async function lifecycle() { | ||
const stopped = await server.stop('SIGTERM', 15000) | ||
if (!stopped || stopped.code !== 0) await broke('shutdown', { signal: 'SIGTERM', traces: 'off' }, 'exit 0 within 15 s', stopped ?? 'still running') | ||
| ||
for (const bad of [{ PDS_LOG_LEVEL: 'loud' }, { OTEL_EXPORTER_OTLP_PROTOCOL: 'grpc' }, { OTEL_EXPORTER_OTLP_PROTOCOL: 'http/json' }, { PDS_METRICS_PORT: 'many' }, { PDS_METRICS_PORT: keep.PDS_PORT }]) { | ||
if (outside) await bound(keep, '0.0.0.0', [outside, '127.0.0.1'], []) | ||
if (v6Loopback) await bound(keep, '::1', ['[::1]'], ['127.0.0.1']) | ||
| ||
for (const bad of [{ PDS_LOG_LEVEL: 'loud' }, { OTEL_EXPORTER_OTLP_PROTOCOL: 'grpc' }, { OTEL_EXPORTER_OTLP_PROTOCOL: 'http/json' }, { PDS_METRICS_PORT: 'many' }, { PDS_METRICS_PORT: keep.PDS_PORT }, { PDS_BIND_ADDRESS: 'localhost' }, { PDS_BIND_ADDRESS: '[::1]' }]) { | ||
Object.assign(server.env, keep, bad) | ||
server.start() | ||
for (let i = 0; i < 100 && !server.exited; i++) await wait(100) | ||
harness/compare.mjs+1−1
| @@ -6,7 +6,7 @@ | ||
// The server is started with these in its environment (spec/PDS.md, | ||
// spec/ACCOUNTS.md), and with no accounts: | ||
// | ||
// PDS_PORT the port to listen on, on 127.0.0.1 | ||
// PDS_PORT the port to listen on, on 127.0.0.1 (PDS_BIND_ADDRESS unset) | ||
// PDS_DATA_DIR an empty directory to keep its data in | ||
// PDS_HOSTNAME localhost | ||
// PDS_PUBLIC_URL http://127.0.0.1:<port> | ||
spec/OPERATIONS.md+8−2
| @@ -390,9 +390,15 @@ is back at two thirds; the steps go on. Then, at the end: | ||
a `traceparent` in the request's trace, a `plc failure` line names the | ||
status and the DID, its `CLIENT` span is an error of | ||
`unknown_service:pds`, and an outbound error is counted. Killed, then | ||
started with no OTLP endpoint: no span. Then `PDS_LOG_LEVEL=loud`, the | ||
started with no OTLP endpoint: no span. Where the machine has an IPv4 | ||
address on an interface other than the loopback, the server started | ||
with `PDS_BIND_ADDRESS` unset does not answer `_health` on it, and | ||
started with `0.0.0.0` answers on it and on `127.0.0.1`; where the | ||
machine has IPv6's loopback, started with `::1` it answers on `[::1]` | ||
and not on `127.0.0.1` (`PDS.md`). Then `PDS_LOG_LEVEL=loud`, the | ||
protocols `grpc` and `http/json`, a `PDS_METRICS_PORT` that is no number | ||
and one equal to `PDS_PORT`: each exits 1 with why on standard error. | ||
and one equal to `PDS_PORT`, and a `PDS_BIND_ADDRESS` of `localhost` and | ||
of `[::1]`: each exits 1 with why on standard error. | ||
- **The log**, whole: every line one JSON object with the fields its event | ||
has, at no level below the start's `PDS_LOG_LEVEL`; each start's lines | ||
starting with `start`, with `recovered` after a start the last server did | ||
spec/PDS.md+18−4
| @@ -41,15 +41,29 @@ methods has a page of its own: | ||
| ||
`pds serve` runs the server (`COMMANDS.md`), configured by its environment: | ||
| ||
| Variable | Is | | ||
| --- | --- | | ||
| `PDS_PORT` | the port to listen on, on 127.0.0.1 | | ||
| `PDS_DATA_DIR` | a directory for its data: empty the first time, and the server's own after | | ||
| Variable | Is | Default | | ||
| --- | --- | --- | | ||
| `PDS_PORT` | the port to listen on | required | | ||
| `PDS_BIND_ADDRESS` | the address to listen on: an IPv4 or IPv6 address, as `0.0.0.0` for every IPv4 address of the machine, or `::` for every address an IPv6 socket is given, which on Linux and macOS is every IPv4 address too | `127.0.0.1` | | ||
| `PDS_DATA_DIR` | a directory for its data: empty the first time, and the server's own after | required | | ||
| ||
and the variables `ACCOUNTS.md`, `FEDERATION.md`, `LIMITS.md`, `EMAIL.md`, | ||
`PROXY.md`, `STORE.md` and `OPERATIONS.md` list, and, for development only, the one | ||
`OAUTH-CLIENTS.md` lists. It starts with no accounts. | ||
| ||
By default it listens on the loopback alone, so that only a reverse proxy | ||
on the same machine reaches it; `PDS_BIND_ADDRESS` puts it where a proxy | ||
on another machine, or a container's network, reaches it. It listens on | ||
that address and no other: bound to `::1`, it does not answer on | ||
`127.0.0.1`. `/metrics` stays on `127.0.0.1` whatever this says | ||
(`OPERATIONS.md`). An empty `PDS_BIND_ADDRESS` is as if unset. A value that | ||
is not an IPv4 or IPv6 address as written, such as `localhost`, a host | ||
name, `[::1]` or an address with a port, makes `pds serve` exit 1 before | ||
it writes anything to standard output, with why on standard error, as | ||
`LIMITS.md` says of its variables. An address it cannot listen on, as one | ||
no interface of the machine has, makes it exit 1 too, with why on | ||
standard error. | ||
| ||
It serves the methods whose lexicons are in `lexicons/`, as they define | ||
them, over XRPC as `atproto/xrpc.md` describes, and each one also as an MCP | ||
tool and a CLI command, as `COMMANDS.md` says: | ||
README.md+4−2
| @@ -165,6 +165,8 @@ server's starts. The `nightly` workflow runs them on `main`'s tip every | ||
night at 10:00 UTC, and when the owner asks for it | ||
(`knotless run rerun --ref main --workflow nightly`). | ||
| ||
The server listens on `127.0.0.1` unless `PDS_BIND_ADDRESS` names another | ||
address (`0.0.0.0` or `::` for a container's network; `spec/PDS.md`). | ||
Behind a reverse proxy, set `PDS_TRUSTED_PROXIES` to the proxy's addresses: | ||
the server reads `X-Forwarded-For` only from those peers, so without it | ||
every client shares the proxy's rate limits (`LIMITS.md`). | ||
| @@ -236,8 +238,8 @@ workflow run (`just code` runs them alone; `node gates.mjs code`, with | ||
| ||
## Known gaps | ||
| ||
- The server listens on 127.0.0.1. Accounts are made with `createAccount`, | ||
with handles under the server's own domains. | ||
- Accounts are made with `createAccount`, with handles under the server's | ||
own domains. | ||
- Of OAuth, no permission sets (`include:` scopes), account pages | ||
(`/account`), or sign-up from the authorization page. No account | ||
migration. The server | ||