style(backend): 精简 P1-1 期间的说明性注释
代码只留一行不变量。进程分工的动机、退避与重连的细节、测试可验证性的理由,都已在 docs/solutions/P1-1-async-workers-out-of-api.md 里写过,注释不再复述。 保留的是读代码看不出来的约束:超时派生自 poller 的 ctx、坏消息不能打断订阅、SSE 响应永不结束、订阅存活够久要重置退避。 文档补一句退避重置规则。这是删掉的注释里唯一没有落在文档上的细节。 验证:go test -count=1 ./... 全绿。
This commit is contained in:
@@ -11,8 +11,7 @@ import (
|
||||
"feedsystem_video_go/internal/notification"
|
||||
)
|
||||
|
||||
// notificationBusChannel 是跨进程推送通道。真实频道名由 cache.Key 加前缀,
|
||||
// 与其他键共用一套命名空间,同一 Redis 上的多套环境才不会串台。
|
||||
// 频道名经 cache.Key 加前缀,与环境内其他键共用命名空间。
|
||||
const notificationBusChannel = "notification:push"
|
||||
|
||||
const (
|
||||
@@ -21,18 +20,13 @@ const (
|
||||
notificationBusMaxBackoff = 30 * time.Second
|
||||
)
|
||||
|
||||
// notificationPushEnvelope 是线缆格式。它与 rabbitmq 包的 exchange/queue 常量同级:
|
||||
// 改动它等于改动跨进程协议,必须同时改生产端与消费端。
|
||||
// 跨进程线缆格式:改动它必须同时改生产端与消费端。
|
||||
type notificationPushEnvelope struct {
|
||||
UserID uint `json:"user_id"`
|
||||
Notification *notification.Notification `json:"notification"`
|
||||
}
|
||||
|
||||
// RedisNotificationPublisher 是 NotificationHub 在 Worker 侧的实现。
|
||||
//
|
||||
// Worker 进程不持有任何 SSE 连接,本地广播没有意义:通知交给 Redis,
|
||||
// 由每个 API 实例推给自己手里那些连接。这样"通知消费者在哪个进程"与
|
||||
// "用户连在哪个实例"彻底解耦,API 才能水平扩容。
|
||||
// RedisNotificationPublisher 把通知发给 Redis,由各 API 实例转给本地 SSE 连接。
|
||||
type RedisNotificationPublisher struct {
|
||||
cache *rediscache.Client
|
||||
}
|
||||
@@ -41,8 +35,7 @@ func NewRedisNotificationPublisher(cache *rediscache.Client) *RedisNotificationP
|
||||
return &RedisNotificationPublisher{cache: cache}
|
||||
}
|
||||
|
||||
// Push 尽力而为:通知已经落库,推送失败只影响"在线时立即看到",
|
||||
// 所以这里不重试,也不把错误抛回消费循环。
|
||||
// Push 尽力而为:通知已落库,失败只影响实时性。
|
||||
func (p *RedisNotificationPublisher) Push(userID uint, n *notification.Notification) {
|
||||
if p == nil || p.cache == nil || n == nil || userID == 0 {
|
||||
return
|
||||
@@ -54,7 +47,6 @@ func (p *RedisNotificationPublisher) Push(userID uint, n *notification.Notificat
|
||||
return
|
||||
}
|
||||
|
||||
// 独立超时:推送是"锦上添花",不允许它把消费循环挂住。
|
||||
ctx, cancel := context.WithTimeout(context.Background(), notificationPushTimeout)
|
||||
defer cancel()
|
||||
if err := p.cache.Publish(ctx, p.cache.Key(notificationBusChannel), payload); err != nil {
|
||||
@@ -63,9 +55,7 @@ func (p *RedisNotificationPublisher) Push(userID uint, n *notification.Notificat
|
||||
}
|
||||
}
|
||||
|
||||
// StartNotificationSubscriber 在 API 进程订阅推送通道,把通知转给本进程的 SSE 客户端。
|
||||
//
|
||||
// Redis 不可用时只记日志:SSE 连接与历史通知仍可用,实时推送降级为"刷新后可见"。
|
||||
// StartNotificationSubscriber 订阅推送通道并转给本地 SSE;Redis 不可用时只降级实时推送。
|
||||
func StartNotificationSubscriber(ctx context.Context, cache *rediscache.Client, hub NotificationHub) {
|
||||
if cache == nil || hub == nil {
|
||||
log.Printf("Notification push disabled: redis 或 hub 未初始化")
|
||||
@@ -80,8 +70,7 @@ func StartNotificationSubscriber(ctx context.Context, cache *rediscache.Client,
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
// 订阅维持过一段时间再断,说明是偶发断连而非配置错误:
|
||||
// 重置退避,避免 Redis 抖动一次后长期停在长退避上。
|
||||
// 存活够久说明是抖动而非配置错误:重置退避,避免长期停在长退避上。
|
||||
if time.Since(started) > notificationBusMaxBackoff {
|
||||
backoff = notificationBusMinBackoff
|
||||
}
|
||||
@@ -97,9 +86,7 @@ func StartNotificationSubscriber(ctx context.Context, cache *rediscache.Client,
|
||||
}()
|
||||
}
|
||||
|
||||
// runNotificationSubscriber 订阅并转发,直到 ctx 取消(返回 nil)或订阅断开(返回原因)。
|
||||
//
|
||||
// onSubscribed 可为 nil;测试用它确认订阅已生效再投递消息,避免用 sleep 猜时序。
|
||||
// runNotificationSubscriber 转发到 ctx 取消或订阅断开;onSubscribed 供测试确认订阅生效。
|
||||
func runNotificationSubscriber(ctx context.Context, cache *rediscache.Client, hub NotificationHub, onSubscribed func()) error {
|
||||
if cache == nil || hub == nil {
|
||||
return errors.New("notification subscriber: redis 或 hub 未初始化")
|
||||
|
||||
Reference in New Issue
Block a user