Skip to content

Commit

Permalink
Pull request: close-upstream-conf
Browse files Browse the repository at this point in the history
Merge in DNS/dnsproxy from close-upstream-conf to master

Squashed commit of the following:

commit 69175c3
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Oct 25 17:23:12 2022 +0300

    proxy: add todo

commit d5d1996
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Oct 25 17:06:59 2022 +0300

    proxy: fix docs

commit d815679
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Oct 25 17:05:04 2022 +0300

    proxy: close upstream conf beter
  • Loading branch information
EugeneOne1 committed Oct 25, 2022
1 parent cc4140d commit ad02cde
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
33 changes: 16 additions & 17 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,16 @@ func (p *Proxy) Start() (err error) {
return nil
}

// closeAll closes all elements in the toClose slice and if there's any error
// appends it to the errs slice.
func closeAll[T io.Closer](toClose []T, errs *[]error) {
for _, c := range toClose {
// closeAll closes all closers and appends the occurred errors to errs.
func closeAll[C io.Closer](errs []error, closers ...C) (appended []error) {
for _, c := range closers {
err := c.Close()
if err != nil {
*errs = append(*errs, err)
errs = append(errs, err)
}
}

return errs
}

// Stop stops the proxy server including all its listeners
Expand All @@ -225,44 +226,42 @@ func (p *Proxy) Stop() error {
return nil
}

errs := []error{}

closeAll(p.tcpListen, &errs)
errs := closeAll(nil, p.tcpListen...)
p.tcpListen = nil

closeAll(p.udpListen, &errs)
errs = closeAll(errs, p.udpListen...)
p.udpListen = nil

closeAll(p.tlsListen, &errs)
errs = closeAll(errs, p.tlsListen...)
p.tlsListen = nil

if p.httpsServer != nil {
closeAll([]io.Closer{p.httpsServer}, &errs)
errs = closeAll(errs, p.httpsServer)
p.httpsServer = nil

// No need to close these since they're closed by httpsServer.Close().
p.httpsListen = nil
}

if p.h3Server != nil {
closeAll([]io.Closer{p.h3Server}, &errs)
errs = closeAll(errs, p.h3Server)
p.h3Server = nil
}

closeAll(p.h3Listen, &errs)
errs = closeAll(errs, p.h3Listen...)
p.h3Listen = nil

closeAll(p.quicListen, &errs)
errs = closeAll(errs, p.quicListen...)
p.quicListen = nil

closeAll(p.dnsCryptUDPListen, &errs)
errs = closeAll(errs, p.dnsCryptUDPListen...)
p.dnsCryptUDPListen = nil

closeAll(p.dnsCryptTCPListen, &errs)
errs = closeAll(errs, p.dnsCryptTCPListen...)
p.dnsCryptTCPListen = nil

if p.UpstreamConfig != nil {
closeAll([]io.Closer{p.UpstreamConfig}, &errs)
errs = closeAll(errs, p.UpstreamConfig)
}

p.started = false
Expand Down
20 changes: 18 additions & 2 deletions proxy/upstreams.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package proxy
import (
"fmt"
"io"
"sort"
"strings"

"github.com/AdguardTeam/dnsproxy/upstream"
Expand Down Expand Up @@ -224,8 +225,23 @@ func (uc *UpstreamConfig) getUpstreamsForDomain(host string) (ups []upstream.Ups

// Close implements the io.Closer interface for *UpstreamConfig.
func (uc *UpstreamConfig) Close() (err error) {
closeErrs := []error{}
closeAll(uc.Upstreams, &closeErrs)
closeErrs := closeAll(nil, uc.Upstreams...)

for _, specUps := range []map[string][]upstream.Upstream{
uc.DomainReservedUpstreams,
uc.SpecifiedDomainUpstreams,
} {
domains := make([]string, 0, len(specUps))
for domain := range specUps {
domains = append(domains, domain)
}
// TODO(e.burkov): Use functions from golang.org/x/exp.
sort.Stable(sort.StringSlice(domains))

for _, domain := range domains {
closeErrs = closeAll(closeErrs, specUps[domain]...)
}
}

if len(closeErrs) > 0 {
return errors.List("failed to close some upstreams", closeErrs...)
Expand Down

0 comments on commit ad02cde

Please sign in to comment.