P0-4 的写层基础:给每次"用户想发一条评论"一个稳定的意图键,并让判重 与写入成为同一条 SQL 的同一个事务。 - comments 加 idempotency_key,与 author_id 组成唯一索引 uk_comments_author_key。用 *string 而不是 string:MySQL 不约束 NULL, 多条无键评论才能共存;空串会互相撞键。 - CreateIdempotentWithPopularity 在一个事务里完成"插入评论 + 首次插入时 视频热度 +1"。重复写入静默返回 created=false,不报错、不覆盖、不再加 热度。判重交给数据库唯一索引,因此不存在"先 SELECT 再 INSERT"的窗口 ——那个窗口正是 MQ 假失败后兜底直写与 worker 稍后消费的交错点。 - DeleteByID 汇报是否真的删掉了一行,作为删除侧热度回退的门控点 (P2-20)。删除事件的重复投递与新增同样会把热度减两次。 - CreateMentions 按 dedupe_key 幂等写入提及通知,并显式写 CreatedAt (P2-16 ①);FindAccountIDs 用一次 username IN (?) 替换逐个查询。 - CommentStore 接口把"判重与写入必须同事务"这条约束变成显式的名字。 集成测试(-tags integration,需要 MYSQL_TEST_DSN)覆盖:重复插入报告 0 行且热度只加一次、8 并发只有 1 个 created、多条 NULL 键共存、提及通知 去重且 CreatedAt 非零、用户名批量解析。
80 lines
3.5 KiB
Go
80 lines
3.5 KiB
Go
package video
|
||
|
||
import (
|
||
"context"
|
||
"time"
|
||
)
|
||
|
||
// Mention 是一条"提及通知"的写入意图。
|
||
//
|
||
// DedupeKey 由 NotificationDedupeKey(notifType, intentKey, recipientID) 生成,
|
||
// 落到 notifications 表的唯一索引 uk_notifications_dedupe 上。它让"同一次
|
||
// 评论意图产生的提及通知"无论被写几次都只有一行——与评论本身用的是同一个
|
||
// 意图键,所以两侧的判重是同一个语义,而不是两套各自为政的计数器。
|
||
type Mention struct {
|
||
RecipientID uint
|
||
SenderID uint
|
||
TargetID uint
|
||
Content string
|
||
DedupeKey string
|
||
}
|
||
|
||
// NotificationRow 是 notifications 表在 video 包里的投影。
|
||
//
|
||
// 为什么不直接 import worker.Notification:worker 包 import 了 video
|
||
// (CommentWorker 依赖 CommentStore),反向 import 会成环。这张投影只需要
|
||
// "写一行通知"所需的字段,schema 的权威定义仍在 worker.Notification,
|
||
// 因此下面每个列标签都必须与它逐字一致——不一致时 AutoMigrate 会试图改动
|
||
// 生产列类型,那是本方案最不希望引入的意外。
|
||
type NotificationRow struct {
|
||
ID uint `gorm:"primaryKey"`
|
||
RecipientID uint `gorm:"index;not null"`
|
||
SenderID uint `gorm:"not null"`
|
||
Type string `gorm:"type:varchar(50);not null"`
|
||
TargetID uint `gorm:"column:target_id"`
|
||
Content string `gorm:"type:varchar(255)"`
|
||
IsRead bool `gorm:"default:false"`
|
||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||
|
||
// DedupeKey 与 worker.Notification.DedupeKey 指向同一个唯一索引
|
||
// uk_notifications_dedupe。为 nil 时不参与判重(MySQL 不约束 NULL),
|
||
// 这正是"没有意图键的旧路径"仍然能写入的原因。
|
||
DedupeKey *string `gorm:"column:dedupe_key;type:varchar(128);uniqueIndex:uk_notifications_dedupe"`
|
||
}
|
||
|
||
func (NotificationRow) TableName() string { return "notifications" }
|
||
|
||
// CommentStore 是 CommentService 与 CommentWorker 依赖的窄接口。
|
||
//
|
||
// 抽接口不是为了"面向接口编程",而是为了给"判重与写入必须发生在同一条 SQL
|
||
// 的同一个事务里"这条约束一个显式的名字:任何实现都必须提供
|
||
// CreateIdempotentWithPopularity,而不是先 Create 再补一条热度 UPDATE——
|
||
// 后者在两条语句之间崩溃就会留下"评论在、热度不在"的半成品。
|
||
type CommentStore interface {
|
||
// CreateIdempotentWithPopularity 在同一个事务里完成
|
||
// "插入评论(幂等)" + "热度增量(仅首次插入时)"。
|
||
// created=false 表示这条意图已经落过库,调用方不得再加热度。
|
||
CreateIdempotentWithPopularity(ctx context.Context, c *Comment, delta int64) (created bool, err error)
|
||
|
||
GetByID(ctx context.Context, id uint) (*Comment, error)
|
||
DeleteComment(ctx context.Context, comment *Comment) error
|
||
DeleteByID(ctx context.Context, id uint) (deleted bool, err error)
|
||
GetAllComments(ctx context.Context, videoID uint) ([]Comment, error)
|
||
|
||
// CreateMentions 按 DedupeKey 幂等写入提及通知。
|
||
CreateMentions(ctx context.Context, mentions []Mention) error
|
||
// FindAccountIDs 一次查询把用户名批量解析成账号 ID。
|
||
FindAccountIDs(ctx context.Context, usernames []string) (map[string]uint, error)
|
||
}
|
||
|
||
// nilIfEmpty 把空串转成 NULL。
|
||
//
|
||
// 用于 idempotency_key / dedupe_key 这类"可空唯一列":空串是一个真实的值,
|
||
// 多条空串会互相撞唯一索引;NULL 才是"没有这个键"。
|
||
func nilIfEmpty(s string) *string {
|
||
if s == "" {
|
||
return nil
|
||
}
|
||
return &s
|
||
}
|