fix(logs): flush small access-log batches within two seconds

Default MinBatchSize of 50 left quiet admin traffic in memory
forever because MaxFlushWait was unset. Force a timed flush so
the logs page can show recent authenticated requests.
This commit is contained in:
ryan
2026-09-02 17:40:43 +08:00
parent bec1352ef7
commit 8aa0753b12
@@ -13,8 +13,12 @@ import (
"time" "time"
) )
// fallbackLogEngine 是日志库状态不可得时对外暴露的引擎标识。 const (
const fallbackLogEngine = "sqlite" // fallbackLogEngine 是日志库状态不可得时对外暴露的引擎标识。
fallbackLogEngine = "sqlite"
// accessLogMaxFlushWait 强制把不足 MinBatchSize 的访问日志刷盘,避免管理台低频访问永远看不到记录。
accessLogMaxFlushWait = 2 * time.Second
)
var ( var (
logWriterMu sync.RWMutex logWriterMu sync.RWMutex
@@ -30,6 +34,7 @@ func InitLogWriter(ctx context.Context) {
} }
cfg := batchwriter.DefaultConfig() cfg := batchwriter.DefaultConfig()
cfg.MaxFlushWait = accessLogMaxFlushWait
writer, err := batchwriter.New[*logstore.UserAccessLog](cfg, writeAccessLogBatch, writer, err := batchwriter.New[*logstore.UserAccessLog](cfg, writeAccessLogBatch,
batchwriter.WithDropHandler[*logstore.UserAccessLog](func(item *logstore.UserAccessLog) { batchwriter.WithDropHandler[*logstore.UserAccessLog](func(item *logstore.UserAccessLog) {
path := "" path := ""