fix(WS诊断): 优化报文批量写入与过期清理
延长数据库写入超时并按批次刷盘,降低数据库锁竞争导致的诊断记录失败;账号启动时清理七天前的 WS 报文,并补充批量写入与保留期测试。
This commit is contained in:
1 parent
2c6385374a
commit
090802e25d
3 files changed
+107
-12
No files matched your search
@@ -39,6 +39,10 @@ const (
|
||||
FrequentDisconnectLimit = 5
|
||||
FrequentDisconnectWindow = 5 * time.Minute
|
||||
TokenCaptchaFailureCooldown = 5 * time.Minute
|
||||
WSRecordBatchSize = 32
|
||||
WSRecordFlushInterval = 250 * time.Millisecond
|
||||
WSRecordWriteTimeout = 5 * time.Second
|
||||
WSRecordRetention = 7 * 24 * time.Hour
|
||||
|
||||
// ShortConnectionThreshold 仅用于统计频繁短连接;已经建立后的网络断线
|
||||
// 不会清 Token 缓存。
|
||||
@@ -559,17 +563,41 @@ func (a *Account) startWSRecorder(ctx context.Context) {
|
||||
}
|
||||
a.wsRecordOnce.Do(func() {
|
||||
go func() {
|
||||
cleanupCtx, cleanupCancel := context.WithTimeout(ctx, WSRecordWriteTimeout)
|
||||
deleted, cleanupErr := a.store.WSMessages.DeleteBefore(cleanupCtx, a.CookieID, time.Now().Add(-WSRecordRetention))
|
||||
cleanupCancel()
|
||||
if cleanupErr != nil && ctx.Err() == nil {
|
||||
a.logger.Warn("清理过期 WS 报文失败", "cookie_id", a.CookieID, "err", cleanupErr)
|
||||
} else if deleted > 0 {
|
||||
a.logger.Info("已清理过期 WS 报文", "cookie_id", a.CookieID, "deleted", deleted)
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(WSRecordFlushInterval)
|
||||
defer ticker.Stop()
|
||||
batch := make([]db.WSMessage, 0, WSRecordBatchSize)
|
||||
flush := func() {
|
||||
if len(batch) == 0 {
|
||||
return
|
||||
}
|
||||
writeCtx, cancel := context.WithTimeout(ctx, WSRecordWriteTimeout)
|
||||
err := a.store.WSMessages.AddBatch(writeCtx, batch)
|
||||
cancel()
|
||||
if err != nil && ctx.Err() == nil {
|
||||
a.logger.Warn("记录 WS 报文失败", "cookie_id", a.CookieID, "count", len(batch), "err", err)
|
||||
}
|
||||
batch = batch[:0]
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case message := <-a.wsRecordQueue:
|
||||
writeCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
||||
err := a.store.WSMessages.Add(writeCtx, message)
|
||||
cancel()
|
||||
if err != nil && ctx.Err() == nil {
|
||||
a.logger.Warn("记录 WS 报文失败", "cookie_id", a.CookieID, "err", err)
|
||||
batch = append(batch, message)
|
||||
if len(batch) >= WSRecordBatchSize {
|
||||
flush()
|
||||
}
|
||||
case <-ticker.C:
|
||||
flush()
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in new issue
Block a user