fix(stability): 修复慢查询雪崩隐患、系统配置死锁与提号翻页错误

- 消除 system_configs 写锁争抢:使用读写锁与启动预热将默认配置初始化收敛为单例,杜绝读请求开事务导致的 Lock wait timeout exceeded
- 强化财务看板缓存:将 Redis 缓存有效期由 1 分钟延长为 5 分钟,接入 singleflight.Group 防并发击穿
- 修复提号列表 GORM 翻页 Bug:分离 Count 查询与列表查询构建,避免重复 Join 导致的 Error 1066
- 生产环境资源加固:docker-compose.prod.yml 中 MySQL 资源限制提升至 2.0 核 CPU、2G 内存
- 补充 systemconfig 并发测试与 pickup 分页 Count 回归测试
This commit is contained in:
yml2213
2026-09-07 19:45:11 +08:00
parent 0df5cebf5d
commit 33ee8408e9
10 changed files with 207 additions and 37 deletions
+29 -27
View File
@@ -893,44 +893,46 @@ func (r *Repository) ListAvailableListings(ctx context.Context, query AvailableL
Select("listing_id, MAX(id) AS upload_id").
Where("listing_id IS NOT NULL").
Group("listing_id")
db := r.db.WithContext(ctx).Table("rental_listings AS l").
Select(`l.id, l.listing_no, l.account_id, l.owner_id, l.price_cent AS listing_price_cent,
a.title AS account_title, a.server_region, a.login_platform, a.haf_coin_amount, a.asset_summary,
owner.phone AS owner_phone, l.handoff_mode, l.settlement_mode,
CASE WHEN lu.id IS NULL THEN 0 ELSE 1 END AS is_external_upload,
COALESCE(lu.source_channel, '') AS source_channel`).
Joins("JOIN game_accounts AS a ON a.id = l.account_id").
Joins("JOIN users AS owner ON owner.id = l.owner_id").
Joins("LEFT JOIN (?) AS latest_upload ON latest_upload.listing_id = l.id", latestUploads).
Joins("LEFT JOIN listing_uploads AS lu ON lu.id = latest_upload.upload_id").
Where("l.status = ? AND l.review_status = ? AND l.in_transaction = ?", "published", "approved", false)
if kw := strings.TrimSpace(query.Keyword); kw != "" {
upper := strings.ToUpper(kw)
if strings.HasPrefix(upper, "L") || (len(kw) >= 12 && isAllDigits(kw)) {
db = db.Where("l.listing_no = ?", kw)
} else {
like := "%" + kw + "%"
db = db.Where("l.listing_no LIKE ? OR a.title LIKE ?", like, like)
buildQuery := func() *gorm.DB {
q := r.db.WithContext(ctx).Table("rental_listings AS l").
Joins("JOIN game_accounts AS a ON a.id = l.account_id").
Joins("JOIN users AS owner ON owner.id = l.owner_id").
Joins("LEFT JOIN (?) AS latest_upload ON latest_upload.listing_id = l.id", latestUploads).
Joins("LEFT JOIN listing_uploads AS lu ON lu.id = latest_upload.upload_id").
Where("l.status = ? AND l.review_status = ? AND l.in_transaction = ?", "published", "approved", false)
if kw := strings.TrimSpace(query.Keyword); kw != "" {
upper := strings.ToUpper(kw)
if strings.HasPrefix(upper, "L") || (len(kw) >= 12 && isAllDigits(kw)) {
q = q.Where("l.listing_no = ?", kw)
} else {
like := "%" + kw + "%"
q = q.Where("l.listing_no LIKE ? OR a.title LIKE ?", like, like)
}
}
}
switch strings.TrimSpace(query.SourceType) {
case ListingSourceExternal:
db = db.Where("lu.id IS NOT NULL")
case ListingSourceInternal:
db = db.Where("lu.id IS NULL")
switch strings.TrimSpace(query.SourceType) {
case ListingSourceExternal:
q = q.Where("lu.id IS NOT NULL")
case ListingSourceInternal:
q = q.Where("lu.id IS NULL")
}
return q
}
page, pageSize := normalizePaging(query.Page, query.PageSize)
var rows []availableListingRow
if err := db.Order("l.id DESC").Offset((page - 1) * pageSize).Limit(pageSize).Scan(&rows).Error; err != nil {
if err := buildQuery().Select(`l.id, l.listing_no, l.account_id, l.owner_id, l.price_cent AS listing_price_cent,
a.title AS account_title, a.server_region, a.login_platform, a.haf_coin_amount, a.asset_summary,
owner.phone AS owner_phone, l.handoff_mode, l.settlement_mode,
CASE WHEN lu.id IS NULL THEN 0 ELSE 1 END AS is_external_upload,
COALESCE(lu.source_channel, '') AS source_channel`).
Order("l.id DESC").Offset((page - 1) * pageSize).Limit(pageSize).Scan(&rows).Error; err != nil {
return nil, err
}
var total int64
if page == 1 && len(rows) < pageSize {
total = int64(len(rows))
} else {
countDB := db.Session(&gorm.Session{}).Select("l.id")
if err := countDB.Count(&total).Error; err != nil {
if err := buildQuery().Count(&total).Error; err != nil {
return nil, err
}
}
@@ -283,6 +283,19 @@ func TestRepositoryListAvailableListingsFiltersAccountSource(t *testing.T) {
if internalResult.Total != 1 || len(internalResult.Items) != 1 {
t.Fatalf("站内上传结果 = total %d, items %d, want 1", internalResult.Total, len(internalResult.Items))
}
// 验证分页场景(len(rows) == pageSize 或 page > 1):触发独立 Count 查询,确保不会重复 Join 报错 Error 1066
pagedResult, err := repo.ListAvailableListings(t.Context(), AvailableListingQuery{
Page: 1,
PageSize: 1,
})
if err != nil {
t.Fatalf("分页查询触发 Count 失败: %v", err)
}
if pagedResult.Total != 2 || len(pagedResult.Items) != 1 {
t.Fatalf("分页结果 = total %d, items %d, want total 2, items 1", pagedResult.Total, len(pagedResult.Items))
}
internalItem := internalResult.Items[0]
if internalItem.ID != internal.ID || internalItem.IsExternalUpload || internalItem.AccountSource != AccountSourceInternal {
t.Fatalf("站内上传账号 = id %d, external %t, source %q", internalItem.ID, internalItem.IsExternalUpload, internalItem.AccountSource)