Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.

feat: deploy behind a gateway proxy instead of reconnecting shards - #133

Merged
rnxm merged 3 commits into
devfrom
feat/gateway-proxy
Nov 13, 2026
Merged

rnxm merged 3 commits into
devfrom
feat/gateway-proxy

Conversation

@rnxm

@rnxm rnxm commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Why

Restarting a bot with many guilds re-IDENTIFIES every shard, and Discord allows one IDENTIFY per 5 seconds (~2 min for 31k guilds). Any deploy, even a one-line change in args/ or helpers/, paid that cost.

The existing bot-side RESUME path (BOT_MODE=clustered only) could not fix this: after a RESUME Discord sends no READY or GUILD_CREATE, so a fresh process starts with bot.user = None and an empty guild cache.

What

  • Gateway proxy. Gelbpunkt/gateway-proxy holds the shard connections and, when the bot connects, replays a synthetic READY + GUILD_CREATEs from its cache. It runs as the gateway-proxy compose profile, using the upstream znver3 image pinned by digest, with its config in gateway-proxy.json and the token from TOKEN.
  • Bot. Setting DISCORD_GATEWAY_URL points discord.py's default gateway at the proxy, so re-identifies after an invalidated session also stay behind it. In that mode:
    • the bot skips discord.py's 5-second wait between shard IDENTIFYs;
    • SHARD_COUNT is required, and the bot refuses to start if it differs from the proxy's /shard-count;
    • the bot also fails fast if discord.py negotiates anything other than zlib-stream, which is the only compression the proxy produces.
  • Deploy. just deploy checks out the latest release tag (or just deploy <tag|branch>, e.g. main for beta) instead of origin/main, then builds and restarts only the default services (db lavalink redis bot), so it never touches the proxy. The new just deploy-proxy recreates the proxy; use it only for proxy upgrades and shard count changes.
  • Migrations. MIGRATE_ON_START=false skips the alembic upgrade head run on boot, because just deploy already migrates. The default (true) keeps self-hosting unchanged.
  • Removed. SessionPersister / RedisSessionPersister, the launch_shard RESUME override, the session save and close code 4000 in close(), and the IDENTIFY lock.
  • Docs. README section, .env.example, a GLOSSARY.md decision, and the AGENTS.md module index.

Notes for review

  • The proxy's cache.users is on. The proxy drops cached members whose user isn't cached, and without it guild.me would be missing after a replay.
  • intents in gateway-proxy.json (53608447) is discord.Intents.all(). The proxy identifies with its own intents, so the two must be kept in sync by hand.
  • Clustered mode without the proxy loses the cross-cluster IDENTIFY lock. Clusters cold-starting at the same time can collide on IDENTIFY again. Behind the proxy this doesn't apply.
  • Voice sessions don't survive a bot restart. The proxy keeps the bot's voice state on Discord's side, so after a restart the bot may still appear in a channel with no player. A clean leave on shutdown is a follow-up.
  • SHARD_COUNT compose default. It's now empty instead of 64, so the bot can tell whether it was set. ClusterConfig still falls back to 64.
  • Blue-green is a follow-up. The proxy sends each event to every client on a shard, so overlapping bots would both answer.

Test plan

  • ruff format --check, ruff check, ty check, pytest (424 passed)
  • Beta: start the proxy, connect the bot, confirm commands work and guild.me and the caches are populated
  • Beta: just deploy while running; measure the gap, and confirm in the proxy logs that no shard reconnects
  • Beta: just deploy-proxy; confirm the bot reconnects on its own

🤖 Generated with Claude Code

rnxm and others added 3 commits November 13, 2026 16:49
Production can now run Gelbpunkt's gateway-proxy, which holds the shard
connections and replays READY/GUILD_CREATE from its cache when the bot
connects. A deploy restarts only the bot and sends no IDENTIFY.

- DISCORD_GATEWAY_URL points discord.py at the proxy and skips the
  5-second IDENTIFY spacing; SHARD_COUNT is required and checked against
  the proxy's /shard-count at startup.
- The proxy is a compose service behind the gateway-proxy profile,
  pinned by digest; `just deploy` never touches it, `just deploy-proxy`
  recreates it.
- MIGRATE_ON_START=false skips the boot-time migration in production.
- Remove the bot-side RESUME machinery (SessionPersister, launch_shard
  override, session save on close, IDENTIFY lock): a fresh process that
  RESUMEs gets no READY or GUILD_CREATE and starts with an empty cache.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`just deploy` now checks out the latest v* release tag (prereleases
excluded), or the tag or branch passed to it, instead of resetting to
origin/main.

Restores the explicit SHARD_COUNT requirement behind the gateway proxy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rnxm
rnxm merged commit 3c73798 into dev Nov 13, 2026
6 checks passed
@rnxm
rnxm deleted the feat/gateway-proxy branch November 13, 2026 23:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant