Pull request: 5035-more-clients-netip-addr

Updates #5035.

Squashed commit of the following:

commit 1934ea14299921760e9fcf6dd9053bd3155cb40e
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Nov 9 14:19:54 2022 +0300

    all: move more client code to netip.Addr
This commit is contained in:
Ainar Garipov
2022-11-09 14:37:07 +03:00
parent 98af0e000e
commit 167b112511
17 changed files with 164 additions and 210 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net"
"net/netip"
"os"
"sync"
"sync/atomic"
@@ -64,7 +65,7 @@ type Interface interface {
// GetTopClientIP returns at most limit IP addresses corresponding to the
// clients with the most number of requests.
TopClientsIP(limit uint) []net.IP
TopClientsIP(limit uint) []netip.Addr
// WriteDiskConfig puts the Interface's configuration to the dc.
WriteDiskConfig(dc *DiskConfig)
@@ -107,8 +108,6 @@ type StatsCtx struct {
filename string
}
var _ Interface = &StatsCtx{}
// New creates s from conf and properly initializes it. Don't use s before
// calling it's Start method.
func New(conf Config) (s *StatsCtx, err error) {
@@ -178,6 +177,9 @@ func withRecovered(orig *error) {
*orig = errors.WithDeferred(*orig, err)
}
// type check
var _ Interface = (*StatsCtx)(nil)
// Start implements the Interface interface for *StatsCtx.
func (s *StatsCtx) Start() {
s.initWeb()
@@ -250,8 +252,8 @@ func (s *StatsCtx) WriteDiskConfig(dc *DiskConfig) {
dc.Interval = atomic.LoadUint32(&s.limitHours) / 24
}
// TopClientsIP implements the Interface interface for *StatsCtx.
func (s *StatsCtx) TopClientsIP(maxCount uint) (ips []net.IP) {
// TopClientsIP implements the [Interface] interface for *StatsCtx.
func (s *StatsCtx) TopClientsIP(maxCount uint) (ips []netip.Addr) {
limit := atomic.LoadUint32(&s.limitHours)
if limit == 0 {
return nil
@@ -271,10 +273,10 @@ func (s *StatsCtx) TopClientsIP(maxCount uint) (ips []net.IP) {
}
a := convertMapToSlice(m, int(maxCount))
ips = []net.IP{}
ips = []netip.Addr{}
for _, it := range a {
ip := net.ParseIP(it.Name)
if ip != nil {
ip, err := netip.ParseAddr(it.Name)
if err == nil {
ips = append(ips, ip)
}
}

View File

@@ -11,6 +11,7 @@ import (
"testing"
"github.com/AdguardTeam/AdGuardHome/internal/stats"
"github.com/AdguardTeam/golibs/netutil"
"github.com/AdguardTeam/golibs/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -45,7 +46,7 @@ func assertSuccessAndUnmarshal(t *testing.T, to any, handler http.Handler, req *
}
func TestStats(t *testing.T) {
cliIP := net.IP{127, 0, 0, 1}
cliIP := netutil.IPv4Localhost()
cliIPStr := cliIP.String()
handlers := map[string]http.Handler{}
@@ -123,7 +124,7 @@ func TestStats(t *testing.T) {
topClients := s.TopClientsIP(2)
require.NotEmpty(t, topClients)
assert.True(t, cliIP.Equal(topClients[0]))
assert.Equal(t, cliIP, topClients[0])
})
t.Run("reset", func(t *testing.T) {