fix(feed): 热榜回退跳过整页已删视频

`DeleteVideo` 不级联删互动行。整页视频都被删掉时,旧代码返回空列表。`has_more` 仍为 true,客户端会翻到一个空页。

`listByHotWindowDB` 改为按 SQL 行推进的扫批循环。整批全删且本批满页时,游标前进 `len(rows)` 继续取。上限 `hotDBMaxSweep = 5` 批,避免一次请求变成几十次查询。`next_offset` 仍是 `offset + 已消费行数`,被跳过的行也计入。

补四条测试。整页已删时不重不漏并能终止。扫满上限后收尾。非整分钟 `as_of` 截断后采纳。

验证:go test -count=1 ./... 全绿
This commit is contained in:
2026-11-01 18:14:21 +08:00
parent d782ccbb53
commit 41b212dd65
2 changed files with 184 additions and 36 deletions

View File

@@ -594,24 +594,55 @@ func parseHotMembers(members []string) []uint {
return ids
}
// hotDBMaxSweep 限制一次翻页最多扫几批:窗口里全是已删视频时,无上限的扫描
// 会把一次请求变成几十次查询。
const hotDBMaxSweep = 5
func (f *FeedService) listByHotWindowDB(ctx context.Context, limit, offset int, asOf time.Time, viewer uint) (ListByPopularityResponse, error) {
from, to := hotWindowBounds(asOf)
rows, err := f.repo.ListHotWindow(ctx, from, to, limit, offset)
if err != nil {
return ListByPopularityResponse{}, err
// 整页都被 GetByIDs 过滤掉时按 SQL 行推进游标继续取,直到凑到视频或取到不满一页。
var (
consumed int
ordered []*video.Video
more bool
)
for sweep := 0; sweep < hotDBMaxSweep; sweep++ {
rows, err := f.repo.ListHotWindow(ctx, from, to, limit, offset+consumed)
if err != nil {
return ListByPopularityResponse{}, err
}
if len(rows) == 0 {
more = false
break
}
consumed += len(rows)
more = len(rows) == limit
ids := make([]uint, 0, len(rows))
for _, r := range rows {
ids = append(ids, r.VideoID)
}
videos, err := f.repo.GetByIDs(ctx, ids)
if err != nil {
return ListByPopularityResponse{}, err
}
byID := make(map[uint]*video.Video, len(videos))
for _, v := range videos {
byID[v.ID] = v
}
for _, id := range ids {
if v := byID[id]; v != nil {
ordered = append(ordered, v)
}
}
if len(ordered) > 0 || !more {
break
}
}
if len(rows) == 0 && offset > 0 {
return ListByPopularityResponse{
VideoList: []FeedVideoItem{},
AsOf: asOf.Unix(),
NextOffset: offset,
HasMore: false,
RankingSource: "hot_db",
}, nil
}
if len(rows) == 0 {
// 窗口内没有任何互动:给"最新",而不是给一个空页面。
if consumed == 0 && offset == 0 {
// 窗口内没有互动:给"最新",不给空页面。
latest, err := f.repo.ListLatest(ctx, limit, time.Time{})
if err != nil {
return ListByPopularityResponse{}, err
@@ -622,38 +653,30 @@ func (f *FeedService) listByHotWindowDB(ctx context.Context, limit, offset int,
}
return ListByPopularityResponse{
VideoList: items, AsOf: asOf.Unix(),
// 这一支是"兜底给点东西看",不是可分页的列表:ListLatest 不收 offset,
// 若这里回一个 next_offset,第 2 页会落到 ListHotWindow 的空结果上并返回空页。
// ListLatest 不收 offset,回一个 next_offset 会让第 2 页落到空热榜上。
NextOffset: 0, HasMore: false, RankingSource: "latest",
}, nil
}
if len(ordered) == 0 {
// 翻到末尾,或连续几批只剩已删视频:终结页。
return ListByPopularityResponse{
VideoList: []FeedVideoItem{},
AsOf: asOf.Unix(),
NextOffset: offset + consumed,
HasMore: false,
RankingSource: "hot_db",
}, nil
}
ids := make([]uint, 0, len(rows))
for _, r := range rows {
ids = append(ids, r.VideoID)
}
videos, err := f.repo.GetByIDs(ctx, ids)
if err != nil {
return ListByPopularityResponse{}, err
}
byID := make(map[uint]*video.Video, len(videos))
for _, v := range videos {
byID[v.ID] = v
}
ordered := make([]*video.Video, 0, len(ids))
for _, id := range ids {
if v := byID[id]; v != nil {
ordered = append(ordered, v)
}
}
items, err := f.buildFeedVideos(ctx, ordered, viewer)
if err != nil {
return ListByPopularityResponse{}, err
}
return ListByPopularityResponse{
VideoList: items, AsOf: asOf.Unix(),
NextOffset: offset + len(rows),
HasMore: len(rows) == limit,
// 游标按 SQL 行推进(含被跳过的已删视频),否则第 2 页会重复读到已消费的位置。
NextOffset: offset + consumed,
HasMore: more,
RankingSource: "hot_db",
}, nil
}