Skip to content

Commit

Permalink
all: imp code, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Oct 18, 2022
1 parent 011fde1 commit f924bc7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
5 changes: 3 additions & 2 deletions internal/aghtest/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func RespondTo(t testing.TB, req *dns.Msg, cl, qt uint16, targ, answer string) (
return resp
}

// Exchange implements the upstream.Upstream interface for *Upstream.
// Exchange implements the [upstream.Upstream] interface for *Upstream.
//
// TODO(a.garipov): Split further into handlers.
func (u *Upstream) Exchange(m *dns.Msg) (resp *dns.Msg, err error) {
Expand Down Expand Up @@ -117,11 +117,12 @@ func (u *Upstream) Exchange(m *dns.Msg) (resp *dns.Msg, err error) {
return resp, nil
}

// Address implements upstream.Upstream interface for *Upstream.
// Address implements [upstream.Upstream] interface for *Upstream.
func (u *Upstream) Address() string {
return u.Addr
}

// Close implements [upstream.Upstream] interface for *Upstream.
func (u *Upstream) Close() (err error) {
return nil
}
Expand Down
38 changes: 38 additions & 0 deletions internal/home/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/netutil"
"github.com/AdguardTeam/golibs/stringutil"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)

const clientsUpdatePeriod = 10 * time.Minute
Expand Down Expand Up @@ -50,6 +52,18 @@ type Client struct {
UseOwnBlockedServices bool
}

// closeUpstreams closes the client-specific upstream config of c if any.
func (c *Client) closeUpstreams() (err error) {
if c.upstreamConfig != nil {
err = c.upstreamConfig.Close()
if err != nil {
return fmt.Errorf("closing upstreams of client %q: %w", c.Name, err)
}
}

return nil
}

type clientSource uint

// Client sources. The order determines the priority.
Expand Down Expand Up @@ -651,6 +665,10 @@ func (clients *clientsContainer) Del(name string) (ok bool) {
return false
}

if err := c.closeUpstreams(); err != nil {
log.Error("client container: removing client %s: %s", name, err)
}

// update Name index
delete(clients.list, name)

Expand Down Expand Up @@ -910,3 +928,23 @@ func (clients *clientsContainer) updateFromDHCP(add bool) {

log.Debug("clients: added %d client aliases from dhcp", n)
}

// Close implements the [io.Closer] interface for *clientsContainer.
func (clients *clientsContainer) Close() (err error) {
persistent := maps.Values(clients.list)
slices.SortFunc(persistent, func(a, b *Client) bool { return a.Name < b.Name })

var errs []error

for _, cli := range persistent {
if err = cli.closeUpstreams(); err != nil {
errs = append(errs, err)
}
}

if len(errs) > 0 {
return errors.List("closing client specific upstreams", errs...)
}

return nil
}
12 changes: 9 additions & 3 deletions internal/home/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,17 +431,23 @@ func reconfigureDNSServer() (err error) {
return nil
}

func stopDNSServer() error {
func stopDNSServer() (err error) {
if !isRunning() {
return nil
}

err := Context.dnsServer.Stop()
err = Context.dnsServer.Stop()
if err != nil {
return fmt.Errorf("couldn't stop forwarding DNS server: %w", err)
return fmt.Errorf("stopping forwarding dns server: %w", err)
}

err = Context.clients.Close()
if err != nil {
return fmt.Errorf("closing clients container: %w", err)
}

closeDNSServer()

return nil
}

Expand Down
1 change: 0 additions & 1 deletion internal/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ func Main(clientBuildFS fs.FS) {
case syscall.SIGHUP:
Context.clients.Reload()
Context.tls.reload()

default:
cleanup(context.Background())
cleanupAlways()
Expand Down

0 comments on commit f924bc7

Please sign in to comment.