2021-08-26 07:59:02 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type multiClient struct {
|
|
|
|
cfg cfg
|
|
|
|
|
|
|
|
account *wallet.Account
|
|
|
|
|
|
|
|
sharedNotary *notary // notary config needed for single client construction
|
|
|
|
|
|
|
|
endpoints []string
|
2022-04-01 13:09:37 +00:00
|
|
|
clientsMtx sync.RWMutex
|
|
|
|
// lastSuccess is an index in endpoints array relating to a last
|
|
|
|
// used endpoint.
|
|
|
|
lastSuccess int
|
|
|
|
clients map[string]*Client
|
2021-08-26 07:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// createForAddress creates single Client instance using provided endpoint.
|
|
|
|
func (x *multiClient) createForAddress(addr string) (*Client, error) {
|
|
|
|
cli, err := client.New(x.cfg.ctx, addr, client.Options{
|
2021-12-15 18:04:36 +00:00
|
|
|
DialTimeout: x.cfg.dialTimeout,
|
|
|
|
MaxConnsPerHost: x.cfg.maxConnPerHost,
|
2021-08-26 07:59:02 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cli.Init() // magic number is set there based on RPC node answer
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-15 11:32:25 +00:00
|
|
|
var c *Client
|
2021-09-21 14:30:45 +00:00
|
|
|
|
2022-03-15 11:32:25 +00:00
|
|
|
x.clientsMtx.Lock()
|
|
|
|
// While creating 2 clients in parallel is ok, we don't want to
|
|
|
|
// 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
|
2021-08-26 07:59:02 +00:00
|
|
|
|
2022-03-15 11:32:25 +00:00
|
|
|
c = &Client{
|
2022-03-15 11:33:09 +00:00
|
|
|
cache: newClientCache(),
|
2022-03-15 11:32:25 +00:00
|
|
|
singleClient: sCli,
|
|
|
|
}
|
|
|
|
x.clients[addr] = c
|
|
|
|
} else {
|
|
|
|
c = x.clients[addr]
|
|
|
|
}
|
|
|
|
x.clientsMtx.Unlock()
|
2021-08-26 07:59:02 +00:00
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2022-04-01 13:09:37 +00:00
|
|
|
// iterateClients executes f on each client until nil error is returned.
|
|
|
|
// When nil error is returned, lastSuccess field is updated.
|
|
|
|
// The iteration order is non-deterministic and shouldn't be relied upon.
|
2021-08-26 07:59:02 +00:00
|
|
|
func (x *multiClient) iterateClients(f func(*Client) error) error {
|
|
|
|
var (
|
|
|
|
firstErr error
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2022-04-01 13:09:37 +00:00
|
|
|
x.clientsMtx.RLock()
|
|
|
|
start := x.lastSuccess
|
|
|
|
x.clientsMtx.RUnlock()
|
|
|
|
|
|
|
|
for i := 0; i < len(x.endpoints); i++ {
|
|
|
|
index := (start + i) % len(x.endpoints)
|
2021-08-26 07:59:02 +00:00
|
|
|
|
2022-04-01 13:09:37 +00:00
|
|
|
x.clientsMtx.RLock()
|
|
|
|
c, cached := x.clients[x.endpoints[index]]
|
|
|
|
x.clientsMtx.RUnlock()
|
2021-08-26 07:59:02 +00:00
|
|
|
if !cached {
|
2022-04-01 13:09:37 +00:00
|
|
|
c, err = x.createForAddress(x.endpoints[index])
|
2021-08-26 07:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !cached && err != nil {
|
|
|
|
x.cfg.logger.Error("could not open morph client connection",
|
2022-04-01 13:09:37 +00:00
|
|
|
zap.String("endpoint", x.endpoints[index]),
|
2021-08-26 07:59:02 +00:00
|
|
|
zap.String("err", err.Error()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
err = f(c)
|
|
|
|
}
|
|
|
|
|
2021-08-31 10:27:12 +00:00
|
|
|
if err == nil {
|
2022-04-01 13:09:37 +00:00
|
|
|
if i != 0 {
|
|
|
|
x.clientsMtx.Lock()
|
|
|
|
x.lastSuccess = index
|
|
|
|
x.clientsMtx.Unlock()
|
|
|
|
}
|
2021-08-31 10:27:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-08-26 07:59:02 +00:00
|
|
|
|
2021-08-31 10:29:43 +00:00
|
|
|
// we dont need to continue the process after the logical error was encountered
|
|
|
|
if errNeoFS := unwrapNeoFSError(err); errNeoFS != nil {
|
|
|
|
return errNeoFS
|
|
|
|
}
|
|
|
|
|
2021-08-31 10:27:12 +00:00
|
|
|
// set first error once
|
|
|
|
if firstErr == nil {
|
2021-08-26 07:59:02 +00:00
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return firstErr
|
|
|
|
}
|