fix(WS诊断): 优化报文批量写入与过期清理
延长数据库写入超时并按批次刷盘,降低数据库锁竞争导致的诊断记录失败;账号启动时清理七天前的 WS 报文,并补充批量写入与保留期测试。
This commit is contained in:
1 parent
2c6385374a
commit
090802e25d
3 files changed
+107
-12
No files matched your search
@@ -920,3 +920,39 @@ func TestWSMessages_Add(t *testing.T) {
|
||||
t.Fatalf("ws_messages 行数=%d want 2", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWSMessages_AddBatchAndDeleteBefore(t *testing.T) {
|
||||
s, cleanup := newTestDB(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
_, cid := seedAccount(t, s)
|
||||
|
||||
if err := s.WSMessages.AddBatch(ctx, []WSMessage{
|
||||
{CookieID: cid, RawText: "first"},
|
||||
{CookieID: cid, Direction: "out", ParseStatus: "parsed", RawText: "second"},
|
||||
}); err != nil {
|
||||
t.Fatalf("AddBatch: %v", err)
|
||||
}
|
||||
if _, err := s.DB.ExecContext(ctx,
|
||||
`UPDATE ws_messages SET created_at=? WHERE cookie_id=? AND raw_text=?`,
|
||||
time.Now().Add(-8*24*time.Hour), cid, "first"); err != nil {
|
||||
t.Fatalf("设置历史时间: %v", err)
|
||||
}
|
||||
|
||||
deleted, err := s.WSMessages.DeleteBefore(ctx, cid, time.Now().Add(-7*24*time.Hour))
|
||||
if err != nil {
|
||||
t.Fatalf("DeleteBefore: %v", err)
|
||||
}
|
||||
if deleted != 1 {
|
||||
t.Fatalf("删除行数=%d want 1", deleted)
|
||||
}
|
||||
var rawText, direction, parseStatus string
|
||||
if err := s.DB.QueryRowContext(ctx,
|
||||
`SELECT raw_text, direction, parse_status FROM ws_messages WHERE cookie_id=?`, cid,
|
||||
).Scan(&rawText, &direction, &parseStatus); err != nil {
|
||||
t.Fatalf("查询保留记录: %v", err)
|
||||
}
|
||||
if rawText != "second" || direction != "out" || parseStatus != "parsed" {
|
||||
t.Fatalf("保留记录=%q/%q/%q", rawText, direction, parseStatus)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package db
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// WSMessage 原始 WS 收包记录。
|
||||
@@ -21,14 +23,43 @@ type WSMessageStore struct{ DB *sql.DB }
|
||||
|
||||
// Add 记录一条 WS 消息。
|
||||
func (w *WSMessageStore) Add(ctx context.Context, m WSMessage) error {
|
||||
if m.Direction == "" {
|
||||
m.Direction = "in"
|
||||
return w.AddBatch(ctx, []WSMessage{m})
|
||||
}
|
||||
|
||||
// AddBatch 在一次数据库操作中记录多条 WS 消息。
|
||||
func (w *WSMessageStore) AddBatch(ctx context.Context, messages []WSMessage) error {
|
||||
if len(messages) == 0 {
|
||||
return nil
|
||||
}
|
||||
if m.ParseStatus == "" {
|
||||
m.ParseStatus = "raw"
|
||||
|
||||
var query strings.Builder
|
||||
query.WriteString("INSERT INTO ws_messages (cookie_id, direction, raw_text, parsed_json, message_kind, parse_status, error, created_at) VALUES ")
|
||||
args := make([]any, 0, len(messages)*7)
|
||||
for i, message := range messages {
|
||||
if i > 0 {
|
||||
query.WriteByte(',')
|
||||
}
|
||||
query.WriteString("(?,?,?,?,?,?,?,CURRENT_TIMESTAMP)")
|
||||
if message.Direction == "" {
|
||||
message.Direction = "in"
|
||||
}
|
||||
if message.ParseStatus == "" {
|
||||
message.ParseStatus = "raw"
|
||||
}
|
||||
args = append(args, message.CookieID, message.Direction, message.RawText, message.ParsedJSON,
|
||||
message.MessageKind, message.ParseStatus, message.Error)
|
||||
}
|
||||
_, err := w.DB.ExecContext(ctx,
|
||||
"INSERT INTO ws_messages (cookie_id, direction, raw_text, parsed_json, message_kind, parse_status, error, created_at) VALUES (?,?,?,?,?,?,?,CURRENT_TIMESTAMP)",
|
||||
m.CookieID, m.Direction, m.RawText, m.ParsedJSON, m.MessageKind, m.ParseStatus, m.Error)
|
||||
|
||||
_, err := w.DB.ExecContext(ctx, query.String(), args...)
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteBefore 删除指定账号在 cutoff 之前的 WS 诊断消息。
|
||||
func (w *WSMessageStore) DeleteBefore(ctx context.Context, cookieID string, cutoff time.Time) (int64, error) {
|
||||
result, err := w.DB.ExecContext(ctx,
|
||||
"DELETE FROM ws_messages WHERE cookie_id=? AND created_at < ?", cookieID, cutoff)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected()
|
||||
}
|
||||
Reference in new issue
Block a user