点赞与评论的计数只由业务事务写。ApplyLike、ApplyUnlike、CreateIdempotentWithPopularity 与 DeleteWithPopularity 各自在单事务内完成插行与计数。 热榜增量改为提交后的 best-effort 写。ApplyHotDelta 按行的 created_at 落分钟桶,写失败只记日志,DB 计数仍然正确。 删除 PopularityMQ 与 PopularityWorker。同一次互动因此只有一个写者,不再有两个门闩各自放行。 删除 hot:applied 占位键。判重改由 likes 的唯一索引与 uk_comments_author_key 负责。 DeleteWithPopularity 先读行再删行。评论删除因此回退热度,重复投递不再多减一次。 likes 与 comments 各加 (created_at, video_id) 覆盖索引,服务热榜窗口查询。 验证:go build -buildvcs=false ./... 通过。go test -buildvcs=false -count=1 ./... 全绿。
35 lines
1.3 KiB
Go
35 lines
1.3 KiB
Go
package video
|
||
|
||
import "time"
|
||
|
||
// Comment 的 (created_at, video_id) 覆盖索引 idx_comments_created_video
|
||
// 服务热榜窗口查询与 DB 回退排序:范围扫描走 created_at,video_id 留在
|
||
// 索引里避免回表。
|
||
type Comment struct {
|
||
ID uint `gorm:"primaryKey" json:"id"`
|
||
Username string `gorm:"index" json:"username"`
|
||
VideoID uint `gorm:"index;index:idx_comments_created_video,priority:2" json:"video_id"`
|
||
AuthorID uint `gorm:"index;uniqueIndex:uk_comments_author_key,priority:1" json:"author_id"`
|
||
Content string `gorm:"type:text" json:"content"`
|
||
CreatedAt time.Time `gorm:"autoCreateTime;index:idx_comments_created_video,priority:1" json:"created_at"`
|
||
|
||
// IdempotencyKey 必须是 *string:空串会互撞唯一索引,NULL 不会。
|
||
IdempotencyKey *string `gorm:"column:idempotency_key;type:varchar(64);uniqueIndex:uk_comments_author_key,priority:2" json:"-"`
|
||
}
|
||
|
||
type PublishCommentRequest struct {
|
||
VideoID uint `json:"video_id"`
|
||
Content string `json:"content"`
|
||
|
||
// RequestID 由客户端为一次发送动作生成,重试时复用;为空则服务端生成。
|
||
RequestID string `json:"request_id"`
|
||
}
|
||
|
||
type DeleteCommentRequest struct {
|
||
CommentID uint `json:"comment_id"`
|
||
}
|
||
|
||
type GetAllCommentsRequest struct {
|
||
VideoID uint `json:"video_id"`
|
||
}
|