mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 04:42:33 +02:00
improved access log flushing
This commit is contained in:
parent
6b669fc540
commit
112859caa5
1 changed files with 12 additions and 3 deletions
|
@ -121,11 +121,15 @@ func (l *AccessLogger) Rotate() error {
|
|||
}
|
||||
|
||||
func (l *AccessLogger) Flush(force bool) {
|
||||
if l.buf.Len() == 0 {
|
||||
return
|
||||
}
|
||||
if force || l.buf.Len() >= l.flushThreshold {
|
||||
l.bufMu.Lock()
|
||||
l.write(l.buf.Bytes())
|
||||
l.buf.Reset()
|
||||
l.bufMu.Unlock()
|
||||
logger.Debug().Msg("access log flushed to " + l.io.Name())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -142,14 +146,19 @@ func (l *AccessLogger) start() {
|
|||
l.task.Finish(nil)
|
||||
}()
|
||||
|
||||
// threshold flush with periodic check
|
||||
flushTicker := time.NewTicker(time.Second)
|
||||
// periodic flush + threshold flush
|
||||
periodic := time.NewTicker(5 * time.Second)
|
||||
threshold := time.NewTicker(time.Second)
|
||||
defer periodic.Stop()
|
||||
defer threshold.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-l.task.Context().Done():
|
||||
return
|
||||
case <-flushTicker.C:
|
||||
case <-periodic.C:
|
||||
l.Flush(true)
|
||||
case <-threshold.C:
|
||||
l.Flush(false)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue