Pull request: 2552 rm context.TODO() instances

Merge in DNS/adguard-home from 2552-context to master

Closes #2552.

Squashed commit of the following:

commit 3d1cef33da529f4611869c4a0f2f294a3c8afcaf
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Tue Jan 26 19:28:23 2021 +0300

    all: fix docs

commit d08c78cf4b96419b928e73c497768f40c9e47bc2
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Tue Jan 26 19:22:00 2021 +0300

    all: doc changes

commit c2814f4d0025be74f38299e7e66e7c0193b6c15f
Merge: 100a1a09 44c7221a
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Tue Jan 26 19:12:55 2021 +0300

    Merge branch 'master' into 2552-context

commit 100a1a0957bc22bfaccb1693e6b9b1c5cb53ed13
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Tue Jan 26 19:10:03 2021 +0300

    home: imp docs, fix naming

commit 22717abe6c0e4c1016a53ff2fac1689d0762c462
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Tue Jan 26 18:14:07 2021 +0300

    home: improve code quality

commit 5c96f77a2b315e2c1ad4a11cc7a64f61bdba52a3
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Jan 25 20:28:51 2021 +0300

    home: add docs

commit 323fc013a57a5c06ec391003133b12f4eb2721cd
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Jan 25 14:50:11 2021 +0300

    home: rm context.TODO() instances
This commit is contained in:
Eugene Burkov
2021-01-26 19:44:19 +03:00
parent 44c7221ae9
commit c215b82004
9 changed files with 106 additions and 58 deletions

View File

@@ -13,6 +13,7 @@ import (
"runtime"
"strconv"
"strings"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/util"
@@ -268,6 +269,9 @@ func copyInstallSettings(dst, src *configuration) {
dst.DNS.Port = src.DNS.Port
}
// shutdownTimeout is the timeout for shutting HTTP server down operation.
const shutdownTimeout = 5 * time.Second
// Apply new configuration, start DNS server, restart Web server
func (web *Web) handleInstallConfigure(w http.ResponseWriter, r *http.Request) {
newSettings := applyConfigReq{}
@@ -320,6 +324,10 @@ func (web *Web) handleInstallConfigure(w http.ResponseWriter, r *http.Request) {
config.DNS.BindHost = newSettings.DNS.IP
config.DNS.Port = newSettings.DNS.Port
// TODO(e.burkov): StartMods() should be put in a separate goroutine at
// the moment we'll allow setting up TLS in the initial configuration or
// the configuration itself will use HTTPS protocol, because the
// underlying functions potentially restart the HTTPS server.
err = StartMods()
if err != nil {
Context.firstRun = true
@@ -351,16 +359,22 @@ func (web *Web) handleInstallConfigure(w http.ResponseWriter, r *http.Request) {
f.Flush()
}
// this needs to be done in a goroutine because Shutdown() is a blocking call, and it will block
// until all requests are finished, and _we_ are inside a request right now, so it will block indefinitely
// The Shutdown() method of (*http.Server) needs to be called in a
// separate goroutine, because it waits until all requests are handled
// and will be blocked by it's own caller.
if restartHTTP {
go func() {
_ = web.httpServer.Shutdown(context.TODO())
}()
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
shut := func(srv *http.Server) {
defer cancel()
err := srv.Shutdown(ctx)
if err != nil {
log.Debug("error while shutting down HTTP server: %s", err)
}
}
go shut(web.httpServer)
if web.httpServerBeta != nil {
go func() {
_ = web.httpServerBeta.Shutdown(context.TODO())
}()
go shut(web.httpServerBeta)
}
}
}