[#428] client: Hide client cache behind interface in dependent packages
Replace usage of `cache.ClientCache` type with interface with similar signature. This will further allow overloading clients without affecting the logic of dependent packages. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
f25253738a
commit
106884fc40
13 changed files with 65 additions and 49 deletions
|
@ -195,7 +195,9 @@ type remoteLoadAnnounceProvider struct {
|
|||
|
||||
loadAddrSrc network.LocalAddressSource
|
||||
|
||||
clientCache *cache.ClientCache
|
||||
clientCache interface {
|
||||
Get(string) (apiClient.Client, error)
|
||||
}
|
||||
|
||||
deadEndProvider loadcontroller.WriterProvider
|
||||
}
|
||||
|
|
|
@ -230,7 +230,7 @@ func initObjectService(c *cfg) {
|
|||
|
||||
sPut := putsvc.NewService(
|
||||
putsvc.WithKeyStorage(keyStorage),
|
||||
putsvc.WithClientCache(clientCache),
|
||||
putsvc.WithClientConstructor(clientCache),
|
||||
putsvc.WithMaxSizeSource(c),
|
||||
putsvc.WithLocalStorage(ls),
|
||||
putsvc.WithContainerSource(c.cfgObject.cnrStorage),
|
||||
|
@ -251,7 +251,7 @@ func initObjectService(c *cfg) {
|
|||
sSearch := searchsvc.New(
|
||||
searchsvc.WithLogger(c.log),
|
||||
searchsvc.WithLocalStorageEngine(ls),
|
||||
searchsvc.WithClientCache(clientCache),
|
||||
searchsvc.WithClientConstructor(clientCache),
|
||||
searchsvc.WithTraverserGenerator(
|
||||
traverseGen.WithTraverseOptions(
|
||||
placement.WithoutSuccessTracking(),
|
||||
|
@ -268,7 +268,7 @@ func initObjectService(c *cfg) {
|
|||
sGet := getsvc.New(
|
||||
getsvc.WithLogger(c.log),
|
||||
getsvc.WithLocalStorageEngine(ls),
|
||||
getsvc.WithClientCache(clientCache),
|
||||
getsvc.WithClientConstructor(clientCache),
|
||||
getsvc.WithTraverserGenerator(
|
||||
traverseGen.WithTraverseOptions(
|
||||
placement.SuccessAfter(1),
|
||||
|
|
|
@ -21,8 +21,10 @@ import (
|
|||
type (
|
||||
ClientCache struct {
|
||||
log *zap.Logger
|
||||
cache *cache.ClientCache
|
||||
key *ecdsa.PrivateKey
|
||||
cache interface {
|
||||
Get(string) (client.Client, error)
|
||||
}
|
||||
key *ecdsa.PrivateKey
|
||||
|
||||
sgTimeout, headTimeout, rangeTimeout time.Duration
|
||||
}
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package getsvc
|
||||
|
||||
import (
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
||||
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
||||
|
@ -91,8 +91,12 @@ func WithLocalStorageEngine(e *engine.StorageEngine) Option {
|
|||
}
|
||||
}
|
||||
|
||||
// WithClientCache returns option to set cache of remote node clients.
|
||||
func WithClientCache(v *cache.ClientCache) Option {
|
||||
type ClientConstructor interface {
|
||||
Get(string) (client.Client, error)
|
||||
}
|
||||
|
||||
// WithClientConstructor returns option to set constructor of remote node clients.
|
||||
func WithClientConstructor(v ClientConstructor) Option {
|
||||
return func(c *cfg) {
|
||||
c.clientCache.(*clientCacheWrapper).cache = v
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@ import (
|
|||
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
|
||||
)
|
||||
|
||||
type SimpleObjectWriter struct {
|
||||
|
@ -18,7 +17,7 @@ type SimpleObjectWriter struct {
|
|||
}
|
||||
|
||||
type clientCacheWrapper struct {
|
||||
cache *cache.ClientCache
|
||||
cache ClientConstructor
|
||||
}
|
||||
|
||||
type clientWrapper struct {
|
||||
|
|
|
@ -7,17 +7,20 @@ import (
|
|||
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/network"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type ClientConstructor interface {
|
||||
Get(string) (client.Client, error)
|
||||
}
|
||||
|
||||
// RemoteHeader represents utility for getting
|
||||
// the object header from a remote host.
|
||||
type RemoteHeader struct {
|
||||
keyStorage *util.KeyStorage
|
||||
|
||||
clientCache *cache.ClientCache
|
||||
clientCache ClientConstructor
|
||||
}
|
||||
|
||||
// RemoteHeadPrm groups remote header operation parameters.
|
||||
|
@ -30,7 +33,7 @@ type RemoteHeadPrm struct {
|
|||
var ErrNotFound = errors.New("object header not found")
|
||||
|
||||
// NewRemoteHeader creates, initializes and returns new RemoteHeader instance.
|
||||
func NewRemoteHeader(keyStorage *util.KeyStorage, cache *cache.ClientCache) *RemoteHeader {
|
||||
func NewRemoteHeader(keyStorage *util.KeyStorage, cache ClientConstructor) *RemoteHeader {
|
||||
return &RemoteHeader{
|
||||
keyStorage: keyStorage,
|
||||
clientCache: cache,
|
||||
|
|
|
@ -6,7 +6,6 @@ import (
|
|||
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/network"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/transformer"
|
||||
"github.com/pkg/errors"
|
||||
|
@ -25,7 +24,7 @@ type remoteTarget struct {
|
|||
|
||||
obj *object.Object
|
||||
|
||||
clientCache *cache.ClientCache
|
||||
clientConstructor ClientConstructor
|
||||
}
|
||||
|
||||
// RemoteSender represents utility for
|
||||
|
@ -33,7 +32,7 @@ type remoteTarget struct {
|
|||
type RemoteSender struct {
|
||||
keyStorage *util.KeyStorage
|
||||
|
||||
clientCache *cache.ClientCache
|
||||
clientConstructor ClientConstructor
|
||||
}
|
||||
|
||||
// RemotePutPrm groups remote put operation parameters.
|
||||
|
@ -60,7 +59,7 @@ func (t *remoteTarget) Close() (*transformer.AccessIdentifiers, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
c, err := t.clientCache.Get(addr)
|
||||
c, err := t.clientConstructor.Get(addr)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "(%T) could not create SDK client %s", t, addr)
|
||||
}
|
||||
|
@ -84,10 +83,10 @@ func (t *remoteTarget) Close() (*transformer.AccessIdentifiers, error) {
|
|||
}
|
||||
|
||||
// NewRemoteSender creates, initializes and returns new RemoteSender instance.
|
||||
func NewRemoteSender(keyStorage *util.KeyStorage, cache *cache.ClientCache) *RemoteSender {
|
||||
func NewRemoteSender(keyStorage *util.KeyStorage, cons ClientConstructor) *RemoteSender {
|
||||
return &RemoteSender{
|
||||
keyStorage: keyStorage,
|
||||
clientCache: cache,
|
||||
keyStorage: keyStorage,
|
||||
clientConstructor: cons,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -112,10 +111,10 @@ func (p *RemotePutPrm) WithObject(v *object.Object) *RemotePutPrm {
|
|||
// PutObject sends object to remote node.
|
||||
func (s *RemoteSender) PutObject(ctx context.Context, p *RemotePutPrm) error {
|
||||
t := &remoteTarget{
|
||||
ctx: ctx,
|
||||
keyStorage: s.keyStorage,
|
||||
addr: p.node,
|
||||
clientCache: s.clientCache,
|
||||
ctx: ctx,
|
||||
keyStorage: s.keyStorage,
|
||||
addr: p.node,
|
||||
clientConstructor: s.clientConstructor,
|
||||
}
|
||||
|
||||
if err := t.WriteHeader(object.NewRawFromObject(p.obj)); err != nil {
|
||||
|
|
|
@ -3,12 +3,12 @@ package putsvc
|
|||
import (
|
||||
"context"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/core/container"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/network"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
|
||||
objutil "github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/util"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
||||
|
@ -29,6 +29,10 @@ type Service struct {
|
|||
|
||||
type Option func(*cfg)
|
||||
|
||||
type ClientConstructor interface {
|
||||
Get(string) (client.Client, error)
|
||||
}
|
||||
|
||||
type cfg struct {
|
||||
keyStorage *objutil.KeyStorage
|
||||
|
||||
|
@ -50,7 +54,7 @@ type cfg struct {
|
|||
|
||||
networkState netmap.State
|
||||
|
||||
clientCache *cache.ClientCache
|
||||
clientConstructor ClientConstructor
|
||||
|
||||
log *logger.Logger
|
||||
}
|
||||
|
@ -138,9 +142,9 @@ func WithNetworkState(v netmap.State) Option {
|
|||
}
|
||||
}
|
||||
|
||||
func WithClientCache(v *cache.ClientCache) Option {
|
||||
func WithClientConstructor(v ClientConstructor) Option {
|
||||
return func(c *cfg) {
|
||||
c.clientCache = v
|
||||
c.clientConstructor = v
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -139,11 +139,11 @@ func (p *Streamer) newCommonTarget(prm *PutInitPrm) transformer.ObjectTarget {
|
|||
}
|
||||
|
||||
return &remoteTarget{
|
||||
ctx: p.ctx,
|
||||
keyStorage: p.keyStorage,
|
||||
commonPrm: prm.common,
|
||||
addr: addr,
|
||||
clientCache: p.clientCache,
|
||||
ctx: p.ctx,
|
||||
keyStorage: p.keyStorage,
|
||||
commonPrm: prm.common,
|
||||
addr: addr,
|
||||
clientConstructor: p.clientConstructor,
|
||||
}
|
||||
},
|
||||
fmt: p.fmtValidator,
|
||||
|
|
|
@ -147,7 +147,7 @@ func (exec execCtx) remoteClient(node *network.Address) (searchClient, bool) {
|
|||
|
||||
log.Debug("could not calculate node IP address")
|
||||
case err == nil:
|
||||
c, err := exec.svc.clientCache.get(ipAddr)
|
||||
c, err := exec.svc.clientConstructor.get(ipAddr)
|
||||
|
||||
switch {
|
||||
default:
|
||||
|
|
|
@ -257,7 +257,7 @@ func TestGetRemoteSmall(t *testing.T) {
|
|||
curEpoch: b,
|
||||
},
|
||||
}
|
||||
svc.clientCache = c
|
||||
svc.clientConstructor = c
|
||||
svc.currentEpochReceiver = testEpochReceiver(curEpoch)
|
||||
|
||||
return svc
|
||||
|
@ -376,7 +376,7 @@ func TestGetFromPastEpoch(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
svc.clientCache = &testClientCache{
|
||||
svc.clientConstructor = &testClientCache{
|
||||
clients: map[string]*testStorage{
|
||||
as[0][0]: c11,
|
||||
as[0][1]: c12,
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package searchsvc
|
||||
|
||||
import (
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
||||
|
@ -25,6 +25,10 @@ type searchClient interface {
|
|||
searchObjects(*execCtx) ([]*object.ID, error)
|
||||
}
|
||||
|
||||
type ClientConstructor interface {
|
||||
Get(string) (client.Client, error)
|
||||
}
|
||||
|
||||
type cfg struct {
|
||||
log *logger.Logger
|
||||
|
||||
|
@ -32,7 +36,7 @@ type cfg struct {
|
|||
search(*execCtx) ([]*object.ID, error)
|
||||
}
|
||||
|
||||
clientCache interface {
|
||||
clientConstructor interface {
|
||||
get(string) (searchClient, error)
|
||||
}
|
||||
|
||||
|
@ -47,8 +51,8 @@ type cfg struct {
|
|||
|
||||
func defaultCfg() *cfg {
|
||||
return &cfg{
|
||||
log: zap.L(),
|
||||
clientCache: new(clientCacheWrapper),
|
||||
log: zap.L(),
|
||||
clientConstructor: new(clientConstructorWrapper),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -81,10 +85,10 @@ func WithLocalStorageEngine(e *engine.StorageEngine) Option {
|
|||
}
|
||||
}
|
||||
|
||||
// WithClientCache returns option to set cache of remote node clients.
|
||||
func WithClientCache(v *cache.ClientCache) Option {
|
||||
// WithClientConstructor returns option to set constructor of remote node clients.
|
||||
func WithClientConstructor(v ClientConstructor) Option {
|
||||
return func(c *cfg) {
|
||||
c.clientCache.(*clientCacheWrapper).cache = v
|
||||
c.clientConstructor.(*clientConstructorWrapper).constructor = v
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,6 @@ import (
|
|||
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
|
||||
)
|
||||
|
@ -21,8 +20,8 @@ type uniqueIDWriter struct {
|
|||
writer IDListWriter
|
||||
}
|
||||
|
||||
type clientCacheWrapper struct {
|
||||
cache *cache.ClientCache
|
||||
type clientConstructorWrapper struct {
|
||||
constructor ClientConstructor
|
||||
}
|
||||
|
||||
type clientWrapper struct {
|
||||
|
@ -68,8 +67,8 @@ func (w *uniqueIDWriter) WriteIDs(list []*objectSDK.ID) error {
|
|||
return w.writer.WriteIDs(list)
|
||||
}
|
||||
|
||||
func (c *clientCacheWrapper) get(addr string) (searchClient, error) {
|
||||
clt, err := c.cache.Get(addr)
|
||||
func (c *clientConstructorWrapper) get(addr string) (searchClient, error) {
|
||||
clt, err := c.constructor.Get(addr)
|
||||
|
||||
return &clientWrapper{
|
||||
client: clt,
|
||||
|
|
Loading…
Reference in a new issue