Skip to content

Commit

Permalink
优化定时器
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Mar 23, 2024
1 parent 1d0689b commit 4b39eb5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
3 changes: 2 additions & 1 deletion flog/fileProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ func (r *fileLoggerPersistent) Log(LogLevel eumLogLevel.Enum, log *LogData, exce

// 将缓冲区的日志,每隔1秒,写入文件
func (r *fileLoggerPersistent) writeFile() {
for range time.NewTicker(time.Second * time.Duration(r.config.RefreshInterval)).C {
ticker := time.NewTicker(time.Second * time.Duration(r.config.RefreshInterval))
for range ticker.C {
// 组装要写入的日志内容
var logs []string
for len(r.logsBuffer) > 0 {
Expand Down
3 changes: 2 additions & 1 deletion flog/fopsProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func (r *fopsLoggerPersistent) Log(LogLevel eumLogLevel.Enum, log *LogData, exce

// 开启上传
func (r *fopsLoggerPersistent) enableUpload() {
for range time.NewTicker(3 * time.Second).C {
ticker := time.NewTicker(5 * time.Second)
for range ticker.C {
var lst []*LogData
// 当队列中有数据 且 取出的数量<1000时,则继续取出
for len(r.queue) > 0 && len(lst) < 1000 {
Expand Down
8 changes: 4 additions & 4 deletions fops/registerApp.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type RegisterAppRequest struct {
// 每隔3秒,上传当前应用信息
func register() {
if core.AppName == "fops" {
<-time.NewTicker(3 * time.Second).C
time.Sleep(5 * time.Second)
}
registerAppRequest := RegisterAppRequest{StartupAt: core.StartupAt, AppName: core.AppName, HostName: core.HostName, AppId: core.AppId, AppIp: core.AppIp, ProcessId: core.ProcessId}
for {
Expand All @@ -66,14 +66,14 @@ func register() {
apiRsp := core.NewApiResponseByReader[any](rsp.Body)
if apiRsp.StatusCode != 200 {
flog.Warningf("注册应用信息到FOPS失败(%v)%s", rsp.StatusCode, apiRsp.StatusMessage)
<-time.NewTicker(20 * time.Second).C
time.Sleep(20 * time.Second)
continue
}
} else {
flog.Warningf("注册应用信息到FOPS失败:%s", err.Error())
<-time.NewTicker(20 * time.Second).C
time.Sleep(20 * time.Second)
continue
}
<-time.NewTicker(20 * time.Second).C
time.Sleep(20 * time.Second)
}
}

0 comments on commit 4b39eb5

Please sign in to comment.