docs: 同步进程职责与推送链路

AGENTS 改写 Dual Binary Split。Worker 是唯一异步进程,API 只做 HTTP 与 SSE 订阅。Workers 一节补充 SSEHub 的位置与 Redis 一跳,并禁止再用固定 sleep 当退出屏障。

README 新增进程分工表。API 可多副本,Worker 暂为单副本。表后说明 Redis 是通知实时性的必要条件,START_WORKER=0 时通知与 timeline 不再被消费。

两份设计文档同步消费者归属与 mq_topology 期望值:API 3,Worker 7。
This commit is contained in:
2026-11-08 14:44:41 +08:00
parent ffd33f981c
commit 239a98e1fa
4 changed files with 29 additions and 9 deletions

View File

@@ -62,7 +62,7 @@ CONFIG_PATH=configs/config.compose-local.yaml go run ./cmd/worker # Worker
### Dual Binary Split
- `backend/cmd/main.go` — Gin HTTP API server (port 8080, pprof on :6060)
- `backend/cmd/worker/main.go` — RabbitMQ consumer process (pprof on :6061). Runs the Social, Like, and Comment workers in independent goroutines; each gets its own AMQP channel from the shared connection manager and reconnects automatically. Interaction counts are written by the business transaction (`ApplyLike`/`ApplyUnlike`/`CreateIdempotentWithPopularity`/`DeleteWithPopularity`); the hot ranking is a derived 60-minute sliding window (`video.ApplyHotDelta`, best-effort after commit, rebuildable with `cmd/reconcile -rebuild-hot`). (Notification workers, the Outbox poller, and the timeline consumer currently live in the API process — see `router.go`.)
- `backend/cmd/worker/main.go` — the **only** async process (pprof on :6061). Runs every RabbitMQ consumer (Social, Like, Comment, the three Notification consumers, the parked-event consumer) plus the Outbox poller and the timeline consumer, each in its own goroutine with its own AMQP channel from the shared connection manager. Interaction counts are written by the business transaction (`ApplyLike`/`ApplyUnlike`/`CreateIdempotentWithPopularity`/`DeleteWithPopularity`); the hot ranking is a derived 60-minute sliding window (`video.ApplyHotDelta`, best-effort after commit, rebuildable with `cmd/reconcile -rebuild-hot`). The API process (`backend/cmd/main.go`) only serves HTTP: it writes Outbox rows and consumes Redis push messages for SSE — it never starts a consumer or a poller.
### Domain Layer Pattern
@@ -102,7 +102,11 @@ Three-tier: L1 in-process `go-cache` (3s) → L2 Redis JSON (1h) → L3 MySQL wi
### Workers
Consumers never touch `*amqp.Connection`. Each one runs `rabbitmq.RunConsumer(ctx, mgr, name, prefetch, fn)` — the single consume loop in the project. It blocks (no polling) while the connection is down, and on recovery rebuilds the channel, re-applies per-channel QoS, and re-runs `Consume`. Channel-level exceptions (queue deleted, 406 conflicts) trigger a topology replay rather than a reconnect. Topology is registered once (`RegisterTopology`) and replayed automatically on every new connection. `SSEHub` is an in-process pub/sub for real-time notification push to connected clients. Notification workers consume from `notification.like`, `notification.comment`, `notification.social` queues. Note: `notification` domain is handler-only — routes in `router.go`, worker-side logic in `worker/notificationworker.go`.
Consumers never touch `*amqp.Connection`. Each one runs `rabbitmq.RunConsumer(ctx, mgr, name, prefetch, fn)` — the single consume loop in the project. It blocks (no polling) while the connection is down, and on recovery rebuilds the channel, re-applies per-channel QoS, and re-runs `Consume`. Channel-level exceptions (queue deleted, 406 conflicts) trigger a topology replay rather than a reconnect. Topology is registered once (`RegisterTopology`) and replayed automatically on every new connection. Notification workers consume from `notification.like`, `notification.comment`, `notification.social` queues. Note: `notification` domain is handler-only — routes in `router.go`, worker-side logic in `worker/notificationworker.go`.
`SSEHub` (`worker/ssehub.go`) is the **API-side** in-process pub/sub for connected clients; it is not the delivery path. Delivery is cross-process: the Worker's `RedisNotificationPublisher` (`worker/notificationbus.go`) publishes `{user_id, notification}` to `v1:notification:push`, and every API instance's `StartNotificationSubscriber` forwards what it receives to its own hub. Without that hop a user connected to instance A never sees a notification produced on instance B. Pub/Sub is at-most-once — the `notifications` table stays the authoritative copy, and the SSE push only makes it appear without a refresh.
Both binaries own their shutdown: `signal.NotifyContext` in `cmd/main.go` and `cmd/worker/main.go`, `http.Server.Shutdown` for the API (SSE handlers return when the process ctx is cancelled) and a `WaitGroup` for the Worker. Never reintroduce a fixed `time.Sleep` as a shutdown barrier.
Inside that callback every business worker calls `rabbitmq.ConsumeLoop(ctx, ch, parker, queue, rabbitmq.MaxAttempts, process)`. It is the only place that decides Ack / retry / park: a `nil` error acks, `rabbitmq.Permanent(err)` parks immediately, and any other error goes back through the queue's own `<queue>.retry` delay queue until `MaxAttempts` is spent. Parking publishes to `dlx.events` with routing key `parked.<source queue>`, and `FailedEventWorker` persists those messages into the `failed_events` table. Retry counts come from the broker's `x-death` header, never from process memory. A worker dispatches on the message body (`action`) rather than `d.RoutingKey`: the retry hop rewrites the routing key to the queue name.
@@ -189,7 +193,7 @@ Host prerequisites: Git ≥ 2.34.1, **Node.js 22** (JavaScript actions such as `
- `backend/internal/http/router.go` — all route definitions and dependency wiring
- `backend/internal/middleware/rabbitmq/` — connection manager (reconnect + topology replay + `RunConsumer`), exchange declarations, DLX, per-domain MQ publishers
- `backend/internal/feed/service.go` — three-tier caching, hot/cold feed split
- `backend/internal/worker/` — all async workers and SSE hub
- `backend/internal/worker/` — all async workers, the SSE hub and the Redis notification bridge (`notificationbus.go`)
- `backend/internal/video/chunk_handler.go` — chunked upload logic
## Test-Driven Development (TDD)