queries architecture

Rendered from the public repository's docs. Design decisions → · Code on GitHub

Current state of the queries service. Update this file in the same change as any code change that alters components, flows, limits or data (CI checks that every path mentioned here exists).

Context

Visitor

Reverse proxy

queries service

PostgreSQL

Slack

Owner

Visitors reach the service only through a reverse proxy; the service binds to loopback only. It posts queries to Slack and reads replies back over one outbound WebSocket, so it needs no public callback endpoint. The owner answers from inside Slack.

Components

PathResponsibility
cmd/server/main.goWiring (config, store, Slack clients, worker, HTTP server), graceful shutdown, healthcheck subcommand for the container
internal/config/config.goEnvironment variables, limits and defaults; secrets read from *_FILE paths
internal/server/server.goHTTP routes, multipart parsing and validation, CAPTCHA check, limits, CORS allow-list, request log
internal/server/ratelimit.goPer-IP limiter
internal/images/images.goContent sniffing, SVG rejection, JPEG metadata stripping
internal/queryid/queryid.goQuery ID generation and normalisation
internal/captcha/captcha.goServer-side CAPTCHA token verification (fails closed)
internal/slackx/slackx.goThin Slack client: post, upload, read thread, download, rate-limit errors, escaping
internal/worker/worker.goOutbox delivery to Slack with backoff, and inbound sync (events plus periodic reconcile)
internal/store/store.gopgx pool, embedded migrations, all SQL, outbox claiming, follow-up limits
DockerfileMulti-stage build, distroless, non-root

Creating a query and delivering it

SlackWorkerPostgreSQLServerBrowserloop[until delivered]POST /api/query (text,images, CAPTCHA token)per-IP limit,concurrency slot,validate, sniff imagesverify CAPTCHA (failclosed)daily cap check, then storequery, files and outbox statequery IDwakeclaim next pending query(lease, SKIP LOCKED)post root messagesave thread timestampimmediatelyupload images into the threadmark done (or record failureand back off)

The answer to the visitor does not wait for Slack, so a Slack outage never loses or blocks a query (ADR-0002).

Replies coming back

BrowserPostgreSQLWorkerSlackOwnerloop[every 5 minutes]reply in the threadevent (Socket Mode)re-read that threadstore new owner messages(unique Slack timestampmakes it idempotent)re-read threads from the last 7daysGET /api/query/{id} reads thethread

Events only trigger a re-read of that thread; messages not yet stored are added, and only messages from the configured owner are accepted (ADR-0003).

Data

hashashas

QUERIES

text

id

PK

text

ip_hash

text

body

text

slack_ts

bool

slack_done

int

slack_attempts

timestamptz

next_attempt_at

REPLIES

bigint

id

PK

text

query_id

FK

text

sender

text

body

text

slack_ts

bool

slack_done

FILES

uuid

id

PK

text

query_id

FK

bigint

reply_id

FK

text

mime

bytea

data

bool

slack_sent

Visitor follow-ups share the replies table with the owner's messages (sender distinguishes them) and use the same outbox columns. Migrations are embedded in the binary and applied on boot under an advisory lock, so concurrent starts are safe. Images are stored in the database, which keeps backups to a single database dump.

Limits (defaults in internal/config/config.go)

2000-character message, 3 images of at most 5 MiB, a daily cap on new queries, and for follow-ups: 30 per thread, 5 per 10 minutes, a daily cap across all threads. Per-IP limiters guard submit, follow-up and read endpoints, and a semaphore bounds concurrent submissions so memory stays bounded (ADR-0005).

Security and privacy

Testing strategy

Slack, CAPTCHA and the store are interfaces; tests use fakes, so go test -race ./... needs no network or secrets. Store tests are integration tests against a real PostgreSQL and skip unless TEST_DB_PORT is set (ADR-0006).