diff --git a/internal/aghalg/ringbuffer.go b/internal/aghalg/ringbuffer.go index b97bcc67223..3faa8cb5fd5 100644 --- a/internal/aghalg/ringbuffer.go +++ b/internal/aghalg/ringbuffer.go @@ -1,23 +1,15 @@ package aghalg -import ( - "github.com/AdguardTeam/golibs/errors" -) - // RingBuffer is the implementation of ring buffer data structure. type RingBuffer[T any] struct { buf []T - cur int + cur uint full bool } // NewRingBuffer initializes the new instance of ring buffer. size must be // greater or equal to zero. -func NewRingBuffer[T any](size int) (rb *RingBuffer[T]) { - if size < 0 { - panic(errors.Error("ring buffer: size must be greater or equal to zero")) - } - +func NewRingBuffer[T any](size uint) (rb *RingBuffer[T]) { return &RingBuffer[T]{ buf: make([]T, size), } @@ -30,7 +22,7 @@ func (rb *RingBuffer[T]) Append(e T) { } rb.buf[rb.cur] = e - rb.cur = (rb.cur + 1) % cap(rb.buf) + rb.cur = (rb.cur + 1) % uint(cap(rb.buf)) if rb.cur == 0 { rb.full = true } @@ -87,12 +79,12 @@ func (rb *RingBuffer[T]) splitCur() (before, after []T) { } // Len returns a length of the buffer. -func (rb *RingBuffer[T]) Len() (l int) { +func (rb *RingBuffer[T]) Len() (l uint) { if !rb.full { return rb.cur } - return cap(rb.buf) + return uint(cap(rb.buf)) } // Clear clears the buffer. diff --git a/internal/aghalg/ringbuffer_test.go b/internal/aghalg/ringbuffer_test.go index 43fbb46b457..fc19ea2e347 100644 --- a/internal/aghalg/ringbuffer_test.go +++ b/internal/aghalg/ringbuffer_test.go @@ -36,25 +36,20 @@ func TestNewRingBuffer(t *testing.T) { for i := 0; i < 10; i++ { b.Append(i) } - assert.Equal(t, []int{5, 6, 7, 8, 9}, elements(b, b.Len(), false)) + assert.Equal(t, []int{5, 6, 7, 8, 9}, elements(b, int(b.Len()), false)) b.Clear() assert.Zero(t, b.Len()) }) - t.Run("negative_size", func(t *testing.T) { - assert.PanicsWithError(t, "ring buffer: size must be greater or equal to zero", func() { - aghalg.NewRingBuffer[int](-5) - }) - }) - t.Run("zero", func(t *testing.T) { b := aghalg.NewRingBuffer[int](0) for i := 0; i < 10; i++ { b.Append(i) - assert.Equal(t, 0, b.Len()) - assert.Empty(t, elements(b, b.Len(), false)) - assert.Empty(t, elements(b, b.Len(), true)) + bufLen := int(b.Len()) + assert.Equal(t, 0, bufLen) + assert.Empty(t, elements(b, bufLen, false)) + assert.Empty(t, elements(b, bufLen, true)) } }) @@ -62,9 +57,10 @@ func TestNewRingBuffer(t *testing.T) { b := aghalg.NewRingBuffer[int](1) for i := 0; i < 10; i++ { b.Append(i) - assert.Equal(t, 1, b.Len()) - assert.Equal(t, []int{i}, elements(b, b.Len(), false)) - assert.Equal(t, []int{i}, elements(b, b.Len(), true)) + bufLen := int(b.Len()) + assert.Equal(t, 1, bufLen) + assert.Equal(t, []int{i}, elements(b, bufLen, false)) + assert.Equal(t, []int{i}, elements(b, bufLen, true)) } }) } @@ -102,7 +98,7 @@ func TestRingBuffer_Range(t *testing.T) { b.Append(i) } - bufLen := b.Len() + bufLen := int(b.Len()) assert.Equal(t, tc.length, bufLen) want := tc.want @@ -163,11 +159,11 @@ func TestRingBuffer_Range_increment(t *testing.T) { for i, tc := range testCases { t.Run(tc.name, func(t *testing.T) { b.Append(i) - - assert.Equal(t, tc.want, elements(b, b.Len(), false)) + bufLen := int(b.Len()) + assert.Equal(t, tc.want, elements(b, bufLen, false)) slices.Reverse(tc.want) - assert.Equal(t, tc.want, elements(b, b.Len(), true)) + assert.Equal(t, tc.want, elements(b, bufLen, true)) }) } } diff --git a/internal/querylog/qlog.go b/internal/querylog/qlog.go index d34eb8efed8..73ac7e6ac69 100644 --- a/internal/querylog/qlog.go +++ b/internal/querylog/qlog.go @@ -196,13 +196,13 @@ 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 int + var memSize uint func() { l.confMu.RLock() defer l.confMu.RUnlock() isEnabled, fileIsEnabled = l.conf.Enabled, l.conf.FileEnabled - memSize = int(l.conf.MemSize) + memSize = l.conf.MemSize }() if !isEnabled { @@ -230,6 +230,7 @@ func (l *queryLog) Add(params *AddParams) { if !l.flushPending && fileIsEnabled && l.buffer.Len() >= memSize { l.flushPending = true + // TODO(s.chzhen): Fix occasional rewrite of entires. go func() { flushErr := l.flushLogBuffer() if flushErr != nil { diff --git a/internal/querylog/querylog.go b/internal/querylog/querylog.go index 69609f83979..189f1f57b02 100644 --- a/internal/querylog/querylog.go +++ b/internal/querylog/querylog.go @@ -143,10 +143,10 @@ func newQueryLog(conf Config) (l *queryLog, err error) { } } - memSize := int(conf.MemSize) + memSize := conf.MemSize if memSize == 0 { - // If query log is not diabled, we still need to write entries to a - // file. And all writing goes through a buffer. + // If query log is enabled, we still need to write entries to a file. + // And all writing goes through a buffer. memSize = 1 } diff --git a/internal/querylog/search.go b/internal/querylog/search.go index b4fd3ffe55a..a6b81d1d7e6 100644 --- a/internal/querylog/search.go +++ b/internal/querylog/search.go @@ -73,7 +73,7 @@ func (l *queryLog) searchMemory(params *searchParams, cache clientCache) (entrie return true }) - return entries, l.buffer.Len() + return entries, int(l.buffer.Len()) } // search - searches log entries in the query log using specified parameters