2023-04-26 12:45:57 +00:00
|
|
|
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 {
|
2023-05-12 08:04:52 +00:00
|
|
|
srv *httputil.Server
|
|
|
|
address string
|
|
|
|
name string
|
|
|
|
handler http.Handler
|
|
|
|
shutdownDur time.Duration
|
|
|
|
enabled bool
|
2023-04-26 12:45:57 +00:00
|
|
|
}
|
|
|
|
|
2023-05-12 08:04:52 +00:00
|
|
|
const (
|
|
|
|
enabledKeyPostfix = ".enabled"
|
|
|
|
addressKeyPostfix = ".address"
|
|
|
|
shutdownTimeoutKeyPostfix = ".shutdown_timeout"
|
|
|
|
)
|
|
|
|
|
2023-04-26 12:45:57 +00:00
|
|
|
func (c *httpComponent) init() {
|
2024-03-11 14:55:50 +00:00
|
|
|
log.Info("init " + c.name)
|
2023-05-12 08:04:52 +00:00
|
|
|
c.enabled = cfg.GetBool(c.name + enabledKeyPostfix)
|
|
|
|
c.address = cfg.GetString(c.name + addressKeyPostfix)
|
|
|
|
c.shutdownDur = cfg.GetDuration(c.name + shutdownTimeoutKeyPostfix)
|
2023-04-26 12:45:57 +00:00
|
|
|
|
|
|
|
if c.enabled {
|
|
|
|
c.srv = httputil.New(
|
|
|
|
httputil.HTTPSrvPrm{
|
|
|
|
Address: c.address,
|
|
|
|
Handler: c.handler,
|
|
|
|
},
|
|
|
|
httputil.WithShutdownTimeout(c.shutdownDur),
|
|
|
|
)
|
|
|
|
} else {
|
2024-03-11 14:55:50 +00:00
|
|
|
log.Info(c.name + " is disabled, skip")
|
2023-04-26 12:45:57 +00:00
|
|
|
c.srv = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *httpComponent) start() {
|
|
|
|
if c.srv != nil {
|
2024-03-11 14:55:50 +00:00
|
|
|
log.Info("start " + c.name)
|
2023-04-26 12:45:57 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2023-05-12 08:38:43 +00:00
|
|
|
defer wg.Done()
|
2023-04-26 12:45:57 +00:00
|
|
|
exitErr(c.srv.Serve())
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *httpComponent) shutdown() error {
|
|
|
|
if c.srv != nil {
|
2024-03-11 14:55:50 +00:00
|
|
|
log.Info("shutdown " + c.name)
|
2023-04-26 12:45:57 +00:00
|
|
|
return c.srv.Shutdown()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-19 17:02:02 +00:00
|
|
|
func (c *httpComponent) needReload() bool {
|
2023-05-12 08:04:52 +00:00
|
|
|
enabled := cfg.GetBool(c.name + enabledKeyPostfix)
|
|
|
|
address := cfg.GetString(c.name + addressKeyPostfix)
|
|
|
|
dur := cfg.GetDuration(c.name + shutdownTimeoutKeyPostfix)
|
2023-04-19 17:02:02 +00:00
|
|
|
return enabled != c.enabled || enabled && (address != c.address || dur != c.shutdownDur)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *httpComponent) reload() {
|
2024-03-11 14:55:50 +00:00
|
|
|
log.Info("reload " + c.name)
|
2023-04-19 17:02:02 +00:00
|
|
|
if c.needReload() {
|
2024-03-11 14:55:50 +00:00
|
|
|
log.Info(c.name + " config updated")
|
2023-04-26 12:45:57 +00:00
|
|
|
if err := c.shutdown(); err != nil {
|
|
|
|
log.Debug(logs.FrostFSIRCouldNotShutdownHTTPServer,
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
c.init()
|
|
|
|
c.start()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|