frostfs-node/cmd/frostfs-ir/httpcomponent.go

81 lines
1.8 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"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
addressKey string
name string
handler http.Handler
shutdownDur time.Duration
shutdownTimeoutKey string
enabled bool
enabledKey string
}
func (c *httpComponent) init() {
log.Info(fmt.Sprintf("init %s", c.name))
c.enabled = cfg.GetBool(c.enabledKey)
c.address = cfg.GetString(c.addressKey)
c.shutdownDur = cfg.GetDuration(c.shutdownTimeoutKey)
if c.enabled {
c.srv = httputil.New(
httputil.HTTPSrvPrm{
Address: c.address,
Handler: c.handler,
},
httputil.WithShutdownTimeout(c.shutdownDur),
)
} else {
log.Info(fmt.Sprintf("%s is disabled, skip", c.name))
c.srv = nil
}
}
func (c *httpComponent) start() {
if c.srv != nil {
log.Info(fmt.Sprintf("start %s", c.name))
wg.Add(1)
go func() {
exitErr(c.srv.Serve())
wg.Done()
}()
}
}
func (c *httpComponent) shutdown() error {
if c.srv != nil {
log.Info(fmt.Sprintf("shutdown %s", c.name))
return c.srv.Shutdown()
}
return nil
}
func (c *httpComponent) reload() {
log.Info(fmt.Sprintf("reload %s", c.name))
enabled := cfg.GetBool(c.enabledKey)
address := cfg.GetString(c.addressKey)
dur := cfg.GetDuration(c.shutdownTimeoutKey)
if enabled != c.enabled || address != c.address || dur != c.shutdownDur {
log.Info(fmt.Sprintf("%s config updated", c.name))
if err := c.shutdown(); err != nil {
log.Debug(logs.FrostFSIRCouldNotShutdownHTTPServer,
zap.String("error", err.Error()),
)
} else {
c.init()
c.start()
}
}
}