From e26577e753c963258af7f5aa30241f404bbbc3b9 Mon Sep 17 00:00:00 2001 From: Marina Biryukova Date: Tue, 5 Sep 2023 18:17:22 +0300 Subject: [PATCH] [#74] Replace atomics with mutex for reloadable params Signed-off-by: Marina Biryukova --- cmd/http-gw/app.go | 39 ++++++++++++++++++++++++++++++++---- internal/handler/download.go | 2 +- internal/handler/handler.go | 34 ++++++++----------------------- internal/handler/upload.go | 2 +- 4 files changed, 45 insertions(+), 32 deletions(-) diff --git a/cmd/http-gw/app.go b/cmd/http-gw/app.go index 40db433..e8ac917 100644 --- a/cmd/http-gw/app.go +++ b/cmd/http-gw/app.go @@ -50,7 +50,7 @@ type ( resolver *resolver.ContainerResolver metrics *gateMetrics services []*metrics.Service - settings *handler.Settings + settings *appSettings servers []Server } @@ -69,6 +69,13 @@ type ( mu sync.RWMutex enabled bool } + + // appSettings stores reloading parameters, so it has to provide getters and setters which use RWMutex. + appSettings struct { + mu sync.RWMutex + defaultTimestamp bool + zipCompression bool + } ) // WithLogger returns Option to set a specific logger. @@ -133,8 +140,32 @@ func newApp(ctx context.Context, opt ...Option) App { return a } +func (s *appSettings) DefaultTimestamp() bool { + s.mu.RLock() + defer s.mu.RUnlock() + return s.defaultTimestamp +} + +func (s *appSettings) setDefaultTimestamp(val bool) { + s.mu.Lock() + s.defaultTimestamp = val + s.mu.Unlock() +} + +func (s *appSettings) ZipCompression() bool { + s.mu.RLock() + defer s.mu.RUnlock() + return s.zipCompression +} + +func (s *appSettings) setZipCompression(val bool) { + s.mu.Lock() + s.zipCompression = val + s.mu.Unlock() +} + func (a *app) initAppSettings() { - a.settings = &handler.Settings{} + a.settings = &appSettings{} a.updateSettings() } @@ -415,8 +446,8 @@ func (a *app) configReload(ctx context.Context) { } func (a *app) updateSettings() { - a.settings.SetDefaultTimestamp(a.cfg.GetBool(cfgUploaderHeaderEnableDefaultTimestamp)) - a.settings.SetZipCompression(a.cfg.GetBool(cfgZipCompression)) + a.settings.setDefaultTimestamp(a.cfg.GetBool(cfgUploaderHeaderEnableDefaultTimestamp)) + a.settings.setZipCompression(a.cfg.GetBool(cfgZipCompression)) } func (a *app) startServices() { diff --git a/internal/handler/download.go b/internal/handler/download.go index 8ee76bf..696c57e 100644 --- a/internal/handler/download.go +++ b/internal/handler/download.go @@ -72,7 +72,7 @@ func (h *Handler) getContainer(ctx context.Context, cnrID cid.ID) (container.Con func (h *Handler) addObjectToZip(zw *zip.Writer, obj *object.Object) (io.Writer, error) { method := zip.Store - if h.settings.ZipCompression() { + if h.config.ZipCompression() { method = zip.Deflate } diff --git a/internal/handler/handler.go b/internal/handler/handler.go index d462280..579a55f 100644 --- a/internal/handler/handler.go +++ b/internal/handler/handler.go @@ -5,7 +5,6 @@ import ( "errors" "io" "net/url" - "sync/atomic" "git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/logs" "git.frostfs.info/TrueCloudLab/frostfs-http-gw/resolver" @@ -21,43 +20,26 @@ import ( "go.uber.org/zap" ) +type Config interface { + DefaultTimestamp() bool + ZipCompression() bool +} + type Handler struct { log *zap.Logger pool *pool.Pool ownerID *user.ID - settings *Settings + config Config containerResolver *resolver.ContainerResolver tree *tree.Tree } -// Settings stores reloading parameters, so it has to provide atomic getters and setters. -type Settings struct { - defaultTimestamp atomic.Bool - zipCompression atomic.Bool -} - -func (s *Settings) DefaultTimestamp() bool { - return s.defaultTimestamp.Load() -} - -func (s *Settings) SetDefaultTimestamp(val bool) { - s.defaultTimestamp.Store(val) -} - -func (s *Settings) ZipCompression() bool { - return s.zipCompression.Load() -} - -func (s *Settings) SetZipCompression(val bool) { - s.zipCompression.Store(val) -} - -func New(params *utils.AppParams, settings *Settings, tree *tree.Tree) *Handler { +func New(params *utils.AppParams, config Config, tree *tree.Tree) *Handler { return &Handler{ log: params.Logger, pool: params.Pool, ownerID: params.Owner, - settings: settings, + config: config, containerResolver: params.Resolver, tree: tree, } diff --git a/internal/handler/upload.go b/internal/handler/upload.go index 8d4e681..f5e0459 100644 --- a/internal/handler/upload.go +++ b/internal/handler/upload.go @@ -121,7 +121,7 @@ func (h *Handler) Upload(req *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 && h.settings.DefaultTimestamp() { + if _, ok := filtered[object.AttributeTimestamp]; !ok && h.config.DefaultTimestamp() { timestamp := object.NewAttribute() timestamp.SetKey(object.AttributeTimestamp) timestamp.SetValue(strconv.FormatInt(time.Now().Unix(), 10)) -- 2.45.2