[#549] network/cache: Change Get signature

Make network cache's `Get` method accept
`network.Address` argument instead of
string.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-05-20 18:17:16 +03:00 committed by Alex Vanin
parent e10981a7d3
commit 33bef46f31
19 changed files with 83 additions and 85 deletions

View file

@ -1,9 +1,11 @@
package cache
import (
"fmt"
"sync"
"github.com/nspcc-dev/neofs-api-go/pkg/client"
"github.com/nspcc-dev/neofs-node/pkg/network"
)
type (
@ -27,9 +29,14 @@ func NewSDKClientCache(opts ...client.Option) *ClientCache {
}
// Get function returns existing client or creates a new one.
func (c *ClientCache) Get(address string) (client.Client, error) {
func (c *ClientCache) Get(netAddr *network.Address) (client.Client, error) {
hostAddr, err := netAddr.HostAddrString()
if err != nil {
return nil, fmt.Errorf("could not parse address as a string: %w", err)
}
c.mu.RLock()
if cli, ok := c.clients[address]; ok {
if cli, ok := c.clients[hostAddr]; ok {
// todo: check underlying connection neofs-api-go#196
c.mu.RUnlock()
@ -43,16 +50,18 @@ func (c *ClientCache) Get(address string) (client.Client, error) {
// 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[address]; ok {
if cli, ok := c.clients[hostAddr]; ok {
return cli, nil
}
cli, err := client.New(append(c.opts, client.WithAddress(address))...)
opts := append(c.opts, client.WithAddress(hostAddr))
cli, err := client.New(opts...)
if err != nil {
return nil, err
}
c.clients[address] = cli
c.clients[hostAddr] = cli
return cli, nil
}