style(backend): 清理幂等写入路径的注释
注释只保留一行级约束。 长段理由移入设计文档。 验证:go test -count=1 ./... 全绿。
This commit is contained in:
@@ -22,25 +22,9 @@ func (r *CommentRepository) CreateComment(ctx context.Context, comment *Comment)
|
||||
return r.db.WithContext(ctx).Create(comment).Error
|
||||
}
|
||||
|
||||
// CreateIdempotentWithPopularity 是 P0-4 的核心写入口。
|
||||
//
|
||||
// 它把两件必须同生共死的事放进同一个事务:
|
||||
//
|
||||
// 1. 插入评论——由 uk_comments_author_key(author_id, idempotency_key) 判重,
|
||||
// 重复时静默返回 created=false,不报错、不覆盖。
|
||||
// 2. 视频热度 +delta——只在真的插入了这一行时才执行。
|
||||
//
|
||||
// 为什么判重不能靠"先 SELECT 再 INSERT":两个 goroutine 之间永远有窗口,
|
||||
// 而这条窗口正好是 P0-4 的场景(MQ 假失败后兜底直写 + worker 稍后消费)。
|
||||
// 让数据库的唯一索引来裁决,判重与写入就变成同一个原子动作,没有窗口。
|
||||
//
|
||||
// 为什么不用 1062 判重:报错与去重是两种不同的语义,用报错表达去重会让
|
||||
// 每一条正常路径都经过一次 error 分支,也难以区分"真冲突"与"别的 1062"。
|
||||
// OnConflict DoNothing 直接给出 RowsAffected——它就是"我是否真的插入了"。
|
||||
//
|
||||
// 注意:本项目 DSN(internal/db/db.go)刻意没有 clientFoundRows。若有人
|
||||
// 加上它,MySQL 会在"行未变化"时也报告 1 行受影响,本函数就会把重复写入
|
||||
// 当成首次写入,热度变成 +2——判重静默失效。db 包与集成测试各有护栏。
|
||||
// CreateIdempotentWithPopularity 由 uk_comments_author_key 判重。
|
||||
// 热度增量与插入必须同事务,只在 created=true 时执行。
|
||||
// 注意:DSN 不能开 clientFoundRows,否则 RowsAffected 恒为 1,判重失效。
|
||||
func (r *CommentRepository) CreateIdempotentWithPopularity(ctx context.Context, c *Comment, delta int64) (bool, error) {
|
||||
var created bool
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
@@ -49,15 +33,12 @@ func (r *CommentRepository) CreateIdempotentWithPopularity(ctx context.Context,
|
||||
return res.Error
|
||||
}
|
||||
if res.RowsAffected == 0 {
|
||||
// 这条意图已经落过库:不是错误,也不再加热度。
|
||||
return nil
|
||||
}
|
||||
created = true
|
||||
if delta == 0 {
|
||||
return nil
|
||||
}
|
||||
// 与插入同一个事务:要么"评论与热度"一起生效,要么都不生效。
|
||||
// GREATEST(...,0) 防止热度被回滚到负数(与点赞侧一致)。
|
||||
return tx.Model(&Video{}).
|
||||
Where("id = ?", c.VideoID).
|
||||
UpdateColumn("popularity", gorm.Expr("GREATEST(popularity + ?, 0)", delta)).Error
|
||||
@@ -72,10 +53,7 @@ func (r *CommentRepository) DeleteComment(ctx context.Context, comment *Comment)
|
||||
return r.db.WithContext(ctx).Delete(comment).Error
|
||||
}
|
||||
|
||||
// DeleteByID 汇报"这次调用是否真的删掉了一行"。
|
||||
//
|
||||
// 删除的重复投递与新增的重复投递同样致命:把热度回退当成无条件动作,
|
||||
// 同一条删除事件投递两次就会把热度减两次(P2-20 的门控点就是这个返回值)。
|
||||
// DeleteByID 返回是否真的删除了一行,供调用方门控热度回退。
|
||||
func (r *CommentRepository) DeleteByID(ctx context.Context, id uint) (bool, error) {
|
||||
if id == 0 {
|
||||
return false, nil
|
||||
@@ -119,11 +97,6 @@ func (r *CommentRepository) GetByID(ctx context.Context, id uint) (*Comment, err
|
||||
return &comment, nil
|
||||
}
|
||||
|
||||
// CreateMentions 幂等写入提及通知:DedupeKey 相同则静默跳过。
|
||||
//
|
||||
// 显式写 CreatedAt 而不依赖 autoCreateTime:这一批行共享同一个时间戳,便于
|
||||
// 按"同一次评论"检索;autoCreateTime 只在字段为零值时才生效,依赖它会让
|
||||
// 同一批写入拿到毫秒级不同的时间(P2-16 ①)。
|
||||
func (r *CommentRepository) CreateMentions(ctx context.Context, mentions []Mention) error {
|
||||
if len(mentions) == 0 {
|
||||
return nil
|
||||
@@ -146,11 +119,7 @@ func (r *CommentRepository) CreateMentions(ctx context.Context, mentions []Menti
|
||||
Create(&rows).Error
|
||||
}
|
||||
|
||||
// FindAccountIDs 一次查询解析一批用户名。
|
||||
//
|
||||
// 逐个 SELECT 会让"一条评论里 @ 了 5 个人"变成 5 次往返,而这段代码跑在
|
||||
// 用户请求路径上(P0-4 §6.5)。不存在的用户名不出现在结果里,调用方按
|
||||
// map 的不存在分支跳过即可。
|
||||
// FindAccountIDs 一次查询解析一批用户名;不存在的用户名不出现在结果里。
|
||||
func (r *CommentRepository) FindAccountIDs(ctx context.Context, usernames []string) (map[string]uint, error) {
|
||||
out := make(map[string]uint, len(usernames))
|
||||
if len(usernames) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user