Pull request 2020: AG-26236-ring-buffer

Squashed commit of the following:

commit 4b9cc9ddf52739fc5f918babedc99ac7ac0e2415
Merge: a6259ed57 39aeaf891
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Oct 3 20:39:58 2023 +0300

    Merge branch 'master' into AG-26236-ring-buffer

commit a6259ed5758156e4110ee3ea6a49760d2880ade3
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Oct 3 20:30:20 2023 +0300

    querylog: imp code

commit 40f9f7cd5a1cff22bcb858020f2cfa9be8399671
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Oct 3 20:11:49 2023 +0300

    querylog: fix typo

commit 1aabbadcb5fcbe6a95945c5bd1455b956b85a8d0
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Oct 3 20:05:22 2023 +0300

    querylog: imp err msg

commit 02913d35b43e190e42765823ccfcdd332839e984
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Oct 3 19:37:47 2023 +0300

    aghalg: imp tests

commit 98a7909088d2a65b78afa9fa3113545c94429e65
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Oct 3 19:23:10 2023 +0300

    all: imp tests

commit e147804eeafe89e5020cd917784eef3ff2b310d0
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Oct 3 18:49:49 2023 +0300

    all: imp code

commit 5f21f2f63b7bce89f2bc79c97c7350cd693c956b
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Oct 3 14:39:43 2023 +0300

    all: add tests

commit 35a45c7dc5d5961f6987da7c69249c56e54e97f5
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Mon Oct 2 17:43:09 2023 +0300

    all: imp code

commit 21e51fcbe411258eaf830825df9d05b7ddcc187a
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Fri Sep 29 18:21:00 2023 +0300

    all: add ring buffer
This commit is contained in:
Stanislav Chzhen
2023-10-03 20:50:43 +03:00
parent 39aeaf8910
commit 4479b32ad4
7 changed files with 346 additions and 73 deletions

View File

@@ -7,6 +7,7 @@ import (
"sync"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
"github.com/AdguardTeam/golibs/errors"
@@ -29,12 +30,12 @@ type queryLog struct {
findClient func(ids []string) (c *Client, err error)
// logFile is the path to the log file.
logFile string
// buffer contains recent log entries. The entries in this buffer must not
// be modified.
buffer []*logEntry
buffer *aghalg.RingBuffer[*logEntry]
// logFile is the path to the log file.
logFile string
// bufferLock protects buffer.
bufferLock sync.RWMutex
@@ -195,7 +196,7 @@ func newLogEntry(params *AddParams) (entry *logEntry) {
// Add implements the [QueryLog] interface for *queryLog.
func (l *queryLog) Add(params *AddParams) {
var isEnabled, fileIsEnabled bool
var memSize uint32
var memSize int
func() {
l.confMu.RLock()
defer l.confMu.RUnlock()
@@ -204,7 +205,7 @@ func (l *queryLog) Add(params *AddParams) {
memSize = l.conf.MemSize
}()
if !isEnabled {
if !isEnabled || memSize == 0 {
return
}
@@ -221,36 +222,18 @@ func (l *queryLog) Add(params *AddParams) {
entry := newLogEntry(params)
needFlush := false
func() {
l.bufferLock.Lock()
defer l.bufferLock.Unlock()
l.bufferLock.Lock()
defer l.bufferLock.Unlock()
l.buffer = append(l.buffer, entry)
l.buffer.Append(entry)
if !fileIsEnabled {
if len(l.buffer) > int(memSize) {
// Writing to file is disabled, so just remove the oldest entry
// from the slices.
//
// TODO(a.garipov): This should be replaced by a proper ring
// buffer, but it's currently difficult to do that.
l.buffer[0] = nil
l.buffer = l.buffer[1:]
}
} else if !l.flushPending {
needFlush = len(l.buffer) >= int(memSize)
if needFlush {
l.flushPending = true
}
}
}()
if !l.flushPending && fileIsEnabled && l.buffer.Len() >= memSize {
l.flushPending = true
if needFlush {
go func() {
flushErr := l.flushLogBuffer()
if flushErr != nil {
log.Error("querylog: flushing after adding: %s", err)
log.Error("querylog: flushing after adding: %s", flushErr)
}
}()
}