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"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/AdGuardHome/internal/arpdb"
"github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc"
"github.com/AdguardTeam/AdGuardHome/internal/whois"
@ -124,6 +125,14 @@ func NewStorage(conf *Config) (s *Storage, err error) {
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 {
err = s.Add(p)
if err != nil {
@ -146,7 +155,7 @@ func (s *Storage) Start(_ context.Context) (err error) {
func (s *Storage) Shutdown(_ context.Context) (err error) {
close(s.done)
return nil
return s.closeUpstreams()
}
// 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() {
defer log.OnPanic("storage")
// Initial ARP refresh.
s.ReloadARP()
t := time.NewTicker(s.arpClientsUpdatePeriod)
for {
@ -216,7 +228,11 @@ func (s *Storage) handleHostsUpdates() {
for {
select {
case upd := <-s.etcHosts.Upd():
case upd, ok := <-s.etcHosts.Upd():
if !ok {
return
}
s.addFromHostsFile(upd)
case <-s.done:
return
@ -490,8 +506,8 @@ func (s *Storage) Size() (n int) {
return s.index.size()
}
// CloseUpstreams closes upstream configurations of persistent clients.
func (s *Storage) CloseUpstreams() (err error) {
// closeUpstreams closes upstream configurations of persistent clients.
func (s *Storage) closeUpstreams() (err error) {
s.mu.Lock()
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
// the persistent clients.
func (clients *clientsContainer) close() (err error) {
return clients.storage.CloseUpstreams()
func (clients *clientsContainer) close(ctx context.Context) (err error) {
return clients.storage.Shutdown(ctx)
}

View file

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