mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-23 21:45:46 +03:00
Pull request: AGDNS-2342-upd-golibs
Squashed commit of the following:
commit 82140ab777
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date: Tue Aug 6 11:25:13 2024 +0500
all: upd golibs
This commit is contained in:
parent
edfa8c147f
commit
1a6ec30bd7
6 changed files with 11 additions and 274 deletions
4
go.mod
4
go.mod
|
@ -3,8 +3,8 @@ module github.com/AdguardTeam/AdGuardHome
|
||||||
go 1.22.5
|
go 1.22.5
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/AdguardTeam/dnsproxy v0.72.2
|
github.com/AdguardTeam/dnsproxy v0.72.3-0.20240806061520-4cc9af6704ef
|
||||||
github.com/AdguardTeam/golibs v0.25.1
|
github.com/AdguardTeam/golibs v0.25.2
|
||||||
github.com/AdguardTeam/urlfilter v0.19.0
|
github.com/AdguardTeam/urlfilter v0.19.0
|
||||||
github.com/NYTimes/gziphandler v1.1.1
|
github.com/NYTimes/gziphandler v1.1.1
|
||||||
github.com/ameshkov/dnscrypt/v2 v2.3.0
|
github.com/ameshkov/dnscrypt/v2 v2.3.0
|
||||||
|
|
8
go.sum
8
go.sum
|
@ -1,7 +1,7 @@
|
||||||
github.com/AdguardTeam/dnsproxy v0.72.2 h1:0uItzXnUIuC9r+ZvPbNquGaAHvdWnWLbhSDdxsZk5og=
|
github.com/AdguardTeam/dnsproxy v0.72.3-0.20240806061520-4cc9af6704ef h1:j6MtYGWnMOj5TdRFeLA/YmUuS4GKsatt1z3bmIJQtZ8=
|
||||||
github.com/AdguardTeam/dnsproxy v0.72.2/go.mod h1:PA1UiTtTHMbXPv9NjHat+zrsgK8S7p/RJ+j/3tNqtUE=
|
github.com/AdguardTeam/dnsproxy v0.72.3-0.20240806061520-4cc9af6704ef/go.mod h1:DCHTuklaAuvCgtZzpoA0TrqCdOBZJQnKjgKxbC9/kQ8=
|
||||||
github.com/AdguardTeam/golibs v0.25.1 h1:po5dBbFCoZAySsbsMN/ZRB0WTLYDA1d8BxPgvriu/EA=
|
github.com/AdguardTeam/golibs v0.25.2 h1:4+c9LjAIdd9trRk71hXghJ5OL/VRosBm+/0dKH+H39U=
|
||||||
github.com/AdguardTeam/golibs v0.25.1/go.mod h1:HaTyS2wCbxFudjht9N/+/Qf1b5cMad2BAYSwe7DPCXI=
|
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 h1:q7eH13+yNETlpD/VD3u5rLQOripcUdEktqZFy+KiQLk=
|
||||||
github.com/AdguardTeam/urlfilter v0.19.0/go.mod h1:+N54ZvxqXYLnXuvpaUhK2exDQW+djZBRSb6F6j0rkBY=
|
github.com/AdguardTeam/urlfilter v0.19.0/go.mod h1:+N54ZvxqXYLnXuvpaUhK2exDQW+djZBRSb6F6j0rkBY=
|
||||||
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=
|
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
|
@ -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))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -7,9 +7,9 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
||||||
|
"github.com/AdguardTeam/golibs/container"
|
||||||
"github.com/AdguardTeam/golibs/errors"
|
"github.com/AdguardTeam/golibs/errors"
|
||||||
"github.com/AdguardTeam/golibs/log"
|
"github.com/AdguardTeam/golibs/log"
|
||||||
"github.com/AdguardTeam/golibs/timeutil"
|
"github.com/AdguardTeam/golibs/timeutil"
|
||||||
|
@ -32,7 +32,7 @@ type queryLog struct {
|
||||||
|
|
||||||
// buffer contains recent log entries. The entries in this buffer must not
|
// buffer contains recent log entries. The entries in this buffer must not
|
||||||
// be modified.
|
// be modified.
|
||||||
buffer *aghalg.RingBuffer[*logEntry]
|
buffer *container.RingBuffer[*logEntry]
|
||||||
|
|
||||||
// logFile is the path to the log file.
|
// logFile is the path to the log file.
|
||||||
logFile string
|
logFile string
|
||||||
|
@ -225,7 +225,7 @@ func (l *queryLog) Add(params *AddParams) {
|
||||||
l.bufferLock.Lock()
|
l.bufferLock.Lock()
|
||||||
defer l.bufferLock.Unlock()
|
defer l.bufferLock.Unlock()
|
||||||
|
|
||||||
l.buffer.Append(entry)
|
l.buffer.Push(entry)
|
||||||
|
|
||||||
if !l.flushPending && fileIsEnabled && l.buffer.Len() >= memSize {
|
if !l.flushPending && fileIsEnabled && l.buffer.Len() >= memSize {
|
||||||
l.flushPending = true
|
l.flushPending = true
|
||||||
|
|
|
@ -7,10 +7,10 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
||||||
|
"github.com/AdguardTeam/golibs/container"
|
||||||
"github.com/AdguardTeam/golibs/errors"
|
"github.com/AdguardTeam/golibs/errors"
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
|
@ -153,7 +153,7 @@ func newQueryLog(conf Config) (l *queryLog, err error) {
|
||||||
l = &queryLog{
|
l = &queryLog{
|
||||||
findClient: findClient,
|
findClient: findClient,
|
||||||
|
|
||||||
buffer: aghalg.NewRingBuffer[*logEntry](memSize),
|
buffer: container.NewRingBuffer[*logEntry](memSize),
|
||||||
|
|
||||||
conf: &Config{},
|
conf: &Config{},
|
||||||
confMu: &sync.RWMutex{},
|
confMu: &sync.RWMutex{},
|
||||||
|
|
Loading…
Reference in a new issue