From 8b50b042c4c46b06a4d94c4bc072211a93f9746e Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 30 Dec 2022 14:19:42 +0300 Subject: [PATCH] [#6] services/object: Remove pointer unneeded indirections All `Service` are accessed by pointer. Signed-off-by: Evgenii Stratonikov --- pkg/services/object/acl/eacl/v2/headers.go | 17 +++++----- pkg/services/object/acl/v2/service.go | 37 ++++++++++------------ pkg/services/object/delete/service.go | 17 ++++------ pkg/services/object/delete/v2/service.go | 10 +++--- pkg/services/object/get/get_test.go | 6 ++-- pkg/services/object/get/service.go | 23 ++++++-------- pkg/services/object/get/v2/service.go | 10 +++--- pkg/services/object/put/service.go | 25 +++++++-------- pkg/services/object/put/v2/service.go | 10 +++--- pkg/services/object/search/search_test.go | 6 ++-- pkg/services/object/search/service.go | 19 +++++------ pkg/services/object/search/v2/service.go | 10 +++--- 12 files changed, 83 insertions(+), 107 deletions(-) diff --git a/pkg/services/object/acl/eacl/v2/headers.go b/pkg/services/object/acl/eacl/v2/headers.go index 52bf0369e..e6b352155 100644 --- a/pkg/services/object/acl/eacl/v2/headers.go +++ b/pkg/services/object/acl/eacl/v2/headers.go @@ -45,31 +45,30 @@ type headerSource struct { incompleteObjectHeaders bool } -func defaultCfg() *cfg { - return &cfg{ - storage: new(localStorage), - } +func (c *cfg) initDefault() { + c.storage = new(localStorage) } func NewMessageHeaderSource(opts ...Option) (eaclSDK.TypedHeaderSource, error) { - cfg := defaultCfg() + var c cfg + c.initDefault() for i := range opts { - opts[i](cfg) + opts[i](&c) } - if cfg.msg == nil { + if c.msg == nil { return nil, errors.New("message is not provided") } var res headerSource - err := cfg.readObjectHeaders(&res) + err := c.readObjectHeaders(&res) if err != nil { return nil, err } - res.requestHeaders = requestHeaders(cfg.msg) + res.requestHeaders = requestHeaders(c.msg) return res, nil } diff --git a/pkg/services/object/acl/v2/service.go b/pkg/services/object/acl/v2/service.go index 62f7917f2..b90a2437c 100644 --- a/pkg/services/object/acl/v2/service.go +++ b/pkg/services/object/acl/v2/service.go @@ -21,7 +21,7 @@ import ( // Service checks basic ACL rules. type Service struct { - *cfg + cfg c senderClassifier } @@ -72,18 +72,17 @@ type cfg struct { next object.ServiceServer } -func defaultCfg() *cfg { - return &cfg{ - log: &logger.Logger{Logger: zap.L()}, - } +func (c *cfg) initDefault() { + c.log = &logger.Logger{Logger: zap.L()} } // New is a constructor for object ACL checking service. -func New(opts ...Option) Service { - cfg := defaultCfg() +func New(opts ...Option) *Service { + var s Service + s.cfg.initDefault() for i := range opts { - opts[i](cfg) + opts[i](&s.cfg) } panicOnNil := func(v any, name string) { @@ -92,20 +91,18 @@ func New(opts ...Option) Service { } } - panicOnNil(cfg.next, "next Service") - panicOnNil(cfg.nm, "netmap client") - panicOnNil(cfg.irFetcher, "inner Ring fetcher") - panicOnNil(cfg.checker, "acl checker") - panicOnNil(cfg.containers, "container source") + panicOnNil(s.cfg.next, "next Service") + panicOnNil(s.cfg.nm, "netmap client") + panicOnNil(s.cfg.irFetcher, "inner Ring fetcher") + panicOnNil(s.cfg.checker, "acl checker") + panicOnNil(s.cfg.containers, "container source") - return Service{ - cfg: cfg, - c: senderClassifier{ - log: cfg.log, - innerRing: cfg.irFetcher, - netmap: cfg.nm, - }, + s.c = senderClassifier{ + log: s.cfg.log, + innerRing: s.cfg.irFetcher, + netmap: s.cfg.nm, } + return &s } // Get implements ServiceServer interface, makes ACL checks and calls diff --git a/pkg/services/object/delete/service.go b/pkg/services/object/delete/service.go index 76024a173..d977b9549 100644 --- a/pkg/services/object/delete/service.go +++ b/pkg/services/object/delete/service.go @@ -15,7 +15,7 @@ import ( // Service utility serving requests of Object.Get service. type Service struct { - *cfg + cfg } // Option is a Service's constructor option. @@ -60,24 +60,21 @@ type cfg struct { keyStorage *util.KeyStorage } -func defaultCfg() *cfg { - return &cfg{ - log: &logger.Logger{Logger: zap.L()}, - } +func (c *cfg) initDefault() { + c.log = &logger.Logger{Logger: zap.L()} } // New creates, initializes and returns utility serving // Object.Get service requests. func New(opts ...Option) *Service { - c := defaultCfg() + var s Service + s.cfg.initDefault() for i := range opts { - opts[i](c) + opts[i](&s.cfg) } - return &Service{ - cfg: c, - } + return &s } // WithLogger returns option to specify Delete service's logger. diff --git a/pkg/services/object/delete/v2/service.go b/pkg/services/object/delete/v2/service.go index 8f7b715c0..79c63d2e3 100644 --- a/pkg/services/object/delete/v2/service.go +++ b/pkg/services/object/delete/v2/service.go @@ -9,7 +9,7 @@ import ( // Service implements Delete operation of Object service v2. type Service struct { - *cfg + cfg } // Option represents Service constructor option. @@ -21,15 +21,13 @@ type cfg struct { // NewService constructs Service instance from provided options. func NewService(opts ...Option) *Service { - c := new(cfg) + var s Service for i := range opts { - opts[i](c) + opts[i](&s.cfg) } - return &Service{ - cfg: c, - } + return &s } // Delete calls internal service. diff --git a/pkg/services/object/get/get_test.go b/pkg/services/object/get/get_test.go index d7e24256d..3c130b1c8 100644 --- a/pkg/services/object/get/get_test.go +++ b/pkg/services/object/get/get_test.go @@ -211,7 +211,7 @@ func TestGetLocalOnly(t *testing.T) { ctx := context.Background() newSvc := func(storage *testStorage) *Service { - svc := &Service{cfg: new(cfg)} + svc := &Service{} svc.log = test.NewLogger(false) svc.localStorage = storage svc.assembly = true @@ -473,7 +473,7 @@ func TestGetRemoteSmall(t *testing.T) { container.CalculateID(&idCnr, cnr) newSvc := func(b *testPlacementBuilder, c *testClientCache) *Service { - svc := &Service{cfg: new(cfg)} + svc := &Service{} svc.log = test.NewLogger(false) svc.localStorage = newTestStorage() svc.assembly = true @@ -1150,7 +1150,7 @@ func TestGetFromPastEpoch(t *testing.T) { c22 := newTestClient() c22.addResult(addr, obj, nil) - svc := &Service{cfg: new(cfg)} + svc := &Service{} svc.log = test.NewLogger(false) svc.localStorage = newTestStorage() svc.assembly = true diff --git a/pkg/services/object/get/service.go b/pkg/services/object/get/service.go index 97e92a717..110613c88 100644 --- a/pkg/services/object/get/service.go +++ b/pkg/services/object/get/service.go @@ -15,7 +15,7 @@ import ( // Service utility serving requests of Object.Get service. type Service struct { - *cfg + cfg } // Option is a Service's constructor option. @@ -51,27 +51,24 @@ type epochSource interface { Epoch() (uint64, error) } -func defaultCfg() *cfg { - return &cfg{ - assembly: true, - log: &logger.Logger{Logger: zap.L()}, - localStorage: new(storageEngineWrapper), - clientCache: new(clientCacheWrapper), - } +func (c *cfg) initDefault() { + c.log = &logger.Logger{Logger: zap.L()} + c.localStorage = new(storageEngineWrapper) + c.assembly = true + c.clientCache = new(clientCacheWrapper) } // New creates, initializes and returns utility serving // Object.Get service requests. func New(opts ...Option) *Service { - c := defaultCfg() + var s Service + s.cfg.initDefault() for i := range opts { - opts[i](c) + opts[i](&s.cfg) } - return &Service{ - cfg: c, - } + return &s } // WithLogger returns option to specify Get service's logger. diff --git a/pkg/services/object/get/v2/service.go b/pkg/services/object/get/v2/service.go index 029671813..ce8be9676 100644 --- a/pkg/services/object/get/v2/service.go +++ b/pkg/services/object/get/v2/service.go @@ -13,7 +13,7 @@ import ( // Service implements Get operation of Object service v2. type Service struct { - *cfg + cfg } // Option represents Service constructor option. @@ -27,15 +27,13 @@ type cfg struct { // NewService constructs Service instance from provided options. func NewService(opts ...Option) *Service { - c := new(cfg) + var s Service for i := range opts { - opts[i](c) + opts[i](&s.cfg) } - return &Service{ - cfg: c, - } + return &s } // Get calls internal service and returns v2 object stream. diff --git a/pkg/services/object/put/service.go b/pkg/services/object/put/service.go index 7e72df18a..16040f800 100644 --- a/pkg/services/object/put/service.go +++ b/pkg/services/object/put/service.go @@ -22,7 +22,7 @@ type MaxSizeSource interface { } type Service struct { - *cfg + cfg } type Option func(*cfg) @@ -57,31 +57,28 @@ type cfg struct { log *logger.Logger } -func defaultCfg() *cfg { - return &cfg{ - remotePool: util.NewPseudoWorkerPool(), - localPool: util.NewPseudoWorkerPool(), - log: &logger.Logger{Logger: zap.L()}, - } +func (c *cfg) initDefault() { + c.remotePool = util.NewPseudoWorkerPool() + c.localPool = util.NewPseudoWorkerPool() + c.log = &logger.Logger{Logger: zap.L()} } func NewService(opts ...Option) *Service { - c := defaultCfg() + var s Service + s.cfg.initDefault() for i := range opts { - opts[i](c) + opts[i](&s.cfg) } - c.fmtValidator = object.NewFormatValidator(c.fmtValidatorOpts...) + s.cfg.fmtValidator = object.NewFormatValidator(s.cfg.fmtValidatorOpts...) - return &Service{ - cfg: c, - } + return &s } func (p *Service) Put(ctx context.Context) (*Streamer, error) { return &Streamer{ - cfg: p.cfg, + cfg: &p.cfg, ctx: ctx, }, nil } diff --git a/pkg/services/object/put/v2/service.go b/pkg/services/object/put/v2/service.go index 5bcb9a683..5b3145c29 100644 --- a/pkg/services/object/put/v2/service.go +++ b/pkg/services/object/put/v2/service.go @@ -11,7 +11,7 @@ import ( // Service implements Put operation of Object service v2. type Service struct { - *cfg + cfg } // Option represents Service constructor option. @@ -24,15 +24,13 @@ type cfg struct { // NewService constructs Service instance from provided options. func NewService(opts ...Option) *Service { - c := new(cfg) + var s Service for i := range opts { - opts[i](c) + opts[i](&s.cfg) } - return &Service{ - cfg: c, - } + return &s } // Put calls internal service and returns v2 object streamer. diff --git a/pkg/services/object/search/search_test.go b/pkg/services/object/search/search_test.go index 33ba07655..3e34cc8ce 100644 --- a/pkg/services/object/search/search_test.go +++ b/pkg/services/object/search/search_test.go @@ -146,7 +146,7 @@ func TestGetLocalOnly(t *testing.T) { ctx := context.Background() newSvc := func(storage *testStorage) *Service { - svc := &Service{cfg: new(cfg)} + svc := &Service{} svc.log = test.NewLogger(false) svc.localStorage = storage @@ -248,7 +248,7 @@ func TestGetRemoteSmall(t *testing.T) { container.CalculateID(&id, cnr) newSvc := func(b *testPlacementBuilder, c *testClientCache) *Service { - svc := &Service{cfg: new(cfg)} + svc := &Service{} svc.log = test.NewLogger(false) svc.localStorage = newTestStorage() @@ -357,7 +357,7 @@ func TestGetFromPastEpoch(t *testing.T) { ids22 := generateIDs(10) c22.addResult(idCnr, ids22, nil) - svc := &Service{cfg: new(cfg)} + svc := &Service{} svc.log = test.NewLogger(false) svc.localStorage = newTestStorage() diff --git a/pkg/services/object/search/service.go b/pkg/services/object/search/service.go index d0cd79e0a..3d577e0a1 100644 --- a/pkg/services/object/search/service.go +++ b/pkg/services/object/search/service.go @@ -15,7 +15,7 @@ import ( // Service is an utility serving requests // of Object.Search service. type Service struct { - *cfg + cfg } // Option is a Service's constructor option. @@ -55,25 +55,22 @@ type epochSource interface { Epoch() (uint64, error) } -func defaultCfg() *cfg { - return &cfg{ - log: &logger.Logger{Logger: zap.L()}, - clientConstructor: new(clientConstructorWrapper), - } +func (c *cfg) initDefault() { + c.log = &logger.Logger{Logger: zap.L()} + c.clientConstructor = new(clientConstructorWrapper) } // New creates, initializes and returns utility serving // Object.Get service requests. func New(opts ...Option) *Service { - c := defaultCfg() + var s Service + s.cfg.initDefault() for i := range opts { - opts[i](c) + opts[i](&s.cfg) } - return &Service{ - cfg: c, - } + return &s } // WithLogger returns option to specify Get service's logger. diff --git a/pkg/services/object/search/v2/service.go b/pkg/services/object/search/v2/service.go index f73233d39..a7efe9264 100644 --- a/pkg/services/object/search/v2/service.go +++ b/pkg/services/object/search/v2/service.go @@ -9,7 +9,7 @@ import ( // Service implements Search operation of Object service v2. type Service struct { - *cfg + cfg } // Option represents Service constructor option. @@ -23,15 +23,13 @@ type cfg struct { // NewService constructs Service instance from provided options. func NewService(opts ...Option) *Service { - c := new(cfg) + var s Service for i := range opts { - opts[i](c) + opts[i](&s.cfg) } - return &Service{ - cfg: c, - } + return &s } // Search calls internal service and returns v2 object stream.