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