home: imp code

This commit is contained in:
Stanislav Chzhen 2024-12-04 18:33:35 +03:00
parent 035477513f
commit f3af1bf3dd
2 changed files with 24 additions and 25 deletions

View File

@ -20,7 +20,6 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/AdGuardHome/internal/version"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/quic-go/quic-go/http3"
)
@ -344,9 +343,9 @@ func copyInstallSettings(dst, src *configuration) {
// shutdownTimeout is the timeout for shutting HTTP server down operation.
const shutdownTimeout = 5 * time.Second
// shutdownSrv shuts srv down and prints error messages to the log.
func shutdownSrv(ctx context.Context, srv *http.Server) {
defer log.OnPanic("")
// shutdownSrv shuts down srv and logs the error, if any. l must not be nil.
func shutdownSrv(ctx context.Context, l *slog.Logger, srv *http.Server) {
defer slogutil.RecoverAndLog(ctx, l)
if srv == nil {
return
@ -357,19 +356,19 @@ func shutdownSrv(ctx context.Context, srv *http.Server) {
return
}
const msgFmt = "shutting down http server %q: %s"
if errors.Is(err, context.Canceled) {
log.Debug(msgFmt, srv.Addr, err)
} else {
log.Error(msgFmt, srv.Addr, err)
lvl := slog.LevelDebug
if !errors.Is(err, context.Canceled) {
lvl = slog.LevelError
}
l.Log(ctx, lvl, "shutting down http server", "addr", srv.Addr, slogutil.KeyError, err)
}
// shutdownSrv3 shuts srv down and prints error messages to the log.
// shutdownSrv3 shuts down srv and logs the error, if any. l must not be nil.
//
// TODO(a.garipov): Think of a good way to merge with [shutdownSrv].
func shutdownSrv3(srv *http3.Server) {
defer log.OnPanic("")
func shutdownSrv3(ctx context.Context, l *slog.Logger, srv *http3.Server) {
defer slogutil.RecoverAndLog(ctx, l)
if srv == nil {
return
@ -380,12 +379,12 @@ func shutdownSrv3(srv *http3.Server) {
return
}
const msgFmt = "shutting down http/3 server %q: %s"
if errors.Is(err, context.Canceled) {
log.Debug(msgFmt, srv.Addr, err)
} else {
log.Error(msgFmt, srv.Addr, err)
lvl := slog.LevelDebug
if !errors.Is(err, context.Canceled) {
lvl = slog.LevelError
}
l.Log(ctx, lvl, "shutting down http/3 server", "addr", srv.Addr, slogutil.KeyError, err)
}
// PasswordMinRunes is the minimum length of user's password in runes.
@ -493,7 +492,7 @@ func (web *webAPI) handleInstallConfigure(w http.ResponseWriter, r *http.Request
defer slogutil.RecoverAndLog(ctx, web.logger)
defer cancel()
shutdownSrv(ctx, web.httpServer)
shutdownSrv(ctx, web.logger, web.httpServer)
}(shutdownTimeout)
}

View File

@ -112,8 +112,8 @@ type webAPI struct {
httpsServer httpsServer
}
// newWebAPI creates a new instance of the web UI and API server. baseLogger
// must not be nil.
// newWebAPI creates a new instance of the web UI and API server. conf must not
// be nil and must be valid
//
// TODO(a.garipov): Return a proper error.
func newWebAPI(ctx context.Context, conf *webConfig) (w *webAPI) {
@ -193,8 +193,8 @@ func (web *webAPI) tlsConfigChanged(ctx context.Context, tlsConf tlsConfigSettin
if web.httpsServer.server != nil {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, shutdownTimeout)
shutdownSrv(ctx, web.httpsServer.server)
shutdownSrv3(web.httpsServer.server3)
shutdownSrv(ctx, web.logger, web.httpsServer.server)
shutdownSrv3(ctx, web.logger, web.httpsServer.server3)
cancel()
}
@ -263,9 +263,9 @@ func (web *webAPI) close(ctx context.Context) {
ctx, cancel = context.WithTimeout(ctx, shutdownTimeout)
defer cancel()
shutdownSrv(ctx, web.httpsServer.server)
shutdownSrv3(web.httpsServer.server3)
shutdownSrv(ctx, web.httpServer)
shutdownSrv(ctx, web.logger, web.httpsServer.server)
shutdownSrv3(ctx, web.logger, web.httpsServer.server3)
shutdownSrv(ctx, web.logger, web.httpServer)
web.logger.InfoContext(ctx, "stopped http server")
}