P3 Redirect relay: printed codes keep working when the main instance is offline #5

Closed
xpltd_admin wants to merge 0 commits from feature/p3-relay into main
Owner

Phase 3 of docs/PLAN.md: the external redirect relay.

What's in it

  • Same image: GLYPHLAB_ROLE=relay turns it into a relay with no database, no UI and no user data. It serves only short links, fallback pages, the domain-verification token and /healthz; any other host gets 421.
  • Sync is pull-based:
    • A signed snapshot at start and every 5 minutes.
    • The changes since the relay's last sequence number, every 10 seconds.
    • If the primary restarts (new epoch) or the relay falls behind the change log, the relay does a full resync.
    • Nothing ever has to connect in to the relay or to the home network.
  • Signed requests: HMAC-SHA256 over method, path, timestamp, relay id and a hash of the body, using GLYPHLAB_RELAY_SECRET (at least 32 characters). Requests older than 5 minutes are refused, and a tampered body is rejected. Without a secret configured, the relay endpoints don't exist.
  • Offline behavior:
    • The relay saves its last snapshot to disk, so it serves redirects after a restart even while the primary is down.
    • Scans are anonymized on the relay with the primary's visitor key, so the same device counts once across both.
    • Scans queue on disk until the primary accepts them. Batch ids make replays idempotent, and the queue is bounded.
  • Verification: the relay answers the reachability check. A short domain pointed at the relay verifies once the relay host is listed in GLYPHLAB_SHORT_DOMAIN_TARGETS.
  • Health: the Domains page shows each relay's last sync, when scans last arrived, and its queue depth. The relay's own /healthz reports sync state, last error and queue depth.
  • Example: docker-compose.relay.example.yml, plus the new settings in .env.example.

Decision that differs from the plan

  • Pull, not push. The plan had the primary push snapshot and deltas. Pulling every 10 s means destination changes reach the relay within about 10 s, and nothing needs to reach the relay from home or the reverse.

Verification

  • Unit and API tests: 143 pass, 10 of them new for the relay:
    • signatures: unsigned, wrong key, stale, tampered body;
    • endpoints off without a secret;
    • the relay follows destination, status and fallback changes;
    • a restart while the primary is down works from disk;
    • queued scans replay, including the returning visitor;
    • a replayed batch isn't double-counted;
    • visitor hashes match across primary and relay;
    • resync after an epoch change;
    • health reporting;
    • config validation.
  • Real containers on ub01 (primary + relay on a private network):
    • scan via relay → 302;
    • stop the primary → two more scans via the relay, both 302, relay reports queue depth 2;
    • start the primary → the relay resynced and drained its queue → the primary shows 3 scans from 1 device, and lists the relay with an empty queue.
    • Everything was torn down afterwards.

🤖 Generated with Claude Code

Phase 3 of `docs/PLAN.md`: the external redirect relay. ## What's in it - **Same image:** `GLYPHLAB_ROLE=relay` turns it into a relay with no database, no UI and no user data. It serves only short links, fallback pages, the domain-verification token and `/healthz`; any other host gets `421`. - **Sync is pull-based:** - A signed snapshot at start and every 5 minutes. - The changes since the relay's last sequence number, every 10 seconds. - If the primary restarts (new epoch) or the relay falls behind the change log, the relay does a full resync. - Nothing ever has to connect in to the relay or to the home network. - **Signed requests:** HMAC-SHA256 over method, path, timestamp, relay id and a hash of the body, using `GLYPHLAB_RELAY_SECRET` (at least 32 characters). Requests older than 5 minutes are refused, and a tampered body is rejected. Without a secret configured, the relay endpoints don't exist. - **Offline behavior:** - The relay saves its last snapshot to disk, so it serves redirects after a restart even while the primary is down. - Scans are anonymized on the relay with the primary's visitor key, so the same device counts once across both. - Scans queue on disk until the primary accepts them. Batch ids make replays idempotent, and the queue is bounded. - **Verification:** the relay answers the reachability check. A short domain pointed at the relay verifies once the relay host is listed in `GLYPHLAB_SHORT_DOMAIN_TARGETS`. - **Health:** the Domains page shows each relay's last sync, when scans last arrived, and its queue depth. The relay's own `/healthz` reports sync state, last error and queue depth. - **Example:** `docker-compose.relay.example.yml`, plus the new settings in `.env.example`. ## Decision that differs from the plan - **Pull, not push.** The plan had the primary push snapshot and deltas. Pulling every 10 s means destination changes reach the relay within about 10 s, and nothing needs to reach the relay from home or the reverse. ## Verification - **Unit and API tests:** 143 pass, 10 of them new for the relay: - signatures: unsigned, wrong key, stale, tampered body; - endpoints off without a secret; - the relay follows destination, status and fallback changes; - a restart while the primary is down works from disk; - queued scans replay, including the returning visitor; - a replayed batch isn't double-counted; - visitor hashes match across primary and relay; - resync after an epoch change; - health reporting; - config validation. - **Real containers on ub01** (primary + relay on a private network): - scan via relay → 302; - **stop the primary** → two more scans via the relay, both 302, relay reports queue depth 2; - start the primary → the relay resynced and drained its queue → the primary shows 3 scans from 1 device, and lists the relay with an empty queue. - Everything was torn down afterwards. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The same image runs as a relay on any small external host so printed codes keep
working while the main instance is offline. It has no database, UI or user data.

- The relay pulls: a signed snapshot at start and every 5 minutes, and the
  changes since its last sequence number every 10 seconds. Nothing has to
  connect in to the relay or to the primary's network, and a primary restart
  (new epoch) or a relay that fell behind triggers a full resync.
- Requests are HMAC-SHA256 signed over method, path, time, relay id and body
  with GLYPHLAB_RELAY_SECRET; older than 5 minutes is refused.
- The relay saves its last snapshot, so it serves redirects after a restart even
  when the primary is down. Scans are anonymized on the relay with the primary's
  visitor key (same device, same hash) and queued on disk until the primary
  accepts them; batch ids make replays idempotent.
- The relay answers the domain reachability check, so pointing a short domain at
  it (and listing it in GLYPHLAB_SHORT_DOMAIN_TARGETS) verifies normally.
- The Domains page shows each relay's last sync, last scans and queue depth.
Document the relay and its pull-based sync
All checks were successful
CI / check (pull_request) Successful in 1m37s
CI / docker (pull_request) Successful in 1m25s
4073b9d4e7
Author
Owner

CI green (check incl. e2e + benchmark, docker). Merged into main with a merge commit, pushed directly. Closing.

CI green (check incl. e2e + benchmark, docker). Merged into `main` with a merge commit, pushed directly. Closing.
xpltd_admin closed this pull request 2026-11-17 21:09:24 -07:00
xpltd_admin referenced this pull request from a commit 2026-11-17 21:09:25 -07:00
All checks were successful
CI / check (pull_request) Successful in 1m37s
CI / docker (pull_request) Successful in 1m25s

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
xpltdco/GlyphlabQR!5
No description provided.