Dmitrii Stepanov
692749dba1
Some checks failed
DCO action / DCO (pull_request) Failing after 1m30s
Tests and linters / Run gofumpt (pull_request) Successful in 1m26s
Vulncheck / Vulncheck (pull_request) Successful in 2m11s
Pre-commit hooks / Pre-commit (pull_request) Successful in 2m25s
Build / Build Components (pull_request) Successful in 2m29s
Tests and linters / Lint (pull_request) Failing after 2m26s
Tests and linters / gopls check (pull_request) Successful in 2m43s
Tests and linters / Staticcheck (pull_request) Successful in 2m56s
Tests and linters / Tests (pull_request) Successful in 4m22s
Tests and linters / Tests with -race (pull_request) Successful in 5m58s
Signed-off-by: Dmitrii Stepanov <d.stepanov@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() {
|
|
log.Info(context.Background(), "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(context.Background(), c.name+" is disabled, skip")
|
|
c.srv = nil
|
|
}
|
|
}
|
|
|
|
func (c *httpComponent) start() {
|
|
if c.srv != nil {
|
|
log.Info(context.Background(), "start "+c.name)
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
exitErr(c.srv.Serve())
|
|
}()
|
|
}
|
|
}
|
|
|
|
func (c *httpComponent) shutdown() error {
|
|
if c.srv != nil {
|
|
log.Info(context.Background(), "shutdown "+c.name)
|
|
return c.srv.Shutdown()
|
|
}
|
|
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() {
|
|
log.Info(context.Background(), "reload "+c.name)
|
|
if c.needReload() {
|
|
log.Info(context.Background(), c.name+" config updated")
|
|
if err := c.shutdown(); err != nil {
|
|
log.Debug(context.Background(), logs.FrostFSIRCouldNotShutdownHTTPServer,
|
|
zap.String("error", err.Error()),
|
|
)
|
|
} else {
|
|
c.init()
|
|
c.start()
|
|
}
|
|
}
|
|
}
|