Skip to content

Run the API on a self-hosted VM: shared limiter and cooldown, arm64, Compose bundle - #23

Open
mtauha wants to merge 8 commits into
mainfrom
feat/oracle-self-host
Open

mtauha wants to merge 8 commits into
mainfrom
feat/oracle-self-host

Conversation

@mtauha

@mtauha mtauha commented Nov 25, 2026

Copy link
Copy Markdown
Member

Summary

Lets the API run several workers on one self-hosted VM with no per-process state.

  • Rate limiter (60/min per IP) counts in Redis (moving-window) when REDIS_URL is set. If Redis is down it falls back to per-process counting and never fails a request over the limiter. /health stays unlimited, as before.
  • PSX cooldown after a 429 is shared through Redis (psxdata:psx_cooldown, set with an expiry), using the same client as the /historical cache. Redis errors fall back to the in-process timer.
  • Dockerfile: --workers ${WEB_CONCURRENCY:-1} --proxy-headers. Defaults are unchanged where the variables are not set.
  • Docker image is built for linux/amd64 and linux/arm64.
  • deploy/oracle/: Compose bundle (api, redis, caddy), Caddyfile, .env.example, README. Fixed bridge subnet, Caddy at a fixed address that FORWARDED_ALLOW_IPS trusts alone; api and redis are not published to the host; log rotation 20m x 5.
  • deploy-oracle.yml: after "Publish Docker Image" succeeds (or manually, with an optional tag) copies the bundle over SSH, sets API_TAG, docker compose pull && up -d, and polls /health for about 2 minutes. Skips with a notice while ORACLE_SSH_HOST is unset.
  • fakeredis[lua] added to dev extras: the moving-window limiter uses Lua scripts, which fakeredis only runs with lupa.

Testing

  • pytest tests/api/: 261 passed. ruff check api/ and mypy api/ clean.
  • New tests: 61st request in a minute gets 429 on every limited route, per-IP isolation, /health unlimited, Redis-backed storage shared between limiter instances, fallback with Redis down; cooldown shared across two services, key expiry, fallback on Redis errors, factory wiring.
  • docker compose -f deploy/oracle/compose.yaml config validates (with API_DOMAIN and ACME_EMAIL set).
  • Workflow YAML parses; tag resolution checked locally for v0.8.1, branch names and empty input.

Not in this PR

  • Changing servers=[...] to api.psxdata.org (done at cutover).
  • A new log source for psx-log-exporter.
  • VM provisioning, DNS, and setting the repo secrets/variable.
  • The arm64 build and the Caddyfile have not been run end to end; the first publish and first deploy will exercise them.

The limiter now uses moving-window counting in REDIS_URL when set, falls back to
per-process counting if Redis is unreachable, and never fails a request over a limiter error.
Adds tests for the 61st-request 429 on every limited route.
After a PSX 429 the cooldown is written to psxdata:psx_cooldown with an expiry, so every
worker stops calling PSX, not just the one that was refused. If Redis errors, each process
falls back to its own timer.
WEB_CONCURRENCY sets the worker count (default 1, unchanged). --proxy-headers makes uvicorn
use X-Forwarded-For from the addresses listed in FORWARDED_ALLOW_IPS, which defaults to
127.0.0.1 so behaviour is unchanged where no proxy sets it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant