[#1213] morph/client: Unlock mutex to perform network requests

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-03-15 14:32:25 +03:00 committed by Alex Vanin
parent 48eb87d32c
commit 61f60b8461

View file

@ -23,8 +23,6 @@ type multiClient struct {
} }
// createForAddress creates single Client instance using provided endpoint. // createForAddress creates single Client instance using provided endpoint.
//
// note: must be wrapped into mutex lock.
func (x *multiClient) createForAddress(addr string) (*Client, error) { func (x *multiClient) createForAddress(addr string) (*Client, error) {
cli, err := client.New(x.cfg.ctx, addr, client.Options{ cli, err := client.New(x.cfg.ctx, addr, client.Options{
DialTimeout: x.cfg.dialTimeout, DialTimeout: x.cfg.dialTimeout,
@ -39,15 +37,25 @@ func (x *multiClient) createForAddress(addr string) (*Client, error) {
return nil, err return nil, err
} }
sCli := blankSingleClient(cli, x.account, &x.cfg) var c *Client
sCli.notary = x.sharedNotary
c := &Client{ x.clientsMtx.Lock()
cache: x.sharedCache, // While creating 2 clients in parallel is ok, we don't want to
singleClient: sCli, // use a client missing from `x.clients` map as it can lead
// to unexpected bugs.
if x.clients[addr] == nil {
sCli := blankSingleClient(cli, x.account, &x.cfg)
sCli.notary = x.sharedNotary
c = &Client{
cache: x.sharedCache,
singleClient: sCli,
}
x.clients[addr] = c
} else {
c = x.clients[addr]
} }
x.clientsMtx.Unlock()
x.clients[addr] = c
return c, nil return c, nil
} }
@ -66,14 +74,12 @@ func (x *multiClient) iterateClients(f func(*Client) error) error {
} }
x.clientsMtx.Lock() x.clientsMtx.Lock()
c, cached := x.clients[x.endpoints[i]] c, cached := x.clients[x.endpoints[i]]
x.clientsMtx.Unlock()
if !cached { if !cached {
c, err = x.createForAddress(x.endpoints[i]) c, err = x.createForAddress(x.endpoints[i])
} }
x.clientsMtx.Unlock()
if !cached && err != nil { if !cached && err != nil {
x.cfg.logger.Error("could not open morph client connection", x.cfg.logger.Error("could not open morph client connection",
zap.String("endpoint", x.endpoints[i]), zap.String("endpoint", x.endpoints[i]),