[#74] Replace atomics with mutex for reloadable params
All checks were successful
/ DCO (pull_request) Successful in 1m7s
/ Vulncheck (pull_request) Successful in 1m28s
/ Builds (1.20) (pull_request) Successful in 1m37s
/ Builds (1.21) (pull_request) Successful in 1m12s
/ Lint (pull_request) Successful in 14m9s
/ Tests (1.20) (pull_request) Successful in 1m41s
/ Tests (1.21) (pull_request) Successful in 9m36s

Signed-off-by: Marina Biryukova <m.biryukova@yadro.com>
This commit is contained in:
Marina Biryukova 2023-09-05 18:17:22 +03:00
parent d219943542
commit e26577e753
4 changed files with 45 additions and 32 deletions

View file

@ -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() {

View file

@ -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
}

View file

@ -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,
}

View file

@ -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))