[#1231] Update new SDK Client interface

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2022-03-11 18:24:11 +03:00 committed by Alex Vanin
parent 697c12a5e9
commit b6720d5f97
14 changed files with 158 additions and 82 deletions

View file

@ -1,8 +1,10 @@
package cache
import (
"crypto/ecdsa"
"encoding/hex"
"sync"
"time"
clientcore "github.com/nspcc-dev/neofs-node/pkg/core/client"
"github.com/nspcc-dev/neofs-node/pkg/network"
@ -15,13 +17,19 @@ type (
ClientCache struct {
mu *sync.RWMutex
clients map[string]clientcore.Client
opts []client.Option
opts ClientCacheOpts
}
ClientCacheOpts struct {
DialTimeout time.Duration
Key *ecdsa.PrivateKey
ResponseCallback func(client.ResponseMetaInfo) error
}
)
// NewSDKClientCache creates instance of client cache.
// `opts` are used for new client creation.
func NewSDKClientCache(opts ...client.Option) *ClientCache {
func NewSDKClientCache(opts ClientCacheOpts) *ClientCache {
return &ClientCache{
mu: new(sync.RWMutex),
clients: make(map[string]clientcore.Client),
@ -62,9 +70,9 @@ func (c *ClientCache) Get(info clientcore.NodeInfo) (clientcore.Client, error) {
return cli, nil
}
cli := newMultiClient(netAddr, append(c.opts,
client.WithResponseInfoHandler(clientcore.AssertKeyResponseCallback(info.PublicKey())),
))
newClientOpts := c.opts
newClientOpts.ResponseCallback = clientcore.AssertKeyResponseCallback(info.PublicKey())
cli := newMultiClient(netAddr, newClientOpts)
c.clients[cacheKey] = cli
@ -79,10 +87,7 @@ func (c *ClientCache) CloseAll() {
{
for _, cl := range c.clients {
con := cl.Conn()
if con != nil {
_ = con.Close()
}
_ = cl.Close()
}
}

View file

@ -4,7 +4,6 @@ import (
"context"
"crypto/tls"
"errors"
"io"
"sync"
rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
@ -20,10 +19,10 @@ type multiClient struct {
addr network.AddressGroup
opts []client.Option
opts ClientCacheOpts
}
func newMultiClient(addr network.AddressGroup, opts []client.Option) *multiClient {
func newMultiClient(addr network.AddressGroup, opts ClientCacheOpts) *multiClient {
return &multiClient{
clients: make(map[string]clientcore.Client),
addr: addr,
@ -33,21 +32,39 @@ func newMultiClient(addr network.AddressGroup, opts []client.Option) *multiClien
// note: must be wrapped into mutex lock.
func (x *multiClient) createForAddress(addr network.Address) clientcore.Client {
opts := append(x.opts, client.WithAddress(addr.HostAddr()))
var (
c client.Client
prmInit client.PrmInit
prmDial client.PrmDial
)
prmDial.SetServerURI(addr.HostAddr())
if addr.TLSEnabled() {
opts = append(opts, client.WithTLSConfig(&tls.Config{}))
prmDial.SetTLSConfig(&tls.Config{})
}
c, err := client.New(opts...)
if x.opts.Key != nil {
prmInit.SetDefaultPrivateKey(*x.opts.Key)
}
if x.opts.DialTimeout > 0 {
prmDial.SetTimeout(x.opts.DialTimeout)
}
if x.opts.ResponseCallback != nil {
prmInit.SetResponseInfoCallback(x.opts.ResponseCallback)
}
c.Init(prmInit)
err := c.Dial(prmDial)
if err != nil {
// client never returns an error
panic(err)
}
x.clients[addr.String()] = c
x.clients[addr.String()] = &c
return c
return &c
}
func (x *multiClient) iterateClients(ctx context.Context, f func(clientcore.Client) error) error {
@ -169,12 +186,8 @@ func (x *multiClient) AnnounceIntermediateTrust(ctx context.Context, prm client.
return
}
func (x *multiClient) Raw() *rawclient.Client {
panic("multiClient.Raw() must not be called")
}
func (x *multiClient) Conn() io.Closer {
return x
func (x *multiClient) ExecRaw(f func(client *rawclient.Client) error) error {
panic("multiClient.ExecRaw() must not be called")
}
func (x *multiClient) Close() error {
@ -182,7 +195,7 @@ func (x *multiClient) Close() error {
{
for _, c := range x.clients {
_ = c.Conn().Close()
_ = c.Close()
}
}
@ -191,8 +204,8 @@ func (x *multiClient) Close() error {
return nil
}
func (x *multiClient) RawForAddress(addr network.Address) *rawclient.Client {
return x.client(addr).Raw()
func (x *multiClient) RawForAddress(addr network.Address, f func(client *rawclient.Client) error) error {
return x.client(addr).ExecRaw(f)
}
func (x *multiClient) client(addr network.Address) clientcore.Client {