[#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>
This commit is contained in:
Evgenii Stratonikov 2021-03-13 18:22:24 +03:00 committed by Leonard Lyubich
parent cc7287d6f7
commit 6679d59e89
13 changed files with 22 additions and 79 deletions

View file

@ -12,19 +12,22 @@ type (
ClientCache struct {
mu *sync.RWMutex
clients map[string]*client.Client
opts []client.Option
}
)
// NewSDKClientCache creates instance of client cache.
func NewSDKClientCache() *ClientCache {
// `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, opts ...client.Option) (*client.Client, error) {
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
@ -44,7 +47,7 @@ func (c *ClientCache) Get(address string, opts ...client.Option) (*client.Client
return cli, nil
}
cli, err := client.New(nil, append(opts, client.WithAddress(address))...)
cli, err := client.New(nil, append(c.opts, client.WithAddress(address))...)
if err != nil {
return nil, err
}