2020-11-18 13:01:59 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2022-03-11 15:24:11 +00:00
|
|
|
"crypto/ecdsa"
|
2020-11-18 13:01:59 +00:00
|
|
|
"sync"
|
2022-03-11 15:24:11 +00:00
|
|
|
"time"
|
2020-11-18 13:01:59 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
clientcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
2020-11-18 13:01:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2022-12-23 17:35:35 +00:00
|
|
|
// ClientCache is a structure around frostfs-sdk-go/client to reuse
|
2020-11-18 13:01:59 +00:00
|
|
|
// already created clients.
|
|
|
|
ClientCache struct {
|
2022-11-12 08:05:37 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
clients map[string]*multiClient
|
|
|
|
opts ClientCacheOpts
|
2022-03-11 15:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ClientCacheOpts struct {
|
|
|
|
DialTimeout time.Duration
|
2022-09-06 15:23:59 +00:00
|
|
|
StreamTimeout time.Duration
|
2022-12-19 15:03:48 +00:00
|
|
|
ReconnectTimeout time.Duration
|
2022-03-11 15:24:11 +00:00
|
|
|
Key *ecdsa.PrivateKey
|
|
|
|
ResponseCallback func(client.ResponseMetaInfo) error
|
2022-09-26 12:34:01 +00:00
|
|
|
AllowExternal bool
|
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.
|
2022-03-11 15:24:11 +00:00
|
|
|
func NewSDKClientCache(opts ClientCacheOpts) *ClientCache {
|
2020-11-18 13:01:59 +00:00
|
|
|
return &ClientCache{
|
2022-11-12 08:05:37 +00:00
|
|
|
clients: make(map[string]*multiClient),
|
|
|
|
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.
|
2023-04-13 17:06:34 +00:00
|
|
|
func (c *ClientCache) Get(info clientcore.NodeInfo) (clientcore.MultiAddressClient, error) {
|
2021-09-28 04:46:10 +00:00
|
|
|
netAddr := info.AddressGroup()
|
2022-11-12 08:05:37 +00:00
|
|
|
if c.opts.AllowExternal {
|
2022-09-26 12:34:01 +00:00
|
|
|
netAddr = append(netAddr, info.ExternalAddressGroup()...)
|
|
|
|
}
|
2022-03-21 12:02:27 +00:00
|
|
|
cacheKey := string(info.PublicKey())
|
2021-05-20 15:17:16 +00:00
|
|
|
|
2020-11-18 13:01:59 +00:00
|
|
|
c.mu.RLock()
|
2021-09-28 05:57:38 +00:00
|
|
|
if cli, ok := c.clients[cacheKey]; ok {
|
2020-11-18 13:01:59 +00:00
|
|
|
c.mu.RUnlock()
|
2022-03-21 12:02:27 +00:00
|
|
|
cli.updateGroup(netAddr)
|
2020-11-18 13:01:59 +00:00
|
|
|
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-09-28 05:57:38 +00:00
|
|
|
if cli, ok := c.clients[cacheKey]; ok {
|
2022-03-21 12:02:27 +00:00
|
|
|
// No need to update address group as the client has just been created.
|
2020-11-18 13:01:59 +00:00
|
|
|
return cli, nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 15:24:11 +00:00
|
|
|
newClientOpts := c.opts
|
|
|
|
newClientOpts.ResponseCallback = clientcore.AssertKeyResponseCallback(info.PublicKey())
|
|
|
|
cli := newMultiClient(netAddr, newClientOpts)
|
2020-11-18 13:01:59 +00:00
|
|
|
|
2021-09-28 05:57:38 +00:00
|
|
|
c.clients[cacheKey] = 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 {
|
2022-03-11 15:24:11 +00:00
|
|
|
_ = cl.Close()
|
2021-05-31 06:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mu.RUnlock()
|
|
|
|
}
|