From 82140ab77775b32ea10d7cd011f2bdc2410f9b92 Mon Sep 17 00:00:00 2001 From: Dimitry Kolyshev Date: Tue, 6 Aug 2024 11:25:13 +0500 Subject: [PATCH] all: upd golibs --- go.mod | 4 +- go.sum | 8 +- internal/aghalg/ringbuffer.go | 94 ---------------- internal/aghalg/ringbuffer_test.go | 169 ----------------------------- internal/querylog/qlog.go | 6 +- internal/querylog/querylog.go | 4 +- 6 files changed, 11 insertions(+), 274 deletions(-) delete mode 100644 internal/aghalg/ringbuffer.go delete mode 100644 internal/aghalg/ringbuffer_test.go diff --git a/go.mod b/go.mod index 17f6cbf9..f95ffd50 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/AdguardTeam/AdGuardHome go 1.22.5 require ( - github.com/AdguardTeam/dnsproxy v0.72.2 - github.com/AdguardTeam/golibs v0.25.1 + github.com/AdguardTeam/dnsproxy v0.72.3-0.20240806061520-4cc9af6704ef + github.com/AdguardTeam/golibs v0.25.2 github.com/AdguardTeam/urlfilter v0.19.0 github.com/NYTimes/gziphandler v1.1.1 github.com/ameshkov/dnscrypt/v2 v2.3.0 diff --git a/go.sum b/go.sum index 71fa228b..09bf94b7 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ -github.com/AdguardTeam/dnsproxy v0.72.2 h1:0uItzXnUIuC9r+ZvPbNquGaAHvdWnWLbhSDdxsZk5og= -github.com/AdguardTeam/dnsproxy v0.72.2/go.mod h1:PA1UiTtTHMbXPv9NjHat+zrsgK8S7p/RJ+j/3tNqtUE= -github.com/AdguardTeam/golibs v0.25.1 h1:po5dBbFCoZAySsbsMN/ZRB0WTLYDA1d8BxPgvriu/EA= -github.com/AdguardTeam/golibs v0.25.1/go.mod h1:HaTyS2wCbxFudjht9N/+/Qf1b5cMad2BAYSwe7DPCXI= +github.com/AdguardTeam/dnsproxy v0.72.3-0.20240806061520-4cc9af6704ef h1:j6MtYGWnMOj5TdRFeLA/YmUuS4GKsatt1z3bmIJQtZ8= +github.com/AdguardTeam/dnsproxy v0.72.3-0.20240806061520-4cc9af6704ef/go.mod h1:DCHTuklaAuvCgtZzpoA0TrqCdOBZJQnKjgKxbC9/kQ8= +github.com/AdguardTeam/golibs v0.25.2 h1:4+c9LjAIdd9trRk71hXghJ5OL/VRosBm+/0dKH+H39U= +github.com/AdguardTeam/golibs v0.25.2/go.mod h1:HaTyS2wCbxFudjht9N/+/Qf1b5cMad2BAYSwe7DPCXI= github.com/AdguardTeam/urlfilter v0.19.0 h1:q7eH13+yNETlpD/VD3u5rLQOripcUdEktqZFy+KiQLk= github.com/AdguardTeam/urlfilter v0.19.0/go.mod h1:+N54ZvxqXYLnXuvpaUhK2exDQW+djZBRSb6F6j0rkBY= github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I= diff --git a/internal/aghalg/ringbuffer.go b/internal/aghalg/ringbuffer.go deleted file mode 100644 index 3faa8cb5..00000000 --- a/internal/aghalg/ringbuffer.go +++ /dev/null @@ -1,94 +0,0 @@ -package aghalg - -// RingBuffer is the implementation of ring buffer data structure. -type RingBuffer[T any] struct { - buf []T - 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 uint) (rb *RingBuffer[T]) { - return &RingBuffer[T]{ - buf: make([]T, size), - } -} - -// Append appends an element to the buffer. -func (rb *RingBuffer[T]) Append(e T) { - if len(rb.buf) == 0 { - return - } - - rb.buf[rb.cur] = e - rb.cur = (rb.cur + 1) % uint(cap(rb.buf)) - if rb.cur == 0 { - rb.full = true - } -} - -// Range calls cb for each element of the buffer. If cb returns false it stops. -func (rb *RingBuffer[T]) Range(cb func(T) (cont bool)) { - before, after := rb.splitCur() - - for _, e := range before { - if !cb(e) { - return - } - } - - for _, e := range after { - if !cb(e) { - return - } - } -} - -// ReverseRange calls cb for each element of the buffer in reverse order. If -// cb returns false it stops. -func (rb *RingBuffer[T]) ReverseRange(cb func(T) (cont bool)) { - before, after := rb.splitCur() - - for i := len(after) - 1; i >= 0; i-- { - if !cb(after[i]) { - return - } - } - - for i := len(before) - 1; i >= 0; i-- { - if !cb(before[i]) { - return - } - } -} - -// splitCur splits the buffer in two, before and after current position in -// chronological order. If buffer is not full, after is nil. -func (rb *RingBuffer[T]) splitCur() (before, after []T) { - if len(rb.buf) == 0 { - return nil, nil - } - - cur := rb.cur - if !rb.full { - return rb.buf[:cur], nil - } - - return rb.buf[cur:], rb.buf[:cur] -} - -// Len returns a length of the buffer. -func (rb *RingBuffer[T]) Len() (l uint) { - if !rb.full { - return rb.cur - } - - return uint(cap(rb.buf)) -} - -// Clear clears the buffer. -func (rb *RingBuffer[T]) Clear() { - rb.full = false - rb.cur = 0 -} diff --git a/internal/aghalg/ringbuffer_test.go b/internal/aghalg/ringbuffer_test.go deleted file mode 100644 index 31ae4d7b..00000000 --- a/internal/aghalg/ringbuffer_test.go +++ /dev/null @@ -1,169 +0,0 @@ -package aghalg_test - -import ( - "slices" - "testing" - - "github.com/AdguardTeam/AdGuardHome/internal/aghalg" - "github.com/stretchr/testify/assert" -) - -// elements is a helper function that returns n elements of the buffer. -func elements(b *aghalg.RingBuffer[int], n uint, reverse bool) (es []int) { - fn := b.Range - if reverse { - fn = b.ReverseRange - } - - var i uint - fn(func(e int) (cont bool) { - if i >= n { - return false - } - - es = append(es, e) - i++ - - return true - }) - - return es -} - -func TestNewRingBuffer(t *testing.T) { - t.Run("success_and_clear", func(t *testing.T) { - b := aghalg.NewRingBuffer[int](5) - for i := range 10 { - b.Append(i) - } - assert.Equal(t, []int{5, 6, 7, 8, 9}, elements(b, b.Len(), false)) - - b.Clear() - assert.Zero(t, b.Len()) - }) - - t.Run("zero", func(t *testing.T) { - b := aghalg.NewRingBuffer[int](0) - for i := range 10 { - b.Append(i) - bufLen := b.Len() - assert.EqualValues(t, 0, bufLen) - assert.Empty(t, elements(b, bufLen, false)) - assert.Empty(t, elements(b, bufLen, true)) - } - }) - - t.Run("single", func(t *testing.T) { - b := aghalg.NewRingBuffer[int](1) - for i := range 10 { - b.Append(i) - bufLen := b.Len() - assert.EqualValues(t, 1, bufLen) - assert.Equal(t, []int{i}, elements(b, bufLen, false)) - assert.Equal(t, []int{i}, elements(b, bufLen, true)) - } - }) -} - -func TestRingBuffer_Range(t *testing.T) { - const size = 5 - - b := aghalg.NewRingBuffer[int](size) - - testCases := []struct { - name string - want []int - count int - length uint - }{{ - name: "three", - count: 3, - length: 3, - want: []int{0, 1, 2}, - }, { - name: "ten", - count: 10, - length: size, - want: []int{5, 6, 7, 8, 9}, - }, { - name: "hundred", - count: 100, - length: size, - want: []int{95, 96, 97, 98, 99}, - }} - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - for i := range tc.count { - b.Append(i) - } - - bufLen := b.Len() - assert.Equal(t, tc.length, bufLen) - - want := tc.want - assert.Equal(t, want, elements(b, bufLen, false)) - assert.Equal(t, want[:len(want)-1], elements(b, bufLen-1, false)) - assert.Equal(t, want[:len(want)/2], elements(b, bufLen/2, false)) - - want = want[:cap(want)] - slices.Reverse(want) - - assert.Equal(t, want, elements(b, bufLen, true)) - assert.Equal(t, want[:len(want)-1], elements(b, bufLen-1, true)) - assert.Equal(t, want[:len(want)/2], elements(b, bufLen/2, true)) - }) - } -} - -func TestRingBuffer_Range_increment(t *testing.T) { - const size = 5 - - b := aghalg.NewRingBuffer[int](size) - - testCases := []struct { - name string - want []int - }{{ - name: "one", - want: []int{0}, - }, { - name: "two", - want: []int{0, 1}, - }, { - name: "three", - want: []int{0, 1, 2}, - }, { - name: "four", - want: []int{0, 1, 2, 3}, - }, { - name: "five", - want: []int{0, 1, 2, 3, 4}, - }, { - name: "six", - want: []int{1, 2, 3, 4, 5}, - }, { - name: "seven", - want: []int{2, 3, 4, 5, 6}, - }, { - name: "eight", - want: []int{3, 4, 5, 6, 7}, - }, { - name: "nine", - want: []int{4, 5, 6, 7, 8}, - }, { - name: "ten", - want: []int{5, 6, 7, 8, 9}, - }} - - for i, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - b.Append(i) - bufLen := b.Len() - assert.Equal(t, tc.want, elements(b, bufLen, false)) - - slices.Reverse(tc.want) - assert.Equal(t, tc.want, elements(b, bufLen, true)) - }) - } -} diff --git a/internal/querylog/qlog.go b/internal/querylog/qlog.go index 73ac7e6a..c0b2edd1 100644 --- a/internal/querylog/qlog.go +++ b/internal/querylog/qlog.go @@ -7,9 +7,9 @@ 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/container" "github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/timeutil" @@ -32,7 +32,7 @@ type queryLog struct { // buffer contains recent log entries. The entries in this buffer must not // be modified. - buffer *aghalg.RingBuffer[*logEntry] + buffer *container.RingBuffer[*logEntry] // logFile is the path to the log file. logFile string @@ -225,7 +225,7 @@ func (l *queryLog) Add(params *AddParams) { l.bufferLock.Lock() defer l.bufferLock.Unlock() - l.buffer.Append(entry) + l.buffer.Push(entry) if !l.flushPending && fileIsEnabled && l.buffer.Len() >= memSize { l.flushPending = true diff --git a/internal/querylog/querylog.go b/internal/querylog/querylog.go index 189f1f57..bccc264a 100644 --- a/internal/querylog/querylog.go +++ b/internal/querylog/querylog.go @@ -7,10 +7,10 @@ import ( "sync" "time" - "github.com/AdguardTeam/AdGuardHome/internal/aghalg" "github.com/AdguardTeam/AdGuardHome/internal/aghhttp" "github.com/AdguardTeam/AdGuardHome/internal/aghnet" "github.com/AdguardTeam/AdGuardHome/internal/filtering" + "github.com/AdguardTeam/golibs/container" "github.com/AdguardTeam/golibs/errors" "github.com/miekg/dns" ) @@ -153,7 +153,7 @@ func newQueryLog(conf Config) (l *queryLog, err error) { l = &queryLog{ findClient: findClient, - buffer: aghalg.NewRingBuffer[*logEntry](memSize), + buffer: container.NewRingBuffer[*logEntry](memSize), conf: &Config{}, confMu: &sync.RWMutex{},