2023-04-26 12:45:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-10-21 07:22:54 +00:00
|
|
|
"context"
|
2023-04-26 12:45:57 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
func (c *httpComponent) init(ctx context.Context) {
|
|
|
|
log.Info(ctx, "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-10-21 13:27:28 +00:00
|
|
|
log.Info(ctx, c.name+" is disabled, skip")
|
2023-04-26 12:45:57 +00:00
|
|
|
c.srv = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
func (c *httpComponent) start(ctx context.Context) {
|
2023-04-26 12:45:57 +00:00
|
|
|
if c.srv != nil {
|
2024-10-21 13:27:28 +00:00
|
|
|
log.Info(ctx, "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())
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
func (c *httpComponent) shutdown(ctx context.Context) error {
|
2023-04-26 12:45:57 +00:00
|
|
|
if c.srv != nil {
|
2024-10-21 13:27:28 +00:00
|
|
|
log.Info(ctx, "shutdown "+c.name)
|
|
|
|
return c.srv.Shutdown(ctx)
|
2023-04-26 12:45:57 +00:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
func (c *httpComponent) reload(ctx context.Context) {
|
|
|
|
log.Info(ctx, "reload "+c.name)
|
2023-04-19 17:02:02 +00:00
|
|
|
if c.needReload() {
|
2024-10-21 13:27:28 +00:00
|
|
|
log.Info(ctx, c.name+" config updated")
|
|
|
|
if err := c.shutdown(ctx); err != nil {
|
|
|
|
log.Debug(ctx, logs.FrostFSIRCouldNotShutdownHTTPServer,
|
2023-04-26 12:45:57 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
} else {
|
2024-10-21 13:27:28 +00:00
|
|
|
c.init(ctx)
|
|
|
|
c.start(ctx)
|
2023-04-26 12:45:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|