[#422] pkg/services: Cache clients by address only
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
55dec28bbb
commit
cc7287d6f7
19 changed files with 55 additions and 68 deletions
22
pkg/network/cache/client.go
vendored
22
pkg/network/cache/client.go
vendored
|
@ -1,13 +1,9 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"sync"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
||||
crypto "github.com/nspcc-dev/neofs-crypto"
|
||||
)
|
||||
|
||||
type (
|
||||
|
@ -28,11 +24,9 @@ func NewSDKClientCache() *ClientCache {
|
|||
}
|
||||
|
||||
// Get function returns existing client or creates a new one.
|
||||
func (c *ClientCache) Get(key *ecdsa.PrivateKey, address string, opts ...client.Option) (*client.Client, error) {
|
||||
id := uniqueID(key, address)
|
||||
|
||||
func (c *ClientCache) Get(address string, opts ...client.Option) (*client.Client, error) {
|
||||
c.mu.RLock()
|
||||
if cli, ok := c.clients[id]; ok {
|
||||
if cli, ok := c.clients[address]; ok {
|
||||
// todo: check underlying connection neofs-api-go#196
|
||||
c.mu.RUnlock()
|
||||
|
||||
|
@ -46,22 +40,16 @@ func (c *ClientCache) Get(key *ecdsa.PrivateKey, address string, opts ...client.
|
|||
|
||||
// check once again if client is missing in cache, concurrent routine could
|
||||
// create client while this routine was locked on `c.mu.Lock()`.
|
||||
if cli, ok := c.clients[id]; ok {
|
||||
if cli, ok := c.clients[address]; ok {
|
||||
return cli, nil
|
||||
}
|
||||
|
||||
cli, err := client.New(key, append(opts, client.WithAddress(address))...)
|
||||
cli, err := client.New(nil, append(opts, client.WithAddress(address))...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.clients[id] = cli
|
||||
c.clients[address] = cli
|
||||
|
||||
return cli, nil
|
||||
}
|
||||
|
||||
func uniqueID(key *ecdsa.PrivateKey, address string) string {
|
||||
keyFingerprint := sha256.Sum256(crypto.MarshalPrivateKey(key))
|
||||
|
||||
return hex.EncodeToString(keyFingerprint[:]) + address
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue