fix: 日志延迟+丢失问题 (#66)
This commit is contained in:
parent
7e910bf5fa
commit
08b54cfa90
|
@ -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)
|
||||||
|
defer timer.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case log := <-olc:
|
||||||
Logs = append(Logs, *log)
|
Logs = append(Logs, *log)
|
||||||
// 每10条记录到数据库
|
// 每10条记录到数据库
|
||||||
if len(Logs) > 5 {
|
if len(Logs) > 5 {
|
||||||
common.DB.Create(&Logs)
|
common.DB.Create(&Logs)
|
||||||
Logs = make([]model.OperationLog, 0)
|
Logs = make([]model.OperationLog, 0)
|
||||||
|
timer.Reset(duration) // 入库重置定时器
|
||||||
|
}
|
||||||
|
case <-timer.C: //5s 自动同步一次
|
||||||
|
common.DB.Create(&Logs)
|
||||||
|
Logs = make([]model.OperationLog, 0)
|
||||||
|
timer.Reset(duration) // 入库重置定时器
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue