all: imp code

This commit is contained in:
Stanislav Chzhen 2024-09-10 17:53:42 +03:00
parent fafd7cbb52
commit 9feda414b6
3 changed files with 23 additions and 7 deletions

View File

@ -9,6 +9,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/AdGuardHome/internal/arpdb" "github.com/AdguardTeam/AdGuardHome/internal/arpdb"
"github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc" "github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc"
"github.com/AdguardTeam/AdGuardHome/internal/whois" "github.com/AdguardTeam/AdGuardHome/internal/whois"
@ -124,6 +125,14 @@ func NewStorage(conf *Config) (s *Storage, err error) {
done: make(chan struct{}), done: make(chan struct{}),
} }
// TODO(s.chzhen): Refactor it.
switch v := s.etcHosts.(type) {
case *aghnet.HostsContainer:
if v == nil {
s.etcHosts = nil
}
}
for i, p := range conf.InitialClients { for i, p := range conf.InitialClients {
err = s.Add(p) err = s.Add(p)
if err != nil { if err != nil {
@ -146,7 +155,7 @@ func (s *Storage) Start(_ context.Context) (err error) {
func (s *Storage) Shutdown(_ context.Context) (err error) { func (s *Storage) Shutdown(_ context.Context) (err error) {
close(s.done) close(s.done)
return nil return s.closeUpstreams()
} }
// periodicARPUpdate periodically reloads runtime clients from ARP. It is // periodicARPUpdate periodically reloads runtime clients from ARP. It is
@ -154,6 +163,9 @@ func (s *Storage) Shutdown(_ context.Context) (err error) {
func (s *Storage) periodicARPUpdate() { func (s *Storage) periodicARPUpdate() {
defer log.OnPanic("storage") defer log.OnPanic("storage")
// Initial ARP refresh.
s.ReloadARP()
t := time.NewTicker(s.arpClientsUpdatePeriod) t := time.NewTicker(s.arpClientsUpdatePeriod)
for { for {
@ -216,7 +228,11 @@ func (s *Storage) handleHostsUpdates() {
for { for {
select { select {
case upd := <-s.etcHosts.Upd(): case upd, ok := <-s.etcHosts.Upd():
if !ok {
return
}
s.addFromHostsFile(upd) s.addFromHostsFile(upd)
case <-s.done: case <-s.done:
return return
@ -490,8 +506,8 @@ func (s *Storage) Size() (n int) {
return s.index.size() return s.index.size()
} }
// CloseUpstreams closes upstream configurations of persistent clients. // closeUpstreams closes upstream configurations of persistent clients.
func (s *Storage) CloseUpstreams() (err error) { func (s *Storage) closeUpstreams() (err error) {
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()

View File

@ -401,6 +401,6 @@ func (clients *clientsContainer) UpdateAddress(ip netip.Addr, host string, info
// close gracefully closes all the client-specific upstream configurations of // close gracefully closes all the client-specific upstream configurations of
// the persistent clients. // the persistent clients.
func (clients *clientsContainer) close() (err error) { func (clients *clientsContainer) close(ctx context.Context) (err error) {
return clients.storage.CloseUpstreams() return clients.storage.Shutdown(ctx)
} }

View File

@ -502,7 +502,7 @@ func stopDNSServer() (err error) {
return fmt.Errorf("stopping forwarding dns server: %w", err) return fmt.Errorf("stopping forwarding dns server: %w", err)
} }
err = Context.clients.close() err = Context.clients.close(context.TODO())
if err != nil { if err != nil {
return fmt.Errorf("closing clients container: %w", err) return fmt.Errorf("closing clients container: %w", err)
} }