2020-11-18 13:01:59 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2021-05-18 06:26:45 +00:00
|
|
|
"crypto/tls"
|
2021-05-20 15:17:16 +00:00
|
|
|
"fmt"
|
2020-11-18 13:01:59 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
2021-05-20 15:17:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
2020-11-18 13:01:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// ClientCache is a structure around neofs-api-go/pkg/client to reuse
|
|
|
|
// already created clients.
|
|
|
|
ClientCache struct {
|
|
|
|
mu *sync.RWMutex
|
2021-03-17 11:34:59 +00:00
|
|
|
clients map[string]client.Client
|
2021-03-13 15:22:24 +00:00
|
|
|
opts []client.Option
|
2020-11-18 13:01:59 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewSDKClientCache creates instance of client cache.
|
2021-03-13 15:22:24 +00:00
|
|
|
// `opts` are used for new client creation.
|
|
|
|
func NewSDKClientCache(opts ...client.Option) *ClientCache {
|
2020-11-18 13:01:59 +00:00
|
|
|
return &ClientCache{
|
|
|
|
mu: new(sync.RWMutex),
|
2021-03-17 11:34:59 +00:00
|
|
|
clients: make(map[string]client.Client),
|
2021-03-13 15:22:24 +00:00
|
|
|
opts: opts,
|
2020-11-18 13:01:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 12:58:39 +00:00
|
|
|
// Get function returns existing client or creates a new one.
|
2021-05-20 15:17:16 +00:00
|
|
|
func (c *ClientCache) Get(netAddr *network.Address) (client.Client, error) {
|
2021-05-18 06:26:45 +00:00
|
|
|
// multiaddr is used as a key in client cache since
|
|
|
|
// same host may have different connections(with tls or not),
|
|
|
|
// therefore, host+port pair is not unique
|
|
|
|
mAddr := netAddr.String()
|
2021-05-20 15:17:16 +00:00
|
|
|
|
2020-11-18 13:01:59 +00:00
|
|
|
c.mu.RLock()
|
2021-05-18 06:26:45 +00:00
|
|
|
if cli, ok := c.clients[mAddr]; ok {
|
2020-11-18 13:01:59 +00:00
|
|
|
// todo: check underlying connection neofs-api-go#196
|
|
|
|
c.mu.RUnlock()
|
|
|
|
|
|
|
|
return cli, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mu.RUnlock()
|
|
|
|
// if client is not found in cache, then create a new one
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
// check once again if client is missing in cache, concurrent routine could
|
|
|
|
// create client while this routine was locked on `c.mu.Lock()`.
|
2021-05-18 06:26:45 +00:00
|
|
|
if cli, ok := c.clients[mAddr]; ok {
|
2020-11-18 13:01:59 +00:00
|
|
|
return cli, nil
|
|
|
|
}
|
|
|
|
|
2021-05-18 06:26:45 +00:00
|
|
|
hostAddr, err := netAddr.HostAddrString()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse address as a string: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-05-20 15:17:16 +00:00
|
|
|
opts := append(c.opts, client.WithAddress(hostAddr))
|
|
|
|
|
2021-05-18 06:26:45 +00:00
|
|
|
if netAddr.TLSEnabled() {
|
|
|
|
opts = append(opts, client.WithTLSConfig(&tls.Config{}))
|
|
|
|
}
|
|
|
|
|
2021-05-20 15:17:16 +00:00
|
|
|
cli, err := client.New(opts...)
|
2020-11-18 13:01:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-18 06:26:45 +00:00
|
|
|
c.clients[mAddr] = cli
|
2020-11-18 13:01:59 +00:00
|
|
|
|
|
|
|
return cli, nil
|
|
|
|
}
|
2021-05-31 06:43:35 +00:00
|
|
|
|
|
|
|
// CloseAll closes underlying connections of all cached clients.
|
|
|
|
//
|
|
|
|
// Ignores closing errors.
|
|
|
|
func (c *ClientCache) CloseAll() {
|
|
|
|
c.mu.RLock()
|
|
|
|
|
|
|
|
{
|
|
|
|
for _, cl := range c.clients {
|
|
|
|
con := cl.Conn()
|
|
|
|
if con != nil {
|
|
|
|
_ = con.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mu.RUnlock()
|
|
|
|
}
|