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

View file

@ -13,6 +13,7 @@ import (
"path" "path"
"strconv" "strconv"
"strings" "strings"
"sync/atomic"
"time" "time"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
@ -255,15 +256,23 @@ type Downloader struct {
log *zap.Logger log *zap.Logger
pool *pool.Pool pool *pool.Pool
containerResolver *resolver.ContainerResolver containerResolver *resolver.ContainerResolver
settings Settings settings *Settings
} }
type Settings struct { 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. // 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{ return &Downloader{
appCtx: ctx, appCtx: ctx,
log: params.Logger, 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) { func (d *Downloader) addObjectToZip(zw *zip.Writer, obj *object.Object) (io.Writer, error) {
method := zip.Store method := zip.Store
if d.settings.ZipCompression { if d.settings.ZipCompression() {
method = zip.Deflate method = zip.Deflate
} }

View file

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