Skip to content

Commit

Permalink
Pull request 2195: upd-go-code
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit a1bd3c2
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Apr 4 14:59:37 2024 +0300

    all: upd more

commit 9e55bbb
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Apr 4 14:12:45 2024 +0300

    all: upd go code
  • Loading branch information
ainar-g committed Apr 4, 2024
1 parent 0e1e568 commit ee619b2
Show file tree
Hide file tree
Showing 34 changed files with 64 additions and 111 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -34,7 +34,7 @@ require (
go.etcd.io/bbolt v1.3.9
golang.org/x/crypto v0.21.0
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225
golang.org/x/net v0.22.0
golang.org/x/net v0.23.0
golang.org/x/sys v0.18.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v3 v3.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -145,8 +145,8 @@ golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
Expand Down
21 changes: 0 additions & 21 deletions internal/aghalg/aghalg.go
Expand Up @@ -10,29 +10,8 @@ import (
"golang.org/x/exp/constraints"
)

// Coalesce returns the first non-zero value. It is named after function
// COALESCE in SQL. If values or all its elements are empty, it returns a zero
// value.
//
// T is comparable, because Go currently doesn't have a comparableWithZeroValue
// constraint.
//
// TODO(a.garipov): Think of ways to merge with [CoalesceSlice].
func Coalesce[T comparable](values ...T) (res T) {
var zero T
for _, v := range values {
if v != zero {
return v
}
}

return zero
}

// CoalesceSlice returns the first non-zero value. It is named after function
// COALESCE in SQL. If values or all its elements are empty, it returns nil.
//
// TODO(a.garipov): Think of ways to merge with [Coalesce].
func CoalesceSlice[E any, S []E](values ...S) (res S) {
for _, v := range values {
if v != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/aghalg/ringbuffer_test.go
Expand Up @@ -33,7 +33,7 @@ func elements(b *aghalg.RingBuffer[int], n uint, reverse bool) (es []int) {
func TestNewRingBuffer(t *testing.T) {
t.Run("success_and_clear", func(t *testing.T) {
b := aghalg.NewRingBuffer[int](5)
for i := 0; i < 10; i++ {
for i := range 10 {
b.Append(i)
}
assert.Equal(t, []int{5, 6, 7, 8, 9}, elements(b, b.Len(), false))
Expand All @@ -44,7 +44,7 @@ func TestNewRingBuffer(t *testing.T) {

t.Run("zero", func(t *testing.T) {
b := aghalg.NewRingBuffer[int](0)
for i := 0; i < 10; i++ {
for i := range 10 {
b.Append(i)
bufLen := b.Len()
assert.EqualValues(t, 0, bufLen)
Expand All @@ -55,7 +55,7 @@ func TestNewRingBuffer(t *testing.T) {

t.Run("single", func(t *testing.T) {
b := aghalg.NewRingBuffer[int](1)
for i := 0; i < 10; i++ {
for i := range 10 {
b.Append(i)
bufLen := b.Len()
assert.EqualValues(t, 1, bufLen)
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestRingBuffer_Range(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for i := 0; i < tc.count; i++ {
for i := range tc.count {
b.Append(i)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/aghalg/sortedmap_test.go
Expand Up @@ -11,7 +11,7 @@ func TestNewSortedMap(t *testing.T) {
var m SortedMap[string, int]

letters := []string{}
for i := 0; i < 10; i++ {
for i := range 10 {
r := string('a' + rune(i))
letters = append(letters, r)
}
Expand Down
2 changes: 2 additions & 0 deletions internal/aghos/filewalker.go
Expand Up @@ -97,6 +97,8 @@ func (fw FileWalker) Walk(fsys fs.FS, initial ...string) (ok bool, err error) {
var filename string
defer func() { err = errors.Annotate(err, "checking %q: %w", filename) }()

// TODO(e.burkov): Redo this loop, as it modifies the very same slice it
// iterates over.
for i := 0; i < len(src); i++ {
var patterns []string
var cont bool
Expand Down
1 change: 0 additions & 1 deletion internal/aghrenameio/renameio_test.go
Expand Up @@ -78,7 +78,6 @@ func TestWithDeferredCleanup(t *testing.T) {
}}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

Expand Down
4 changes: 0 additions & 4 deletions internal/client/addrproc_test.go
Expand Up @@ -91,8 +91,6 @@ func TestDefaultAddrProc_Process_rDNS(t *testing.T) {
}}

for _, tc := range testCases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -186,8 +184,6 @@ func TestDefaultAddrProc_Process_WHOIS(t *testing.T) {
}}

for _, tc := range testCases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

Expand Down
4 changes: 3 additions & 1 deletion internal/configmigrate/v15.go
@@ -1,5 +1,7 @@
package configmigrate

import "github.com/AdguardTeam/golibs/errors"

// migrateTo15 performs the following changes:
//
// # BEFORE:
Expand Down Expand Up @@ -43,7 +45,7 @@ func migrateTo15(diskConf yobj) (err error) {
}
diskConf["querylog"] = qlog

return coalesceError(
return errors.Join(
moveVal[bool](dns, qlog, "querylog_enabled", "enabled"),
moveVal[bool](dns, qlog, "querylog_file_enabled", "file_enabled"),
moveVal[any](dns, qlog, "querylog_interval", "interval"),
Expand Down
4 changes: 3 additions & 1 deletion internal/configmigrate/v24.go
@@ -1,5 +1,7 @@
package configmigrate

import "github.com/AdguardTeam/golibs/errors"

// migrateTo24 performs the following changes:
//
// # BEFORE:
Expand Down Expand Up @@ -28,7 +30,7 @@ func migrateTo24(diskConf yobj) (err error) {
diskConf["schema_version"] = 24

logObj := yobj{}
err = coalesceError(
err = errors.Join(
moveVal[string](diskConf, logObj, "log_file", "file"),
moveVal[int](diskConf, logObj, "log_max_backups", "max_backups"),
moveVal[int](diskConf, logObj, "log_max_size", "max_size"),
Expand Down
4 changes: 3 additions & 1 deletion internal/configmigrate/v26.go
@@ -1,5 +1,7 @@
package configmigrate

import "github.com/AdguardTeam/golibs/errors"

// migrateTo26 performs the following changes:
//
// # BEFORE:
Expand Down Expand Up @@ -78,7 +80,7 @@ func migrateTo26(diskConf yobj) (err error) {
}

filteringObj := yobj{}
err = coalesceError(
err = errors.Join(
moveSameVal[bool](dns, filteringObj, "filtering_enabled"),
moveSameVal[int](dns, filteringObj, "filters_update_interval"),
moveSameVal[bool](dns, filteringObj, "parental_enabled"),
Expand Down
4 changes: 3 additions & 1 deletion internal/configmigrate/v7.go
@@ -1,5 +1,7 @@
package configmigrate

import "github.com/AdguardTeam/golibs/errors"

// migrateTo7 performs the following changes:
//
// # BEFORE:
Expand Down Expand Up @@ -37,7 +39,7 @@ func migrateTo7(diskConf yobj) (err error) {
}

dhcpv4 := yobj{}
err = coalesceError(
err = errors.Join(
moveSameVal[string](dhcp, dhcpv4, "gateway_ip"),
moveSameVal[string](dhcp, dhcpv4, "subnet_mask"),
moveSameVal[string](dhcp, dhcpv4, "range_start"),
Expand Down
16 changes: 0 additions & 16 deletions internal/configmigrate/yaml.go
Expand Up @@ -50,19 +50,3 @@ func moveVal[T any](src, dst yobj, srcKey, dstKey string) (err error) {
func moveSameVal[T any](src, dst yobj, key string) (err error) {
return moveVal[T](src, dst, key, key)
}

// coalesceError returns the first non-nil error. It is named after function
// COALESCE in SQL. If all errors are nil, it returns nil.
//
// TODO(e.burkov): Replace with [errors.Join].
//
// TODO(a.garipov): Think of ways to merge with [aghalg.Coalesce].
func coalesceError(errors ...error) (res error) {
for _, err := range errors {
if err != nil {
return err
}
}

return nil
}
1 change: 0 additions & 1 deletion internal/dnsforward/dns64_test.go
Expand Up @@ -266,7 +266,6 @@ func TestServer_HandleDNSRequest_dns64(t *testing.T) {
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
upsHdlr := dns.HandlerFunc(func(w dns.ResponseWriter, req *dns.Msg) {
q := req.Question[0]
Expand Down
4 changes: 2 additions & 2 deletions internal/dnsforward/dnsforward.go
Expand Up @@ -2,6 +2,7 @@
package dnsforward

import (
"cmp"
"context"
"fmt"
"io"
Expand All @@ -15,7 +16,6 @@ import (
"sync/atomic"
"time"

"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/AdGuardHome/internal/client"
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
Expand Down Expand Up @@ -908,5 +908,5 @@ func (s *Server) IsBlockedClient(ip netip.Addr, clientID string) (blocked bool,
blocked = true
}

return blocked, aghalg.Coalesce(rule, clientID)
return blocked, cmp.Or(rule, clientID)
}
16 changes: 8 additions & 8 deletions internal/dnsforward/dnsforward_test.go
@@ -1,6 +1,7 @@
package dnsforward

import (
"cmp"
"context"
"crypto/ecdsa"
"crypto/rand"
Expand All @@ -21,7 +22,6 @@ import (
"testing/fstest"
"time"

"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
Expand Down Expand Up @@ -190,7 +190,7 @@ func newGoogleUpstream() (u upstream.Upstream) {
return &aghtest.UpstreamMock{
OnAddress: func() (addr string) { return "google.upstream.example" },
OnExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
return aghalg.Coalesce(
return cmp.Or(
aghtest.MatchedResponse(req, dns.TypeA, googleDomainName, "8.8.8.8"),
new(dns.Msg).SetRcode(req, dns.RcodeNameError),
), nil
Expand Down Expand Up @@ -253,7 +253,7 @@ func sendTestMessagesAsync(t *testing.T, conn *dns.Conn) {

wg := &sync.WaitGroup{}

for i := 0; i < testMessagesCount; i++ {
for range testMessagesCount {
msg := createGoogleATestMessage()
wg.Add(1)

Expand All @@ -276,7 +276,7 @@ func sendTestMessagesAsync(t *testing.T, conn *dns.Conn) {
func sendTestMessages(t *testing.T, conn *dns.Conn) {
t.Helper()

for i := 0; i < testMessagesCount; i++ {
for i := range testMessagesCount {
req := createGoogleATestMessage()
err := conn.WriteMsg(req)
assert.NoErrorf(t, err, "cannot write message #%d: %s", i, err)
Expand Down Expand Up @@ -691,7 +691,7 @@ func TestServerCustomClientUpstream(t *testing.T) {
ups := aghtest.NewUpstreamMock(func(req *dns.Msg) (resp *dns.Msg, err error) {
atomic.AddUint32(&upsCalledCounter, 1)

return aghalg.Coalesce(
return cmp.Or(
aghtest.MatchedResponse(req, dns.TypeA, "host", "192.168.0.1"),
new(dns.Msg).SetRcode(req, dns.RcodeNameError),
), nil
Expand Down Expand Up @@ -1152,7 +1152,7 @@ func TestRewrite(t *testing.T) {
}))

ups := aghtest.NewUpstreamMock(func(req *dns.Msg) (resp *dns.Msg, err error) {
return aghalg.Coalesce(
return cmp.Or(
aghtest.MatchedResponse(req, dns.TypeA, "example.org", "4.3.2.1"),
new(dns.Msg).SetRcode(req, dns.RcodeNameError),
), nil
Expand Down Expand Up @@ -1481,7 +1481,7 @@ func TestServer_Exchange(t *testing.T) {
require.NoError(t, err)

extUpsHdlr := dns.HandlerFunc(func(w dns.ResponseWriter, req *dns.Msg) {
resp := aghalg.Coalesce(
resp := cmp.Or(
aghtest.MatchedResponse(req, dns.TypePTR, onesRevExtIPv4, dns.Fqdn(onesHost)),
doubleTTL(aghtest.MatchedResponse(req, dns.TypePTR, twosRevExtIPv4, dns.Fqdn(twosHost))),
new(dns.Msg).SetRcode(req, dns.RcodeNameError),
Expand All @@ -1495,7 +1495,7 @@ func TestServer_Exchange(t *testing.T) {
require.NoError(t, err)

locUpsHdlr := dns.HandlerFunc(func(w dns.ResponseWriter, req *dns.Msg) {
resp := aghalg.Coalesce(
resp := cmp.Or(
aghtest.MatchedResponse(req, dns.TypePTR, revLocIPv4, dns.Fqdn(localDomainHost)),
new(dns.Msg).SetRcode(req, dns.RcodeNameError),
)
Expand Down
10 changes: 3 additions & 7 deletions internal/dnsforward/process_internal_test.go
@@ -1,11 +1,11 @@
package dnsforward

import (
"cmp"
"net"
"net/netip"
"testing"

"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
"github.com/AdguardTeam/dnsproxy/proxy"
Expand Down Expand Up @@ -70,8 +70,6 @@ func TestServer_ProcessInitial(t *testing.T) {
}}

for _, tc := range testCases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -171,8 +169,6 @@ func TestServer_ProcessFilteringAfterResponse(t *testing.T) {
}}

for _, tc := range testCases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -667,7 +663,7 @@ func TestServer_ProcessRestrictLocal(t *testing.T) {
)

localUpsHdlr := dns.HandlerFunc(func(w dns.ResponseWriter, req *dns.Msg) {
resp := aghalg.Coalesce(
resp := cmp.Or(
aghtest.MatchedResponse(req, dns.TypePTR, extPTRQuestion, extPTRAnswer),
aghtest.MatchedResponse(req, dns.TypePTR, intPTRQuestion, intPTRAnswer),
new(dns.Msg).SetRcode(req, dns.RcodeNameError),
Expand Down Expand Up @@ -756,7 +752,7 @@ func TestServer_ProcessLocalPTR_usingResolvers(t *testing.T) {
const reqAddr = "1.1.168.192.in-addr.arpa."

localUpsHdlr := dns.HandlerFunc(func(w dns.ResponseWriter, req *dns.Msg) {
resp := aghalg.Coalesce(
resp := cmp.Or(
aghtest.MatchedResponse(req, dns.TypePTR, reqAddr, locDomain),
new(dns.Msg).SetRcode(req, dns.RcodeNameError),
)
Expand Down
4 changes: 2 additions & 2 deletions internal/filtering/filtering_test.go
Expand Up @@ -200,7 +200,7 @@ func TestParallelSB(t *testing.T) {
t.Cleanup(d.Close)

t.Run("group", func(t *testing.T) {
for i := 0; i < 100; i++ {
for i := range 100 {
t.Run(fmt.Sprintf("aaa%d", i), func(t *testing.T) {
t.Parallel()
d.checkMatch(t, sbBlocked, setts)
Expand Down Expand Up @@ -670,7 +670,7 @@ func BenchmarkSafeBrowsing(b *testing.B) {
}, nil)
b.Cleanup(d.Close)

for n := 0; n < b.N; n++ {
for range b.N {
res, err := d.CheckHost(sbBlocked, dns.TypeA, setts)
require.NoError(b, err)

Expand Down

0 comments on commit ee619b2

Please sign in to comment.