From e8fe07edd0560043eca05d4ce2973745e75aa403 Mon Sep 17 00:00:00 2001
From: Alex Vanin <alexey@nspcc.ru>
Date: Wed, 18 Nov 2020 16:04:59 +0300
Subject: [PATCH] [#184] Use SDK client cache in object.Rangehash

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
---
 cmd/neofs-node/object.go                     | 1 +
 pkg/services/object/rangehash/distributed.go | 5 +++--
 pkg/services/object/rangehash/remote.go      | 7 ++++---
 pkg/services/object/rangehash/service.go     | 9 +++++++++
 4 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/cmd/neofs-node/object.go b/cmd/neofs-node/object.go
index 5b5b043b0..2a0367ca4 100644
--- a/cmd/neofs-node/object.go
+++ b/cmd/neofs-node/object.go
@@ -317,6 +317,7 @@ func initObjectService(c *cfg) {
 
 	sRangeHash := rangehashsvc.NewService(
 		rangehashsvc.WithKeyStorage(keyStorage),
+		rangehashsvc.WithClientCache(clientCache),
 		rangehashsvc.WithLocalStorage(ls),
 		rangehashsvc.WithContainerSource(c.cfgObject.cnrStorage),
 		rangehashsvc.WithNetworkMapSource(c.cfgObject.netMapStorage),
diff --git a/pkg/services/object/rangehash/distributed.go b/pkg/services/object/rangehash/distributed.go
index 79872cd6b..da9a45c91 100644
--- a/pkg/services/object/rangehash/distributed.go
+++ b/pkg/services/object/rangehash/distributed.go
@@ -112,8 +112,9 @@ loop:
 					}
 				} else {
 					hasher = &remoteHasher{
-						keyStorage: h.keyStorage,
-						node:       addr,
+						keyStorage:  h.keyStorage,
+						node:        addr,
+						clientCache: h.clientCache,
 					}
 				}
 
diff --git a/pkg/services/object/rangehash/remote.go b/pkg/services/object/rangehash/remote.go
index 4635c8728..5486a68c4 100644
--- a/pkg/services/object/rangehash/remote.go
+++ b/pkg/services/object/rangehash/remote.go
@@ -7,6 +7,7 @@ import (
 	"github.com/nspcc-dev/neofs-api-go/pkg"
 	"github.com/nspcc-dev/neofs-api-go/pkg/client"
 	"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"
 )
@@ -15,6 +16,8 @@ type remoteHasher struct {
 	keyStorage *util.KeyStorage
 
 	node *network.Address
+
+	clientCache *cache.ClientCache
 }
 
 func (h *remoteHasher) hashRange(ctx context.Context, prm *Prm, handler func([][]byte)) error {
@@ -28,9 +31,7 @@ func (h *remoteHasher) hashRange(ctx context.Context, prm *Prm, handler func([][
 		return err
 	}
 
-	c, err := client.New(key,
-		client.WithAddress(addr),
-	)
+	c, err := h.clientCache.Get(key, addr)
 	if err != nil {
 		return errors.Wrapf(err, "(%T) could not create SDK client %s", h, addr)
 	}
diff --git a/pkg/services/object/rangehash/service.go b/pkg/services/object/rangehash/service.go
index df2052f01..a40dbb8eb 100644
--- a/pkg/services/object/rangehash/service.go
+++ b/pkg/services/object/rangehash/service.go
@@ -12,6 +12,7 @@ import (
 	"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
 	"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/localstore"
 	"github.com/nspcc-dev/neofs-node/pkg/network"
+	"github.com/nspcc-dev/neofs-node/pkg/network/cache"
 	headsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/head"
 	rangesvc "github.com/nspcc-dev/neofs-node/pkg/services/object/range"
 	objutil "github.com/nspcc-dev/neofs-node/pkg/services/object/util"
@@ -41,6 +42,8 @@ type cfg struct {
 	headSvc *headsvc.Service
 
 	rangeSvc *rangesvc.Service
+
+	clientCache *cache.ClientCache
 }
 
 func defaultCfg() *cfg {
@@ -264,3 +267,9 @@ func WithRangeService(v *rangesvc.Service) Option {
 		c.rangeSvc = v
 	}
 }
+
+func WithClientCache(v *cache.ClientCache) Option {
+	return func(c *cfg) {
+		c.clientCache = v
+	}
+}