frostfs-node/pkg/network/cache/client.go
Evgenii Stratonikov 6679d59e89 [#422] pkg/services: Provide client options on cache creation
Because options are not used when client is already in cache
providing them to shared cache is misleading at best.
In the worst case `dial_timeout` is set randomly (because of race
condition) which can lead to one service having `dial_timeout` of
another. Thus we set default client creation options when cache is
created.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-03-15 14:07:53 +03:00

58 lines
1.3 KiB
Go

package cache
import (
"sync"
"github.com/nspcc-dev/neofs-api-go/pkg/client"
)
type (
// ClientCache is a structure around neofs-api-go/pkg/client to reuse
// already created clients.
ClientCache struct {
mu *sync.RWMutex
clients map[string]*client.Client
opts []client.Option
}
)
// NewSDKClientCache creates instance of client cache.
// `opts` are used for new client creation.
func NewSDKClientCache(opts ...client.Option) *ClientCache {
return &ClientCache{
mu: new(sync.RWMutex),
clients: make(map[string]*client.Client),
opts: opts,
}
}
// Get function returns existing client or creates a new one.
func (c *ClientCache) Get(address string) (*client.Client, error) {
c.mu.RLock()
if cli, ok := c.clients[address]; ok {
// 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()`.
if cli, ok := c.clients[address]; ok {
return cli, nil
}
cli, err := client.New(nil, append(c.opts, client.WithAddress(address))...)
if err != nil {
return nil, err
}
c.clients[address] = cli
return cli, nil
}