fix: 日志延迟+丢失问题 (#66)

This commit is contained in:
Evan 2022-07-12 11:13:18 +08:00 committed by GitHub
parent 7e910bf5fa
commit 08b54cfa90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
"time"
"github.com/eryajf/go-ldap-admin/model" "github.com/eryajf/go-ldap-admin/model"
"github.com/eryajf/go-ldap-admin/model/request" "github.com/eryajf/go-ldap-admin/model/request"
@ -20,14 +21,25 @@ type OperationLogService struct{}
func (s OperationLogService) SaveOperationLogChannel(olc <-chan *model.OperationLog) { func (s OperationLogService) SaveOperationLogChannel(olc <-chan *model.OperationLog) {
// 只会在线程开启的时候执行一次 // 只会在线程开启的时候执行一次
Logs := make([]model.OperationLog, 0) Logs := make([]model.OperationLog, 0)
//5s 自动同步一次
// 一直执行--收到olc就会执行 duration := 5 * time.Second
for log := range olc { timer := time.NewTimer(duration)
Logs = append(Logs, *log) defer timer.Stop()
// 每10条记录到数据库 for {
if len(Logs) > 5 { select {
case log := <-olc:
Logs = append(Logs, *log)
// 每10条记录到数据库
if len(Logs) > 5 {
common.DB.Create(&Logs)
Logs = make([]model.OperationLog, 0)
timer.Reset(duration) // 入库重置定时器
}
case <-timer.C: //5s 自动同步一次
common.DB.Create(&Logs) common.DB.Create(&Logs)
Logs = make([]model.OperationLog, 0) Logs = make([]model.OperationLog, 0)
timer.Reset(duration) // 入库重置定时器
} }
} }
} }