From ce84dc7068c0042c6dffd444cf307280234ac183 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Fri, 9 Sep 2022 09:33:31 +0300 Subject: [PATCH] [#200] Reload upload/download settings on SIGHUP Signed-off-by: Denis Kirillov --- app.go | 33 ++++++++++++++++++++++++++++----- downloader/download.go | 17 +++++++++++++---- uploader/upload.go | 41 +++++++++++++++++++++++++++-------------- 3 files changed, 68 insertions(+), 23 deletions(-) diff --git a/app.go b/app.go index e7c5c09..c180353 100644 --- a/app.go +++ b/app.go @@ -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() { diff --git a/downloader/download.go b/downloader/download.go index e34e3b7..09b0f4a 100644 --- a/downloader/download.go +++ b/downloader/download.go @@ -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 } diff --git a/uploader/upload.go b/uploader/upload.go index 4c1172c..0784249 100644 --- a/uploader/upload.go +++ b/uploader/upload.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "strconv" + "sync/atomic" "time" "github.com/nspcc-dev/neofs-http-gw/resolver" @@ -28,12 +29,12 @@ const ( // Uploader is an upload request handler. type Uploader struct { - appCtx context.Context - log *zap.Logger - pool *pool.Pool - ownerID *user.ID - enableDefaultTimestamp bool - containerResolver *resolver.ContainerResolver + appCtx context.Context + log *zap.Logger + pool *pool.Pool + ownerID *user.ID + settings *Settings + containerResolver *resolver.ContainerResolver } type epochDurations struct { @@ -42,16 +43,28 @@ 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, - containerResolver: params.Resolver, + appCtx: ctx, + log: params.Logger, + pool: params.Pool, + ownerID: params.Owner, + 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))