Aleksey Savchuk
f0c43c8d80
All checks were successful
Vulncheck / Vulncheck (pull_request) Successful in 3m1s
Pre-commit hooks / Pre-commit (pull_request) Successful in 3m29s
Tests and linters / gopls check (pull_request) Successful in 3m50s
Tests and linters / Lint (pull_request) Successful in 4m35s
DCO action / DCO (pull_request) Successful in 5m12s
Tests and linters / Run gofumpt (pull_request) Successful in 5m33s
Build / Build Components (pull_request) Successful in 5m45s
Tests and linters / Tests with -race (pull_request) Successful in 6m37s
Tests and linters / Tests (pull_request) Successful in 7m17s
Tests and linters / Staticcheck (pull_request) Successful in 7m36s
Tests and linters / Run gofumpt (push) Successful in 1m22s
Tests and linters / Staticcheck (push) Successful in 3m19s
Tests and linters / Lint (push) Successful in 4m35s
Vulncheck / Vulncheck (push) Successful in 5m20s
Build / Build Components (push) Successful in 6m16s
Pre-commit hooks / Pre-commit (push) Successful in 6m37s
Tests and linters / Tests (push) Successful in 6m48s
Tests and linters / Tests with -race (push) Successful in 7m15s
Tests and linters / gopls check (push) Successful in 7m27s
Use `zap.Error` instead of `zap.String` for logging errors: change all expressions like `zap.String("error", err.Error())` or `zap.String("err", err.Error())` to `zap.Error(err)`. Leave similar expressions with other messages unchanged, for example, `zap.String("last_error", lastErr.Error())` or `zap.String("reason", ctx.Err().Error())`. This change was made by applying the following patch: ```diff @@ var err expression @@ -zap.String("error", err.Error()) +zap.Error(err) @@ var err expression @@ -zap.String("err", err.Error()) +zap.Error(err) ``` Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
87 lines
2 KiB
Go
87 lines
2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
|
httputil "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/http"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type httpComponent struct {
|
|
srv *httputil.Server
|
|
address string
|
|
name string
|
|
handler http.Handler
|
|
shutdownDur time.Duration
|
|
enabled bool
|
|
}
|
|
|
|
const (
|
|
enabledKeyPostfix = ".enabled"
|
|
addressKeyPostfix = ".address"
|
|
shutdownTimeoutKeyPostfix = ".shutdown_timeout"
|
|
)
|
|
|
|
func (c *httpComponent) init(ctx context.Context) {
|
|
log.Info(ctx, "init "+c.name)
|
|
c.enabled = cfg.GetBool(c.name + enabledKeyPostfix)
|
|
c.address = cfg.GetString(c.name + addressKeyPostfix)
|
|
c.shutdownDur = cfg.GetDuration(c.name + shutdownTimeoutKeyPostfix)
|
|
|
|
if c.enabled {
|
|
c.srv = httputil.New(
|
|
httputil.HTTPSrvPrm{
|
|
Address: c.address,
|
|
Handler: c.handler,
|
|
},
|
|
httputil.WithShutdownTimeout(c.shutdownDur),
|
|
)
|
|
} else {
|
|
log.Info(ctx, c.name+" is disabled, skip")
|
|
c.srv = nil
|
|
}
|
|
}
|
|
|
|
func (c *httpComponent) start(ctx context.Context) {
|
|
if c.srv != nil {
|
|
log.Info(ctx, "start "+c.name)
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
exitErr(c.srv.Serve())
|
|
}()
|
|
}
|
|
}
|
|
|
|
func (c *httpComponent) shutdown(ctx context.Context) error {
|
|
if c.srv != nil {
|
|
log.Info(ctx, "shutdown "+c.name)
|
|
return c.srv.Shutdown(ctx)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *httpComponent) needReload() bool {
|
|
enabled := cfg.GetBool(c.name + enabledKeyPostfix)
|
|
address := cfg.GetString(c.name + addressKeyPostfix)
|
|
dur := cfg.GetDuration(c.name + shutdownTimeoutKeyPostfix)
|
|
return enabled != c.enabled || enabled && (address != c.address || dur != c.shutdownDur)
|
|
}
|
|
|
|
func (c *httpComponent) reload(ctx context.Context) {
|
|
log.Info(ctx, "reload "+c.name)
|
|
if c.needReload() {
|
|
log.Info(ctx, c.name+" config updated")
|
|
if err := c.shutdown(ctx); err != nil {
|
|
log.Debug(ctx, logs.FrostFSIRCouldNotShutdownHTTPServer,
|
|
zap.Error(err),
|
|
)
|
|
} else {
|
|
c.init(ctx)
|
|
c.start(ctx)
|
|
}
|
|
}
|
|
}
|