[#200] Reload upload/download settings on SIGHUP

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2022-09-09 09:33:31 +03:00 committed by Kirillov Denis
parent 1e05d8a935
commit ce84dc7068
3 changed files with 68 additions and 23 deletions

33
app.go
View file

@ -41,6 +41,12 @@ type (
resolver *resolver.ContainerResolver
metrics *gateMetrics
services []*metrics.Service
settings *appSettings
}
appSettings struct {
Uploader *uploader.Settings
Downloader *downloader.Settings
}
// App is an interface for the main gateway function.
@ -158,12 +164,22 @@ func newApp(ctx context.Context, opt ...Option) App {
a.log.Fatal("failed to dial pool", zap.Error(err))
}
a.initAppSettings()
a.initResolver()
a.initMetrics()
return a
}
func (a *app) initAppSettings() {
a.settings = &appSettings{
Uploader: &uploader.Settings{},
Downloader: &downloader.Settings{},
}
a.updateSettings()
}
func (a *app) initResolver() {
var err error
a.resolver, err = resolver.NewContainerResolver(a.getResolverConfig())
@ -309,10 +325,8 @@ func (a *app) setHealthStatus() {
}
func (a *app) Serve(ctx context.Context) {
edts := a.cfg.GetBool(cfgUploaderHeaderEnableDefaultTimestamp)
uploadRoutes := uploader.New(ctx, a.AppParams(), edts)
downloadSettings := downloader.Settings{ZipCompression: a.cfg.GetBool(cfgZipCompression)}
downloadRoutes := downloader.New(ctx, a.AppParams(), downloadSettings)
uploadRoutes := uploader.New(ctx, a.AppParams(), a.settings.Uploader)
downloadRoutes := downloader.New(ctx, a.AppParams(), a.settings.Downloader)
// Configure router.
a.configureRouter(uploadRoutes, downloadRoutes)
@ -358,7 +372,7 @@ LOOP:
}
func (a *app) configReload() {
a.log.Info("SIGHUP config reload")
a.log.Info("SIGHUP config reload started")
if !a.cfg.IsSet(cmdConfig) {
a.log.Warn("failed to reload config because it's missed")
return
@ -380,8 +394,17 @@ func (a *app) configReload() {
a.stopServices()
a.startServices()
a.updateSettings()
a.metrics.SetEnabled(a.cfg.GetBool(cfgPrometheusEnabled))
a.setHealthStatus()
a.log.Info("SIGHUP config reload completed")
}
func (a *app) updateSettings() {
a.settings.Uploader.SetDefaultTimestamp(a.cfg.GetBool(cfgUploaderHeaderEnableDefaultTimestamp))
a.settings.Downloader.SetZipCompression(a.cfg.GetBool(cfgZipCompression))
}
func (a *app) startServices() {

View file

@ -13,6 +13,7 @@ import (
"path"
"strconv"
"strings"
"sync/atomic"
"time"
"unicode"
"unicode/utf8"
@ -255,15 +256,23 @@ type Downloader struct {
log *zap.Logger
pool *pool.Pool
containerResolver *resolver.ContainerResolver
settings Settings
settings *Settings
}
type Settings struct {
ZipCompression bool
zipCompression atomic.Bool
}
func (s *Settings) ZipCompression() bool {
return s.zipCompression.Load()
}
func (s *Settings) SetZipCompression(val bool) {
s.zipCompression.Store(val)
}
// New creates an instance of Downloader using specified options.
func New(ctx context.Context, params *utils.AppParams, settings Settings) *Downloader {
func New(ctx context.Context, params *utils.AppParams, settings *Settings) *Downloader {
return &Downloader{
appCtx: ctx,
log: params.Logger,
@ -385,7 +394,7 @@ func (d *Downloader) search(c *fasthttp.RequestCtx, cid *cid.ID, key, val string
func (d *Downloader) addObjectToZip(zw *zip.Writer, obj *object.Object) (io.Writer, error) {
method := zip.Store
if d.settings.ZipCompression {
if d.settings.ZipCompression() {
method = zip.Deflate
}

View file

@ -6,6 +6,7 @@ import (
"fmt"
"io"
"strconv"
"sync/atomic"
"time"
"github.com/nspcc-dev/neofs-http-gw/resolver"
@ -32,7 +33,7 @@ type Uploader struct {
log *zap.Logger
pool *pool.Pool
ownerID *user.ID
enableDefaultTimestamp bool
settings *Settings
containerResolver *resolver.ContainerResolver
}
@ -42,15 +43,27 @@ type epochDurations struct {
blockPerEpoch uint64
}
type Settings struct {
defaultTimestamp atomic.Bool
}
func (s *Settings) DefaultTimestamp() bool {
return s.defaultTimestamp.Load()
}
func (s *Settings) SetDefaultTimestamp(val bool) {
s.defaultTimestamp.Store(val)
}
// New creates a new Uploader using specified logger, connection pool and
// other options.
func New(ctx context.Context, params *utils.AppParams, enableDefaultTimestamp bool) *Uploader {
func New(ctx context.Context, params *utils.AppParams, settings *Settings) *Uploader {
return &Uploader{
appCtx: ctx,
log: params.Logger,
pool: params.Pool,
ownerID: params.Owner,
enableDefaultTimestamp: enableDefaultTimestamp,
settings: settings,
containerResolver: params.Resolver,
}
}
@ -130,7 +143,7 @@ func (u *Uploader) Upload(c *fasthttp.RequestCtx) {
attributes = append(attributes, *filename)
}
// sets Timestamp attribute if it wasn't set from header and enabled by settings
if _, ok := filtered[object.AttributeTimestamp]; !ok && u.enableDefaultTimestamp {
if _, ok := filtered[object.AttributeTimestamp]; !ok && u.settings.DefaultTimestamp() {
timestamp := object.NewAttribute()
timestamp.SetKey(object.AttributeTimestamp)
timestamp.SetValue(strconv.FormatInt(time.Now().Unix(), 10))