Skip to content

Commit

Permalink
Pull request: 3823 fix rotation check
Browse files Browse the repository at this point in the history
Merge in DNS/adguard-home from 3823-log-rotation to master

Closes AdguardTeam#3823.

Squashed commit of the following:

commit 4075c69
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Nov 9 18:27:09 2021 +0500

    querylog: fix rotation check
  • Loading branch information
EugeneOne1 committed Nov 9, 2021
1 parent b0b2eb3 commit 6fd9e72
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions internal/querylog/querylogfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/timeutil"
)

// flushLogBuffer flushes the current buffer to file and resets the current buffer
Expand Down Expand Up @@ -136,36 +135,49 @@ func (l *queryLog) readFileFirstTimeValue() (first time.Time, err error) {
func (l *queryLog) periodicRotate() {
defer log.OnPanic("querylog: rotating")

rotations := time.NewTicker(1 * timeutil.Day)
l.checkAndRotate()

// rotationCheckIvl is the period of time between checking the need for
// rotating log files. It's smaller of any available rotation interval to
// increase time accuracy.
//
// See https://github.com/AdguardTeam/AdGuardHome/issues/3823.
const rotationCheckIvl = 1 * time.Hour

rotations := time.NewTicker(rotationCheckIvl)
defer rotations.Stop()

for range rotations.C {
oldest, err := l.readFileFirstTimeValue()
if err != nil && !errors.Is(err, os.ErrNotExist) {
log.Error("querylog: reading oldest record for rotation: %s", err)
l.checkAndRotate()
}
}

continue
}
// checkAndRotate rotates log files if those are older than the specified
// rotation interval.
func (l *queryLog) checkAndRotate() {
oldest, err := l.readFileFirstTimeValue()
if err != nil && !errors.Is(err, os.ErrNotExist) {
log.Error("querylog: reading oldest record for rotation: %s", err)

rot := oldest.Add(l.conf.RotationIvl)
now := time.Now()
if rot.After(time.Now()) {
log.Debug(
"querylog: %s <= %s, not rotating",
now.Format(time.RFC3339),
rot.Format(time.RFC3339),
)
return
}

continue
}
if rot, now := oldest.Add(l.conf.RotationIvl), time.Now(); rot.After(now) {
log.Debug(
"querylog: %s <= %s, not rotating",
now.Format(time.RFC3339),
rot.Format(time.RFC3339),
)

err = l.rotate()
if err != nil {
log.Error("querylog: rotating: %s", err)
return
}

continue
}
err = l.rotate()
if err != nil {
log.Error("querylog: rotating: %s", err)

log.Debug("querylog: rotated successfully")
return
}

log.Debug("querylog: rotated successfully")
}

0 comments on commit 6fd9e72

Please sign in to comment.