Dmitrii Stepanov
d433b49265
All checks were successful
DCO action / DCO (pull_request) Successful in 2m40s
Vulncheck / Vulncheck (pull_request) Successful in 3m41s
Build / Build Components (1.20) (pull_request) Successful in 4m27s
Build / Build Components (1.21) (pull_request) Successful in 5m6s
Tests and linters / Staticcheck (pull_request) Successful in 6m16s
Tests and linters / gopls check (pull_request) Successful in 6m23s
Tests and linters / Lint (pull_request) Successful in 6m48s
Tests and linters / Tests (1.20) (pull_request) Successful in 9m4s
Tests and linters / Tests with -race (pull_request) Successful in 9m9s
Tests and linters / Tests (1.21) (pull_request) Successful in 9m23s
`fmt.Errorf can be replaced with errors.New` and `fmt.Sprintf can be replaced with string addition` Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
86 lines
1.9 KiB
Go
86 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"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("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(c.name + " is disabled, skip")
|
|
c.srv = nil
|
|
}
|
|
}
|
|
|
|
func (c *httpComponent) start() {
|
|
if c.srv != nil {
|
|
log.Info("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("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("reload " + c.name)
|
|
if c.needReload() {
|
|
log.Info(c.name + " config updated")
|
|
if err := c.shutdown(); err != nil {
|
|
log.Debug(logs.FrostFSIRCouldNotShutdownHTTPServer,
|
|
zap.String("error", err.Error()),
|
|
)
|
|
} else {
|
|
c.init()
|
|
c.start()
|
|
}
|
|
}
|
|
}
|