refactor(p0-5): 收敛热度相关注释
背景叙述与历史取舍已写进 `docs/design/hot-ranking.md`,从源码删除。 只保留不变式与安全约束:`clientFoundRows` 禁令、唯一索引判重、 成员补零的排序约定、快照用 `RENAMENX` 只建一次、`EXPIRE` 失败即删键。 `video_service.go` 的 `NewVideoService` 注释原本复述已删方法的来历,一并收敛。 `zset.go` 的 `ZUnionStore` 与 `RenameNX` 注释各压到四行以内。 验证:go build ./... 通过。 验证:go vet ./... 通过。 验证:go test -count=1 ./... 全绿(本机无 cgo,未跑 -race)。 验证:go test -tags integration -p 1 -count=1 ./internal/feed/ ./internal/video/ ./internal/worker/ 全绿。
This commit is contained in:
@@ -74,8 +74,7 @@ func (w *CommentWorker) applyPublish(ctx context.Context, evt *rabbitmq.CommentE
|
||||
AuthorID: evt.AuthorID,
|
||||
Content: strings.TrimSpace(evt.Content),
|
||||
// 行时间来自事件:桶必须落在评论发生的那一分钟。
|
||||
// 事件没带时间时保持零值,由 GORM 的 autoCreateTime 补当前时刻,
|
||||
// 并回填到这个字段,因此下面的落桶仍与行一致。
|
||||
// 零值时由 GORM 的 autoCreateTime 补当前时刻并回填到这个字段。
|
||||
CreatedAt: evt.OccurredAt,
|
||||
}
|
||||
// event_id 即意图键:API 已直写时这里会命中唯一索引。
|
||||
|
||||
@@ -69,14 +69,12 @@ func (w *LikeWorker) applyLike(ctx context.Context, evt rabbitmq.LikeEvent) erro
|
||||
return nil
|
||||
}
|
||||
|
||||
// 行时间来自消息:积压的 worker 必须把增量记在事件发生的那一分钟,
|
||||
// 不能记在它被消费的那一分钟。旧的告警消息没有 occurred_at,退回当前时刻。
|
||||
// 行时间来自消息:积压的 worker 必须把增量记在事件发生的那一分钟。
|
||||
at := evt.OccurredAt
|
||||
if at.IsZero() {
|
||||
at = time.Now()
|
||||
}
|
||||
|
||||
// 插行与两个计数在同一个事务里,不再有"点赞成功、计数少 1"的中间态。
|
||||
created, err := w.likes.ApplyLike(ctx, &video.Like{
|
||||
VideoID: evt.VideoID,
|
||||
AccountID: evt.UserID,
|
||||
@@ -90,8 +88,7 @@ func (w *LikeWorker) applyLike(ctx context.Context, evt rabbitmq.LikeEvent) erro
|
||||
return nil
|
||||
}
|
||||
|
||||
// 热榜是派生数据。写失败不回滚点赞(I1 只看 DB),只留痕:
|
||||
// -verify 能发现这个桶偏少,-rebuild-hot 能把它建回来。
|
||||
// 热榜是派生数据:写失败不回滚点赞,-verify 能发现、-rebuild-hot 能补回。
|
||||
if err := video.ApplyHotDelta(ctx, w.cache, evt.VideoID, 1, at); err != nil {
|
||||
log.Printf("like worker: hot window update failed video=%d: %v", evt.VideoID, err)
|
||||
}
|
||||
@@ -116,7 +113,7 @@ func (w *LikeWorker) applyUnlike(ctx context.Context, evt rabbitmq.LikeEvent) er
|
||||
return nil
|
||||
}
|
||||
|
||||
// 减回点赞那一刻的桶:热榜是滑窗,按消费时刻减会留下一个永远偏高的旧桶。
|
||||
// 减回点赞那一刻的桶,不能按消费时刻减。
|
||||
if err := video.ApplyHotDelta(ctx, w.cache, evt.VideoID, -1, row.CreatedAt); err != nil {
|
||||
log.Printf("like worker: hot window update failed video=%d: %v", evt.VideoID, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user