all: slog client

This commit is contained in:
Stanislav Chzhen 2024-10-10 20:59:46 +03:00
parent 73ff401b0f
commit 1ad99ee3cb
14 changed files with 224 additions and 117 deletions

View File

@ -89,14 +89,14 @@ func (s *ServiceWithConfig[ConfigType]) Config() (c ConfigType) {
// AddressProcessor is a fake [client.AddressProcessor] implementation for // AddressProcessor is a fake [client.AddressProcessor] implementation for
// tests. // tests.
type AddressProcessor struct { type AddressProcessor struct {
OnProcess func(ip netip.Addr) OnProcess func(ctx context.Context, ip netip.Addr)
OnClose func() (err error) OnClose func() (err error)
} }
// Process implements the [client.AddressProcessor] interface for // Process implements the [client.AddressProcessor] interface for
// *AddressProcessor. // *AddressProcessor.
func (p *AddressProcessor) Process(ip netip.Addr) { func (p *AddressProcessor) Process(ctx context.Context, ip netip.Addr) {
p.OnProcess(ip) p.OnProcess(ctx, ip)
} }
// Close implements the [client.AddressProcessor] interface for // Close implements the [client.AddressProcessor] interface for
@ -107,13 +107,18 @@ func (p *AddressProcessor) Close() (err error) {
// AddressUpdater is a fake [client.AddressUpdater] implementation for tests. // AddressUpdater is a fake [client.AddressUpdater] implementation for tests.
type AddressUpdater struct { type AddressUpdater struct {
OnUpdateAddress func(ip netip.Addr, host string, info *whois.Info) OnUpdateAddress func(ctx context.Context, ip netip.Addr, host string, info *whois.Info)
} }
// UpdateAddress implements the [client.AddressUpdater] interface for // UpdateAddress implements the [client.AddressUpdater] interface for
// *AddressUpdater. // *AddressUpdater.
func (p *AddressUpdater) UpdateAddress(ip netip.Addr, host string, info *whois.Info) { func (p *AddressUpdater) UpdateAddress(
p.OnUpdateAddress(ip, host, info) ctx context.Context,
ip netip.Addr,
host string,
info *whois.Info,
) {
p.OnUpdateAddress(ctx, ip, host, info)
} }
// Package dnsforward // Package dnsforward

View File

@ -11,7 +11,6 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/rdns" "github.com/AdguardTeam/AdGuardHome/internal/rdns"
"github.com/AdguardTeam/AdGuardHome/internal/whois" "github.com/AdguardTeam/AdGuardHome/internal/whois"
"github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/logutil/slogutil" "github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/AdguardTeam/golibs/netutil" "github.com/AdguardTeam/golibs/netutil"
) )
@ -22,7 +21,7 @@ const ErrClosed errors.Error = "use of closed address processor"
// AddressProcessor is the interface for types that can process clients. // AddressProcessor is the interface for types that can process clients.
type AddressProcessor interface { type AddressProcessor interface {
Process(ip netip.Addr) Process(ctx context.Context, ip netip.Addr)
Close() (err error) Close() (err error)
} }
@ -33,7 +32,7 @@ type EmptyAddrProc struct{}
var _ AddressProcessor = EmptyAddrProc{} var _ AddressProcessor = EmptyAddrProc{}
// Process implements the [AddressProcessor] interface for EmptyAddrProc. // Process implements the [AddressProcessor] interface for EmptyAddrProc.
func (EmptyAddrProc) Process(_ netip.Addr) {} func (EmptyAddrProc) Process(_ context.Context, _ netip.Addr) {}
// Close implements the [AddressProcessor] interface for EmptyAddrProc. // Close implements the [AddressProcessor] interface for EmptyAddrProc.
func (EmptyAddrProc) Close() (_ error) { return nil } func (EmptyAddrProc) Close() (_ error) { return nil }
@ -90,12 +89,15 @@ type DefaultAddrProcConfig struct {
type AddressUpdater interface { type AddressUpdater interface {
// UpdateAddress updates information about an IP address, setting host (if // UpdateAddress updates information about an IP address, setting host (if
// not empty) and WHOIS information (if not nil). // not empty) and WHOIS information (if not nil).
UpdateAddress(ip netip.Addr, host string, info *whois.Info) UpdateAddress(ctx context.Context, ip netip.Addr, host string, info *whois.Info)
} }
// DefaultAddrProc processes incoming client addresses with rDNS and WHOIS, if // DefaultAddrProc processes incoming client addresses with rDNS and WHOIS, if
// configured, and updates that information in a client storage. // configured, and updates that information in a client storage.
type DefaultAddrProc struct { type DefaultAddrProc struct {
// logger is used to log the operation of address processor.
logger *slog.Logger
// clientIPsMu serializes closure of clientIPs and access to isClosed. // clientIPsMu serializes closure of clientIPs and access to isClosed.
clientIPsMu *sync.Mutex clientIPsMu *sync.Mutex
@ -142,6 +144,7 @@ const (
// not be nil. // not be nil.
func NewDefaultAddrProc(c *DefaultAddrProcConfig) (p *DefaultAddrProc) { func NewDefaultAddrProc(c *DefaultAddrProcConfig) (p *DefaultAddrProc) {
p = &DefaultAddrProc{ p = &DefaultAddrProc{
logger: c.BaseLogger.With(slogutil.KeyPrefix, "addrproc"),
clientIPsMu: &sync.Mutex{}, clientIPsMu: &sync.Mutex{},
clientIPs: make(chan netip.Addr, defaultQueueSize), clientIPs: make(chan netip.Addr, defaultQueueSize),
rdns: &rdns.Empty{}, rdns: &rdns.Empty{},
@ -164,10 +167,13 @@ func NewDefaultAddrProc(c *DefaultAddrProcConfig) (p *DefaultAddrProc) {
p.whois = newWHOIS(c.BaseLogger.With(slogutil.KeyPrefix, "whois"), c.DialContext) p.whois = newWHOIS(c.BaseLogger.With(slogutil.KeyPrefix, "whois"), c.DialContext)
} }
go p.process(c.CatchPanics) // TODO(s.chzhen): Pass context.
ctx := context.TODO()
go p.process(ctx, c.CatchPanics)
for _, ip := range c.InitialAddresses { for _, ip := range c.InitialAddresses {
p.Process(ip) p.Process(ctx, ip)
} }
return p return p
@ -210,7 +216,7 @@ func newWHOIS(logger *slog.Logger, dialFunc aghnet.DialContextFunc) (w whois.Int
var _ AddressProcessor = (*DefaultAddrProc)(nil) var _ AddressProcessor = (*DefaultAddrProc)(nil)
// Process implements the [AddressProcessor] interface for *DefaultAddrProc. // Process implements the [AddressProcessor] interface for *DefaultAddrProc.
func (p *DefaultAddrProc) Process(ip netip.Addr) { func (p *DefaultAddrProc) Process(ctx context.Context, ip netip.Addr) {
p.clientIPsMu.Lock() p.clientIPsMu.Lock()
defer p.clientIPsMu.Unlock() defer p.clientIPsMu.Unlock()
@ -222,38 +228,42 @@ func (p *DefaultAddrProc) Process(ip netip.Addr) {
case p.clientIPs <- ip: case p.clientIPs <- ip:
// Go on. // Go on.
default: default:
log.Debug("clients: ip channel is full; len: %d", len(p.clientIPs)) p.logger.DebugContext(ctx, "ip channel is full", "len", len(p.clientIPs))
} }
} }
// process processes the incoming client IP-address information. It is intended // process processes the incoming client IP-address information. It is intended
// to be used as a goroutine. Once clientIPs is closed, process exits. // to be used as a goroutine. Once clientIPs is closed, process exits.
func (p *DefaultAddrProc) process(catchPanics bool) { func (p *DefaultAddrProc) process(ctx context.Context, catchPanics bool) {
if catchPanics { if catchPanics {
defer log.OnPanic("addrProcessor.process") slogutil.RecoverAndLog(ctx, p.logger)
} }
log.Info("clients: processing addresses") p.logger.InfoContext(ctx, "processing addresses")
ctx := context.TODO()
for ip := range p.clientIPs { for ip := range p.clientIPs {
host := p.processRDNS(ctx, ip) host := p.processRDNS(ctx, ip)
info := p.processWHOIS(ctx, ip) info := p.processWHOIS(ctx, ip)
p.addrUpdater.UpdateAddress(ip, host, info) p.addrUpdater.UpdateAddress(ctx, ip, host, info)
} }
log.Info("clients: finished processing addresses") p.logger.InfoContext(ctx, "finished processing addresses")
} }
// processRDNS resolves the clients' IP addresses using reverse DNS. host is // processRDNS resolves the clients' IP addresses using reverse DNS. host is
// empty if there were errors or if the information hasn't changed. // empty if there were errors or if the information hasn't changed.
func (p *DefaultAddrProc) processRDNS(ctx context.Context, ip netip.Addr) (host string) { func (p *DefaultAddrProc) processRDNS(ctx context.Context, ip netip.Addr) (host string) {
start := time.Now() start := time.Now()
log.Debug("clients: processing %s with rdns", ip) p.logger.DebugContext(ctx, "processing rdns", "ip", ip)
defer func() { defer func() {
log.Debug("clients: finished processing %s with rdns in %s", ip, time.Since(start)) p.logger.DebugContext(
ctx,
"finished processing rdns",
"ip", ip,
"host", host,
"elapsed", time.Since(start),
)
}() }()
ok := p.shouldResolve(ip) ok := p.shouldResolve(ip)
@ -280,9 +290,15 @@ func (p *DefaultAddrProc) shouldResolve(ip netip.Addr) (ok bool) {
// hasn't changed. // hasn't changed.
func (p *DefaultAddrProc) processWHOIS(ctx context.Context, ip netip.Addr) (info *whois.Info) { func (p *DefaultAddrProc) processWHOIS(ctx context.Context, ip netip.Addr) (info *whois.Info) {
start := time.Now() start := time.Now()
log.Debug("clients: processing %s with whois", ip) p.logger.DebugContext(ctx, "processing whois", "ip", ip)
defer func() { defer func() {
log.Debug("clients: finished processing %s with whois in %s", ip, time.Since(start)) p.logger.DebugContext(
ctx,
"finished processing whois",
"ip", ip,
"whois", info,
"elapsed", time.Since(start),
)
}() }()
// TODO(s.chzhen): Move the timeout logic from WHOIS configuration to the // TODO(s.chzhen): Move the timeout logic from WHOIS configuration to the

View File

@ -26,7 +26,8 @@ func TestEmptyAddrProc(t *testing.T) {
p := client.EmptyAddrProc{} p := client.EmptyAddrProc{}
assert.NotPanics(t, func() { assert.NotPanics(t, func() {
p.Process(testIP) ctx := testutil.ContextWithTimeout(t, testTimeout)
p.Process(ctx, testIP)
}) })
assert.NotPanics(t, func() { assert.NotPanics(t, func() {
@ -120,7 +121,8 @@ func TestDefaultAddrProc_Process_rDNS(t *testing.T) {
}) })
testutil.CleanupAndRequireSuccess(t, p.Close) testutil.CleanupAndRequireSuccess(t, p.Close)
p.Process(tc.ip) ctx := testutil.ContextWithTimeout(t, testTimeout)
p.Process(ctx, tc.ip)
if !tc.wantUpd { if !tc.wantUpd {
return return
@ -146,8 +148,8 @@ func newOnUpdateAddress(
ips chan<- netip.Addr, ips chan<- netip.Addr,
hosts chan<- string, hosts chan<- string,
infos chan<- *whois.Info, infos chan<- *whois.Info,
) (f func(ip netip.Addr, host string, info *whois.Info)) { ) (f func(ctx context.Context, ip netip.Addr, host string, info *whois.Info)) {
return func(ip netip.Addr, host string, info *whois.Info) { return func(ctx context.Context, ip netip.Addr, host string, info *whois.Info) {
if !want && (host != "" || info != nil) { if !want && (host != "" || info != nil) {
panic(fmt.Errorf("got unexpected update for %v with %q and %v", ip, host, info)) panic(fmt.Errorf("got unexpected update for %v with %q and %v", ip, host, info))
} }
@ -230,7 +232,8 @@ func TestDefaultAddrProc_Process_WHOIS(t *testing.T) {
}) })
testutil.CleanupAndRequireSuccess(t, p.Close) testutil.CleanupAndRequireSuccess(t, p.Close)
p.Process(testIP) ctx := testutil.ContextWithTimeout(t, testTimeout)
p.Process(ctx, testIP)
if !tc.wantUpd { if !tc.wantUpd {
return return
@ -251,7 +254,9 @@ func TestDefaultAddrProc_Process_WHOIS(t *testing.T) {
func TestDefaultAddrProc_Close(t *testing.T) { func TestDefaultAddrProc_Close(t *testing.T) {
t.Parallel() t.Parallel()
p := client.NewDefaultAddrProc(&client.DefaultAddrProcConfig{}) p := client.NewDefaultAddrProc(&client.DefaultAddrProcConfig{
BaseLogger: slogutil.NewDiscardLogger(),
})
err := p.Close() err := p.Close()
assert.NoError(t, err) assert.NoError(t, err)

View File

@ -1,8 +1,10 @@
package client package client
import ( import (
"context"
"encoding" "encoding"
"fmt" "fmt"
"log/slog"
"net" "net"
"net/netip" "net/netip"
"slices" "slices"
@ -12,7 +14,7 @@ import (
"github.com/AdguardTeam/dnsproxy/proxy" "github.com/AdguardTeam/dnsproxy/proxy"
"github.com/AdguardTeam/dnsproxy/upstream" "github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/AdguardTeam/golibs/netutil" "github.com/AdguardTeam/golibs/netutil"
"github.com/google/uuid" "github.com/google/uuid"
) )
@ -134,7 +136,7 @@ type Persistent struct {
// validate returns an error if persistent client information contains errors. // validate returns an error if persistent client information contains errors.
// allTags must be sorted. // allTags must be sorted.
func (c *Persistent) validate(allTags []string) (err error) { func (c *Persistent) validate(ctx context.Context, l *slog.Logger, allTags []string) (err error) {
switch { switch {
case c.Name == "": case c.Name == "":
return errors.Error("empty name") return errors.Error("empty name")
@ -151,7 +153,7 @@ func (c *Persistent) validate(allTags []string) (err error) {
err = conf.Close() err = conf.Close()
if err != nil { if err != nil {
log.Error("client: closing upstream config: %s", err) l.ErrorContext(ctx, "client: closing upstream config", slogutil.KeyError, err)
} }
for _, t := range c.Tags { for _, t := range c.Tags {

View File

@ -3,6 +3,7 @@ package client
import ( import (
"context" "context"
"fmt" "fmt"
"log/slog"
"net" "net"
"net/netip" "net/netip"
"slices" "slices"
@ -15,6 +16,7 @@ import (
"github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/hostsfile" "github.com/AdguardTeam/golibs/hostsfile"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/logutil/slogutil"
) )
// allowedTags is the list of available client tags. // allowedTags is the list of available client tags.
@ -83,6 +85,9 @@ type HostsContainer interface {
// StorageConfig is the client storage configuration structure. // StorageConfig is the client storage configuration structure.
type StorageConfig struct { type StorageConfig struct {
// Logger is used for logging the operation of the client storage.
Logger *slog.Logger
// DHCP is used to match IPs against MACs of persistent clients and update // DHCP is used to match IPs against MACs of persistent clients and update
// [SourceDHCP] runtime client information. It must not be nil. // [SourceDHCP] runtime client information. It must not be nil.
DHCP DHCP DHCP DHCP
@ -108,6 +113,9 @@ type StorageConfig struct {
// Storage contains information about persistent and runtime clients. // Storage contains information about persistent and runtime clients.
type Storage struct { type Storage struct {
// logger is used for logging the operation of the client storage.
logger *slog.Logger
// mu protects indexes of persistent and runtime clients. // mu protects indexes of persistent and runtime clients.
mu *sync.Mutex mu *sync.Mutex
@ -145,12 +153,12 @@ type Storage struct {
} }
// NewStorage returns initialized client storage. conf must not be nil. // NewStorage returns initialized client storage. conf must not be nil.
func NewStorage(conf *StorageConfig) (s *Storage, err error) { func NewStorage(ctx context.Context, conf *StorageConfig) (s *Storage, err error) {
tags := slices.Clone(allowedTags) tags := slices.Clone(allowedTags)
slices.Sort(tags) slices.Sort(tags)
s = &Storage{ s = &Storage{
allowedTags: tags, logger: conf.Logger,
mu: &sync.Mutex{}, mu: &sync.Mutex{},
index: newIndex(), index: newIndex(),
runtimeIndex: newRuntimeIndex(), runtimeIndex: newRuntimeIndex(),
@ -158,18 +166,19 @@ func NewStorage(conf *StorageConfig) (s *Storage, err error) {
etcHosts: conf.EtcHosts, etcHosts: conf.EtcHosts,
arpDB: conf.ARPDB, arpDB: conf.ARPDB,
done: make(chan struct{}), done: make(chan struct{}),
allowedTags: tags,
arpClientsUpdatePeriod: conf.ARPClientsUpdatePeriod, arpClientsUpdatePeriod: conf.ARPClientsUpdatePeriod,
runtimeSourceDHCP: conf.RuntimeSourceDHCP, runtimeSourceDHCP: conf.RuntimeSourceDHCP,
} }
for i, p := range conf.InitialClients { for i, p := range conf.InitialClients {
err = s.Add(p) err = s.Add(ctx, p)
if err != nil { if err != nil {
return nil, fmt.Errorf("adding client %q at index %d: %w", p.Name, i, err) return nil, fmt.Errorf("adding client %q at index %d: %w", p.Name, i, err)
} }
} }
s.ReloadARP() s.ReloadARP(ctx)
return s, nil return s, nil
} }
@ -177,9 +186,9 @@ func NewStorage(conf *StorageConfig) (s *Storage, err error) {
// Start starts the goroutines for updating the runtime client information. // Start starts the goroutines for updating the runtime client information.
// //
// TODO(s.chzhen): Pass context. // TODO(s.chzhen): Pass context.
func (s *Storage) Start(_ context.Context) (err error) { func (s *Storage) Start(ctx context.Context) (err error) {
go s.periodicARPUpdate() go s.periodicARPUpdate(ctx)
go s.handleHostsUpdates() go s.handleHostsUpdates(ctx)
return nil return nil
} }
@ -195,15 +204,15 @@ func (s *Storage) Shutdown(_ context.Context) (err error) {
// periodicARPUpdate periodically reloads runtime clients from ARP. It is // periodicARPUpdate periodically reloads runtime clients from ARP. It is
// intended to be used as a goroutine. // intended to be used as a goroutine.
func (s *Storage) periodicARPUpdate() { func (s *Storage) periodicARPUpdate(ctx context.Context) {
defer log.OnPanic("storage") slogutil.RecoverAndLog(ctx, s.logger)
t := time.NewTicker(s.arpClientsUpdatePeriod) t := time.NewTicker(s.arpClientsUpdatePeriod)
for { for {
select { select {
case <-t.C: case <-t.C:
s.ReloadARP() s.ReloadARP(ctx)
case <-s.done: case <-s.done:
return return
} }
@ -211,28 +220,28 @@ func (s *Storage) periodicARPUpdate() {
} }
// ReloadARP reloads runtime clients from ARP, if configured. // ReloadARP reloads runtime clients from ARP, if configured.
func (s *Storage) ReloadARP() { func (s *Storage) ReloadARP(ctx context.Context) {
if s.arpDB != nil { if s.arpDB != nil {
s.addFromSystemARP() s.addFromSystemARP(ctx)
} }
} }
// addFromSystemARP adds the IP-hostname pairings from the output of the arp -a // addFromSystemARP adds the IP-hostname pairings from the output of the arp -a
// command. // command.
func (s *Storage) addFromSystemARP() { func (s *Storage) addFromSystemARP(ctx context.Context) {
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
if err := s.arpDB.Refresh(); err != nil { if err := s.arpDB.Refresh(); err != nil {
s.arpDB = arpdb.Empty{} s.arpDB = arpdb.Empty{}
log.Error("refreshing arp container: %s", err) s.logger.ErrorContext(ctx, "refreshing arp container", slogutil.KeyError, err)
return return
} }
ns := s.arpDB.Neighbors() ns := s.arpDB.Neighbors()
if len(ns) == 0 { if len(ns) == 0 {
log.Debug("refreshing arp container: the update is empty") s.logger.DebugContext(ctx, "refreshing arp container: the update is empty")
return return
} }
@ -246,17 +255,22 @@ func (s *Storage) addFromSystemARP() {
removed := s.runtimeIndex.removeEmpty() removed := s.runtimeIndex.removeEmpty()
log.Debug("storage: added %d, removed %d client aliases from arp neighborhood", len(ns), removed) s.logger.DebugContext(
ctx,
"updating client aliases from arp neighborhood",
"added", len(ns),
"removed", removed,
)
} }
// handleHostsUpdates receives the updates from the hosts container and adds // handleHostsUpdates receives the updates from the hosts container and adds
// them to the clients storage. It is intended to be used as a goroutine. // them to the clients storage. It is intended to be used as a goroutine.
func (s *Storage) handleHostsUpdates() { func (s *Storage) handleHostsUpdates(ctx context.Context) {
if s.etcHosts == nil { if s.etcHosts == nil {
return return
} }
defer log.OnPanic("storage") slogutil.RecoverAndLog(ctx, s.logger)
for { for {
select { select {
@ -265,7 +279,7 @@ func (s *Storage) handleHostsUpdates() {
return return
} }
s.addFromHostsFile(upd) s.addFromHostsFile(ctx, upd)
case <-s.done: case <-s.done:
return return
} }
@ -274,7 +288,7 @@ func (s *Storage) handleHostsUpdates() {
// addFromHostsFile fills the client-hostname pairing index from the system's // addFromHostsFile fills the client-hostname pairing index from the system's
// hosts files. // hosts files.
func (s *Storage) addFromHostsFile(hosts *hostsfile.DefaultStorage) { func (s *Storage) addFromHostsFile(ctx context.Context, hosts *hostsfile.DefaultStorage) {
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
@ -294,14 +308,19 @@ func (s *Storage) addFromHostsFile(hosts *hostsfile.DefaultStorage) {
}) })
removed := s.runtimeIndex.removeEmpty() removed := s.runtimeIndex.removeEmpty()
log.Debug("storage: added %d, removed %d client aliases from system hosts file", added, removed) s.logger.DebugContext(
ctx,
"updating client aliases from system hosts file",
"added", added,
"removed", removed,
)
} }
// type check // type check
var _ AddressUpdater = (*Storage)(nil) var _ AddressUpdater = (*Storage)(nil)
// UpdateAddress implements the [AddressUpdater] interface for *Storage // UpdateAddress implements the [AddressUpdater] interface for *Storage
func (s *Storage) UpdateAddress(ip netip.Addr, host string, info *whois.Info) { func (s *Storage) UpdateAddress(ctx context.Context, ip netip.Addr, host string, info *whois.Info) {
// Common fast path optimization. // Common fast path optimization.
if host == "" && info == nil { if host == "" && info == nil {
return return
@ -315,12 +334,12 @@ func (s *Storage) UpdateAddress(ip netip.Addr, host string, info *whois.Info) {
} }
if info != nil { if info != nil {
s.setWHOISInfo(ip, info) s.setWHOISInfo(ctx, ip, info)
} }
} }
// UpdateDHCP updates [SourceDHCP] runtime client information. // UpdateDHCP updates [SourceDHCP] runtime client information.
func (s *Storage) UpdateDHCP() { func (s *Storage) UpdateDHCP(ctx context.Context) {
if s.dhcp == nil || !s.runtimeSourceDHCP { if s.dhcp == nil || !s.runtimeSourceDHCP {
return return
} }
@ -338,14 +357,23 @@ func (s *Storage) UpdateDHCP() {
} }
removed := s.runtimeIndex.removeEmpty() removed := s.runtimeIndex.removeEmpty()
log.Debug("storage: added %d, removed %d client aliases from dhcp", added, removed) s.logger.DebugContext(
ctx,
"updating client aliases from dhcp",
"added", added,
"removed", removed,
)
} }
// setWHOISInfo sets the WHOIS information for a runtime client. // setWHOISInfo sets the WHOIS information for a runtime client.
func (s *Storage) setWHOISInfo(ip netip.Addr, wi *whois.Info) { func (s *Storage) setWHOISInfo(ctx context.Context, ip netip.Addr, wi *whois.Info) {
_, ok := s.index.findByIP(ip) _, ok := s.index.findByIP(ip)
if ok { if ok {
log.Debug("storage: client for %s is already created, ignore whois info", ip) s.logger.DebugContext(
ctx,
"persistent client is already created, ignore whois info",
"ip", ip,
)
return return
} }
@ -358,14 +386,14 @@ func (s *Storage) setWHOISInfo(ip netip.Addr, wi *whois.Info) {
rc.setWHOIS(wi) rc.setWHOIS(wi)
log.Debug("storage: set whois info for runtime client with ip %s: %+v", ip, wi) s.logger.DebugContext(ctx, "set whois info for runtime client", "ip", ip, "whois", wi)
} }
// Add stores persistent client information or returns an error. // Add stores persistent client information or returns an error.
func (s *Storage) Add(p *Persistent) (err error) { func (s *Storage) Add(ctx context.Context, p *Persistent) (err error) {
defer func() { err = errors.Annotate(err, "adding client: %w") }() defer func() { err = errors.Annotate(err, "adding client: %w") }()
err = p.validate(s.allowedTags) err = p.validate(ctx, s.logger, s.allowedTags)
if err != nil { if err != nil {
// Don't wrap the error since there is already an annotation deferred. // Don't wrap the error since there is already an annotation deferred.
return err return err
@ -388,7 +416,13 @@ func (s *Storage) Add(p *Persistent) (err error) {
s.index.add(p) s.index.add(p)
log.Debug("client storage: added %q: IDs: %q [%d]", p.Name, p.IDs(), s.index.size()) s.logger.DebugContext(
ctx,
"client added",
"name", p.Name,
"ids", p.IDs(),
"clients_count", s.index.size(),
)
return nil return nil
} }
@ -490,10 +524,10 @@ func (s *Storage) RemoveByName(name string) (ok bool) {
// Update finds the stored persistent client by its name and updates its // Update finds the stored persistent client by its name and updates its
// information from p. // information from p.
func (s *Storage) Update(name string, p *Persistent) (err error) { func (s *Storage) Update(ctx context.Context, name string, p *Persistent) (err error) {
defer func() { err = errors.Annotate(err, "updating client: %w") }() defer func() { err = errors.Annotate(err, "updating client: %w") }()
err = p.validate(s.allowedTags) err = p.validate(ctx, s.logger, s.allowedTags)
if err != nil { if err != nil {
// Don't wrap the error since there is already an annotation deferred. // Don't wrap the error since there is already an annotation deferred.
return err return err

View File

@ -15,6 +15,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc" "github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc"
"github.com/AdguardTeam/AdGuardHome/internal/whois" "github.com/AdguardTeam/AdGuardHome/internal/whois"
"github.com/AdguardTeam/golibs/hostsfile" "github.com/AdguardTeam/golibs/hostsfile"
"github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/AdguardTeam/golibs/testutil" "github.com/AdguardTeam/golibs/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -110,7 +111,9 @@ func TestStorage_Add_hostsfile(t *testing.T) {
onUpd: func() (updates <-chan *hostsfile.DefaultStorage) { return hostCh }, onUpd: func() (updates <-chan *hostsfile.DefaultStorage) { return hostCh },
} }
storage, err := client.NewStorage(&client.StorageConfig{ ctx := testutil.ContextWithTimeout(t, testTimeout)
storage, err := client.NewStorage(ctx, &client.StorageConfig{
Logger: slogutil.NewDiscardLogger(),
DHCP: client.EmptyDHCP{}, DHCP: client.EmptyDHCP{},
EtcHosts: h, EtcHosts: h,
ARPClientsUpdatePeriod: testTimeout / 10, ARPClientsUpdatePeriod: testTimeout / 10,
@ -198,7 +201,9 @@ func TestStorage_Add_arp(t *testing.T) {
}, },
} }
storage, err := client.NewStorage(&client.StorageConfig{ ctx := testutil.ContextWithTimeout(t, testTimeout)
storage, err := client.NewStorage(ctx, &client.StorageConfig{
Logger: slogutil.NewDiscardLogger(),
DHCP: client.EmptyDHCP{}, DHCP: client.EmptyDHCP{},
ARPDB: a, ARPDB: a,
ARPClientsUpdatePeriod: testTimeout / 10, ARPClientsUpdatePeriod: testTimeout / 10,
@ -273,7 +278,9 @@ func TestStorage_Add_whois(t *testing.T) {
cliName3 = "client_three" cliName3 = "client_three"
) )
storage, err := client.NewStorage(&client.StorageConfig{ ctx := testutil.ContextWithTimeout(t, testTimeout)
storage, err := client.NewStorage(ctx, &client.StorageConfig{
Logger: slogutil.NewDiscardLogger(),
DHCP: client.EmptyDHCP{}, DHCP: client.EmptyDHCP{},
}) })
require.NoError(t, err) require.NoError(t, err)
@ -284,7 +291,7 @@ func TestStorage_Add_whois(t *testing.T) {
} }
t.Run("new_client", func(t *testing.T) { t.Run("new_client", func(t *testing.T) {
storage.UpdateAddress(cliIP1, "", whois) storage.UpdateAddress(ctx, cliIP1, "", whois)
cli1 := storage.ClientRuntime(cliIP1) cli1 := storage.ClientRuntime(cliIP1)
require.NotNil(t, cli1) require.NotNil(t, cli1)
@ -292,8 +299,8 @@ func TestStorage_Add_whois(t *testing.T) {
}) })
t.Run("existing_runtime_client", func(t *testing.T) { t.Run("existing_runtime_client", func(t *testing.T) {
storage.UpdateAddress(cliIP2, cliName2, nil) storage.UpdateAddress(ctx, cliIP2, cliName2, nil)
storage.UpdateAddress(cliIP2, "", whois) storage.UpdateAddress(ctx, cliIP2, "", whois)
cli2 := storage.ClientRuntime(cliIP2) cli2 := storage.ClientRuntime(cliIP2)
require.NotNil(t, cli2) require.NotNil(t, cli2)
@ -304,14 +311,14 @@ func TestStorage_Add_whois(t *testing.T) {
}) })
t.Run("can't_set_persistent_client", func(t *testing.T) { t.Run("can't_set_persistent_client", func(t *testing.T) {
err = storage.Add(&client.Persistent{ err = storage.Add(ctx, &client.Persistent{
Name: cliName3, Name: cliName3,
UID: client.MustNewUID(), UID: client.MustNewUID(),
IPs: []netip.Addr{cliIP3}, IPs: []netip.Addr{cliIP3},
}) })
require.NoError(t, err) require.NoError(t, err)
storage.UpdateAddress(cliIP3, "", whois) storage.UpdateAddress(ctx, cliIP3, "", whois)
rc := storage.ClientRuntime(cliIP3) rc := storage.ClientRuntime(cliIP3)
require.Nil(t, rc) require.Nil(t, rc)
}) })
@ -364,7 +371,9 @@ func TestClientsDHCP(t *testing.T) {
}, },
} }
storage, err := client.NewStorage(&client.StorageConfig{ ctx := testutil.ContextWithTimeout(t, testTimeout)
storage, err := client.NewStorage(ctx, &client.StorageConfig{
Logger: slogutil.NewDiscardLogger(),
DHCP: d, DHCP: d,
RuntimeSourceDHCP: true, RuntimeSourceDHCP: true,
}) })
@ -378,7 +387,7 @@ func TestClientsDHCP(t *testing.T) {
}) })
t.Run("find_persistent", func(t *testing.T) { t.Run("find_persistent", func(t *testing.T) {
err = storage.Add(&client.Persistent{ err = storage.Add(ctx, &client.Persistent{
Name: prsCliName, Name: prsCliName,
UID: client.MustNewUID(), UID: client.MustNewUID(),
MACs: []net.HardwareAddr{prsCliMAC}, MACs: []net.HardwareAddr{prsCliMAC},
@ -393,7 +402,7 @@ func TestClientsDHCP(t *testing.T) {
t.Run("leases", func(t *testing.T) { t.Run("leases", func(t *testing.T) {
delete(ipToHost, cliIP1) delete(ipToHost, cliIP1)
storage.UpdateDHCP() storage.UpdateDHCP(ctx)
cli1 := storage.ClientRuntime(cliIP1) cli1 := storage.ClientRuntime(cliIP1)
require.Nil(t, cli1) require.Nil(t, cli1)
@ -421,8 +430,11 @@ func TestClientsDHCP(t *testing.T) {
} }
func TestClientsAddExisting(t *testing.T) { func TestClientsAddExisting(t *testing.T) {
ctx := testutil.ContextWithTimeout(t, testTimeout)
t.Run("simple", func(t *testing.T) { t.Run("simple", func(t *testing.T) {
storage, err := client.NewStorage(&client.StorageConfig{ storage, err := client.NewStorage(ctx, &client.StorageConfig{
Logger: slogutil.NewDiscardLogger(),
DHCP: client.EmptyDHCP{}, DHCP: client.EmptyDHCP{},
}) })
require.NoError(t, err) require.NoError(t, err)
@ -430,7 +442,7 @@ func TestClientsAddExisting(t *testing.T) {
ip := netip.MustParseAddr("1.1.1.1") ip := netip.MustParseAddr("1.1.1.1")
// Add a client. // Add a client.
err = storage.Add(&client.Persistent{ err = storage.Add(ctx, &client.Persistent{
Name: "client1", Name: "client1",
UID: client.MustNewUID(), UID: client.MustNewUID(),
IPs: []netip.Addr{ip, netip.MustParseAddr("1:2:3::4")}, IPs: []netip.Addr{ip, netip.MustParseAddr("1:2:3::4")},
@ -440,7 +452,7 @@ func TestClientsAddExisting(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// Now add an auto-client with the same IP. // Now add an auto-client with the same IP.
storage.UpdateAddress(ip, "test", nil) storage.UpdateAddress(ctx, ip, "test", nil)
rc := storage.ClientRuntime(ip) rc := storage.ClientRuntime(ip)
assert.True(t, compareRuntimeInfo(rc, client.SourceRDNS, "test")) assert.True(t, compareRuntimeInfo(rc, client.SourceRDNS, "test"))
}) })
@ -468,7 +480,8 @@ func TestClientsAddExisting(t *testing.T) {
dhcpServer, err := dhcpd.Create(config) dhcpServer, err := dhcpd.Create(config)
require.NoError(t, err) require.NoError(t, err)
storage, err := client.NewStorage(&client.StorageConfig{ storage, err := client.NewStorage(ctx, &client.StorageConfig{
Logger: slogutil.NewDiscardLogger(),
DHCP: dhcpServer, DHCP: dhcpServer,
}) })
require.NoError(t, err) require.NoError(t, err)
@ -484,7 +497,7 @@ func TestClientsAddExisting(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// Add a new client with the same IP as for a client with MAC. // Add a new client with the same IP as for a client with MAC.
err = storage.Add(&client.Persistent{ err = storage.Add(ctx, &client.Persistent{
Name: "client2", Name: "client2",
UID: client.MustNewUID(), UID: client.MustNewUID(),
IPs: []netip.Addr{ip}, IPs: []netip.Addr{ip},
@ -492,7 +505,7 @@ func TestClientsAddExisting(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// Add a new client with the IP from the first client's IP range. // Add a new client with the IP from the first client's IP range.
err = storage.Add(&client.Persistent{ err = storage.Add(ctx, &client.Persistent{
Name: "client3", Name: "client3",
UID: client.MustNewUID(), UID: client.MustNewUID(),
IPs: []netip.Addr{netip.MustParseAddr("2.2.2.2")}, IPs: []netip.Addr{netip.MustParseAddr("2.2.2.2")},
@ -506,14 +519,16 @@ func TestClientsAddExisting(t *testing.T) {
func newStorage(tb testing.TB, m []*client.Persistent) (s *client.Storage) { func newStorage(tb testing.TB, m []*client.Persistent) (s *client.Storage) {
tb.Helper() tb.Helper()
s, err := client.NewStorage(&client.StorageConfig{ ctx := testutil.ContextWithTimeout(tb, testTimeout)
s, err := client.NewStorage(ctx, &client.StorageConfig{
Logger: slogutil.NewDiscardLogger(),
DHCP: client.EmptyDHCP{}, DHCP: client.EmptyDHCP{},
}) })
require.NoError(tb, err) require.NoError(tb, err)
for _, c := range m { for _, c := range m {
c.UID = client.MustNewUID() c.UID = client.MustNewUID()
require.NoError(tb, s.Add(c)) require.NoError(tb, s.Add(ctx, c))
} }
require.Equal(tb, len(m), s.Size()) require.Equal(tb, len(m), s.Size())
@ -555,7 +570,10 @@ func TestStorage_Add(t *testing.T) {
UID: existingClientUID, UID: existingClientUID,
} }
s, err := client.NewStorage(&client.StorageConfig{}) ctx := testutil.ContextWithTimeout(t, testTimeout)
s, err := client.NewStorage(ctx, &client.StorageConfig{
Logger: slogutil.NewDiscardLogger(),
})
require.NoError(t, err) require.NoError(t, err)
tags := s.AllowedTags() tags := s.AllowedTags()
@ -568,7 +586,7 @@ func TestStorage_Add(t *testing.T) {
_, ok = slices.BinarySearch(tags, notAllowedTag) _, ok = slices.BinarySearch(tags, notAllowedTag)
require.False(t, ok) require.False(t, ok)
err = s.Add(existingClient) err = s.Add(ctx, existingClient)
require.NoError(t, err) require.NoError(t, err)
testCases := []struct { testCases := []struct {
@ -669,7 +687,7 @@ func TestStorage_Add(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
err = s.Add(tc.cli) err = s.Add(ctx, tc.cli)
testutil.AssertErrorMsg(t, tc.wantErrMsg, err) testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
}) })
@ -687,10 +705,13 @@ func TestStorage_RemoveByName(t *testing.T) {
UID: client.MustNewUID(), UID: client.MustNewUID(),
} }
s, err := client.NewStorage(&client.StorageConfig{}) ctx := testutil.ContextWithTimeout(t, testTimeout)
s, err := client.NewStorage(ctx, &client.StorageConfig{
Logger: slogutil.NewDiscardLogger(),
})
require.NoError(t, err) require.NoError(t, err)
err = s.Add(existingClient) err = s.Add(ctx, existingClient)
require.NoError(t, err) require.NoError(t, err)
testCases := []struct { testCases := []struct {
@ -714,10 +735,12 @@ func TestStorage_RemoveByName(t *testing.T) {
} }
t.Run("duplicate_remove", func(t *testing.T) { t.Run("duplicate_remove", func(t *testing.T) {
s, err = client.NewStorage(&client.StorageConfig{}) s, err = client.NewStorage(ctx, &client.StorageConfig{
Logger: slogutil.NewDiscardLogger(),
})
require.NoError(t, err) require.NoError(t, err)
err = s.Add(existingClient) err = s.Add(ctx, existingClient)
require.NoError(t, err) require.NoError(t, err)
assert.True(t, s.RemoveByName(existingName)) assert.True(t, s.RemoveByName(existingName))
@ -1080,6 +1103,7 @@ func TestStorage_Update(t *testing.T) {
`uses the same ClientID "obstructing_client_id"`, `uses the same ClientID "obstructing_client_id"`,
}} }}
ctx := testutil.ContextWithTimeout(t, testTimeout)
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
s := newStorage( s := newStorage(
@ -1090,7 +1114,7 @@ func TestStorage_Update(t *testing.T) {
}, },
) )
err := s.Update(clientName, tc.cli) err := s.Update(ctx, clientName, tc.cli)
testutil.AssertErrorMsg(t, tc.wantErrMsg, err) testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
}) })
} }

View File

@ -2,6 +2,7 @@ package dnsforward
import ( import (
"cmp" "cmp"
"context"
"encoding/binary" "encoding/binary"
"net" "net"
"net/netip" "net/netip"
@ -203,7 +204,8 @@ func (s *Server) processClientIP(addr netip.Addr) {
s.serverLock.RLock() s.serverLock.RLock()
defer s.serverLock.RUnlock() defer s.serverLock.RUnlock()
s.addrProc.Process(addr) // TODO(s.chzhen): Pass context.
s.addrProc.Process(context.TODO(), addr)
} }
// processDDRQuery responds to Discovery of Designated Resolvers (DDR) SVCB // processDDRQuery responds to Discovery of Designated Resolvers (DDR) SVCB

View File

@ -2,6 +2,7 @@ package dnsforward
import ( import (
"cmp" "cmp"
"context"
"net" "net"
"net/netip" "net/netip"
"testing" "testing"
@ -90,7 +91,7 @@ func TestServer_ProcessInitial(t *testing.T) {
var gotAddr netip.Addr var gotAddr netip.Addr
s.addrProc = &aghtest.AddressProcessor{ s.addrProc = &aghtest.AddressProcessor{
OnProcess: func(ip netip.Addr) { gotAddr = ip }, OnProcess: func(ctx context.Context, ip netip.Addr) { gotAddr = ip },
OnClose: func() (err error) { panic("not implemented") }, OnClose: func() (err error) { panic("not implemented") },
} }

View File

@ -107,7 +107,8 @@ func (clients *clientsContainer) Init(
hosts = etcHosts hosts = etcHosts
} }
clients.storage, err = client.NewStorage(&client.StorageConfig{ clients.storage, err = client.NewStorage(ctx, &client.StorageConfig{
Logger: baseLogger.With(slogutil.KeyPrefix, "client_storage"),
InitialClients: confClients, InitialClients: confClients,
DHCP: dhcpServer, DHCP: dhcpServer,
EtcHosts: hosts, EtcHosts: hosts,
@ -417,7 +418,8 @@ func (clients *clientsContainer) UpstreamConfigByID(
) )
c.UpstreamConfig = conf c.UpstreamConfig = conf
err = clients.storage.Update(c.Name, c) // TODO(s.chzhen): Pass context.
err = clients.storage.Update(context.TODO(), c.Name, c)
if err != nil { if err != nil {
return nil, fmt.Errorf("setting upstream config: %w", err) return nil, fmt.Errorf("setting upstream config: %w", err)
} }
@ -430,8 +432,13 @@ var _ client.AddressUpdater = (*clientsContainer)(nil)
// UpdateAddress implements the [client.AddressUpdater] interface for // UpdateAddress implements the [client.AddressUpdater] interface for
// *clientsContainer // *clientsContainer
func (clients *clientsContainer) UpdateAddress(ip netip.Addr, host string, info *whois.Info) { func (clients *clientsContainer) UpdateAddress(
clients.storage.UpdateAddress(ip, host, info) ctx context.Context,
ip netip.Addr,
host string,
info *whois.Info,
) {
clients.storage.UpdateAddress(ctx, ip, host, info)
} }
// close gracefully closes all the client-specific upstream configurations of // close gracefully closes all the client-specific upstream configurations of

View File

@ -40,9 +40,10 @@ func newClientsContainer(t *testing.T) (c *clientsContainer) {
func TestClientsCustomUpstream(t *testing.T) { func TestClientsCustomUpstream(t *testing.T) {
clients := newClientsContainer(t) clients := newClientsContainer(t)
ctx := testutil.ContextWithTimeout(t, testTimeout)
// Add client with upstreams. // Add client with upstreams.
err := clients.storage.Add(&client.Persistent{ err := clients.storage.Add(ctx, &client.Persistent{
Name: "client1", Name: "client1",
UID: client.MustNewUID(), UID: client.MustNewUID(),
IPs: []netip.Addr{netip.MustParseAddr("1.1.1.1"), netip.MustParseAddr("1:2:3::4")}, IPs: []netip.Addr{netip.MustParseAddr("1.1.1.1"), netip.MustParseAddr("1:2:3::4")},

View File

@ -106,7 +106,7 @@ func (clients *clientsContainer) handleGetClients(w http.ResponseWriter, r *http
return true return true
}) })
clients.storage.UpdateDHCP() clients.storage.UpdateDHCP(r.Context())
clients.storage.RangeRuntime(func(rc *client.Runtime) (cont bool) { clients.storage.RangeRuntime(func(rc *client.Runtime) (cont bool) {
src, host := rc.Info() src, host := rc.Info()
@ -341,7 +341,7 @@ func (clients *clientsContainer) handleAddClient(w http.ResponseWriter, r *http.
return return
} }
err = clients.storage.Add(c) err = clients.storage.Add(r.Context(), c)
if err != nil { if err != nil {
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err) aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
@ -411,7 +411,7 @@ func (clients *clientsContainer) handleUpdateClient(w http.ResponseWriter, r *ht
return return
} }
err = clients.storage.Update(dj.Name, c) err = clients.storage.Update(r.Context(), dj.Name, c)
if err != nil { if err != nil {
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err) aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)

View File

@ -203,13 +203,14 @@ func TestClientsContainer_HandleAddClient(t *testing.T) {
func TestClientsContainer_HandleDelClient(t *testing.T) { func TestClientsContainer_HandleDelClient(t *testing.T) {
clients := newClientsContainer(t) clients := newClientsContainer(t)
ctx := testutil.ContextWithTimeout(t, testTimeout)
clientOne := newPersistentClientWithIDs(t, "client1", []string{testClientIP1}) clientOne := newPersistentClientWithIDs(t, "client1", []string{testClientIP1})
err := clients.storage.Add(clientOne) err := clients.storage.Add(ctx, clientOne)
require.NoError(t, err) require.NoError(t, err)
clientTwo := newPersistentClientWithIDs(t, "client2", []string{testClientIP2}) clientTwo := newPersistentClientWithIDs(t, "client2", []string{testClientIP2})
err = clients.storage.Add(clientTwo) err = clients.storage.Add(ctx, clientTwo)
require.NoError(t, err) require.NoError(t, err)
assertPersistentClients(t, clients, []*client.Persistent{clientOne, clientTwo}) assertPersistentClients(t, clients, []*client.Persistent{clientOne, clientTwo})
@ -265,9 +266,10 @@ func TestClientsContainer_HandleDelClient(t *testing.T) {
func TestClientsContainer_HandleUpdateClient(t *testing.T) { func TestClientsContainer_HandleUpdateClient(t *testing.T) {
clients := newClientsContainer(t) clients := newClientsContainer(t)
ctx := testutil.ContextWithTimeout(t, testTimeout)
clientOne := newPersistentClientWithIDs(t, "client1", []string{testClientIP1}) clientOne := newPersistentClientWithIDs(t, "client1", []string{testClientIP1})
err := clients.storage.Add(clientOne) err := clients.storage.Add(ctx, clientOne)
require.NoError(t, err) require.NoError(t, err)
assertPersistentClients(t, clients, []*client.Persistent{clientOne}) assertPersistentClients(t, clients, []*client.Persistent{clientOne})
@ -348,12 +350,14 @@ func TestClientsContainer_HandleFindClient(t *testing.T) {
}, },
} }
ctx := testutil.ContextWithTimeout(t, testTimeout)
clientOne := newPersistentClientWithIDs(t, "client1", []string{testClientIP1}) clientOne := newPersistentClientWithIDs(t, "client1", []string{testClientIP1})
err := clients.storage.Add(clientOne) err := clients.storage.Add(ctx, clientOne)
require.NoError(t, err) require.NoError(t, err)
clientTwo := newPersistentClientWithIDs(t, "client2", []string{testClientIP2}) clientTwo := newPersistentClientWithIDs(t, "client2", []string{testClientIP2})
err = clients.storage.Add(clientTwo) err = clients.storage.Add(ctx, clientTwo)
require.NoError(t, err) require.NoError(t, err)
assertPersistentClients(t, clients, []*client.Persistent{clientOne, clientTwo}) assertPersistentClients(t, clients, []*client.Persistent{clientOne, clientTwo})

View File

@ -7,6 +7,8 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/client" "github.com/AdguardTeam/AdGuardHome/internal/client"
"github.com/AdguardTeam/AdGuardHome/internal/filtering" "github.com/AdguardTeam/AdGuardHome/internal/filtering"
"github.com/AdguardTeam/AdGuardHome/internal/schedule" "github.com/AdguardTeam/AdGuardHome/internal/schedule"
"github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/AdguardTeam/golibs/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -18,12 +20,15 @@ var testIPv4 = netip.AddrFrom4([4]byte{1, 2, 3, 4})
func newStorage(tb testing.TB, clients []*client.Persistent) (s *client.Storage) { func newStorage(tb testing.TB, clients []*client.Persistent) (s *client.Storage) {
tb.Helper() tb.Helper()
s, err := client.NewStorage(&client.StorageConfig{}) ctx := testutil.ContextWithTimeout(tb, testTimeout)
s, err := client.NewStorage(ctx, &client.StorageConfig{
Logger: slogutil.NewDiscardLogger(),
})
require.NoError(tb, err) require.NoError(tb, err)
for _, p := range clients { for _, p := range clients {
p.UID = client.MustNewUID() p.UID = client.MustNewUID()
require.NoError(tb, s.Add(p)) require.NoError(tb, s.Add(ctx, p))
} }
return s return s

View File

@ -118,12 +118,13 @@ func Main(clientBuildFS fs.FS) {
for { for {
sig := <-signals sig := <-signals
log.Info("Received signal %q", sig) log.Info("Received signal %q", sig)
ctx := context.Background()
switch sig { switch sig {
case syscall.SIGHUP: case syscall.SIGHUP:
Context.clients.storage.ReloadARP() Context.clients.storage.ReloadARP(ctx)
Context.tls.reload() Context.tls.reload()
default: default:
cleanup(context.Background()) cleanup(ctx)
cleanupAlways() cleanupAlways()
close(done) close(done)
} }