2021-06-21 14:56:19 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2021-08-03 11:21:04 +00:00
|
|
|
"errors"
|
2021-06-21 14:56:19 +00:00
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
|
2022-01-13 15:01:50 +00:00
|
|
|
clientcore "github.com/nspcc-dev/neofs-node/pkg/core/client"
|
2021-06-21 14:56:19 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/client"
|
2021-06-21 14:56:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type multiClient struct {
|
|
|
|
mtx sync.RWMutex
|
|
|
|
|
2022-01-13 15:01:50 +00:00
|
|
|
clients map[string]clientcore.Client
|
2021-06-21 14:56:19 +00:00
|
|
|
|
|
|
|
addr network.AddressGroup
|
|
|
|
|
|
|
|
opts []client.Option
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMultiClient(addr network.AddressGroup, opts []client.Option) *multiClient {
|
|
|
|
return &multiClient{
|
2022-01-13 15:01:50 +00:00
|
|
|
clients: make(map[string]clientcore.Client),
|
2021-06-21 14:56:19 +00:00
|
|
|
addr: addr,
|
|
|
|
opts: opts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// note: must be wrapped into mutex lock.
|
2022-01-13 15:01:50 +00:00
|
|
|
func (x *multiClient) createForAddress(addr network.Address) clientcore.Client {
|
2021-06-21 14:56:19 +00:00
|
|
|
opts := append(x.opts, client.WithAddress(addr.HostAddr()))
|
|
|
|
|
|
|
|
if addr.TLSEnabled() {
|
|
|
|
opts = append(opts, client.WithTLSConfig(&tls.Config{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := client.New(opts...)
|
|
|
|
if err != nil {
|
|
|
|
// client never returns an error
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
x.clients[addr.String()] = c
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-01-13 15:01:50 +00:00
|
|
|
func (x *multiClient) iterateClients(ctx context.Context, f func(clientcore.Client) error) error {
|
2021-06-21 14:56:19 +00:00
|
|
|
var firstErr error
|
|
|
|
|
|
|
|
x.addr.IterateAddresses(func(addr network.Address) bool {
|
2021-08-03 11:21:04 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
firstErr = context.Canceled
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2021-06-21 14:56:19 +00:00
|
|
|
var err error
|
|
|
|
|
2021-07-13 11:58:26 +00:00
|
|
|
c := x.client(addr)
|
2021-06-21 14:56:19 +00:00
|
|
|
|
|
|
|
err = f(c)
|
|
|
|
|
2021-08-03 11:21:04 +00:00
|
|
|
success := err == nil || errors.Is(err, context.Canceled)
|
2021-06-21 14:56:19 +00:00
|
|
|
|
|
|
|
if success || firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
|
|
|
|
return success
|
|
|
|
})
|
|
|
|
|
|
|
|
return firstErr
|
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *multiClient) ObjectPutInit(ctx context.Context, p client.PrmObjectPutInit) (res *client.ObjectWriter, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-02-25 09:20:49 +00:00
|
|
|
res, err = c.ObjectPutInit(ctx, p)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
2021-06-21 14:56:19 +00:00
|
|
|
})
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
return
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *multiClient) ContainerAnnounceUsedSpace(ctx context.Context, prm client.PrmAnnounceSpace) (res *client.ResAnnounceSpace, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-02-25 09:20:49 +00:00
|
|
|
res, err = c.ContainerAnnounceUsedSpace(ctx, prm)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
2021-06-21 14:56:19 +00:00
|
|
|
})
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
return
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *multiClient) ObjectDelete(ctx context.Context, p client.PrmObjectDelete) (res *client.ResObjectDelete, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-02-25 09:20:49 +00:00
|
|
|
res, err = c.ObjectDelete(ctx, p)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
2021-06-21 14:56:19 +00:00
|
|
|
})
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
return
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *multiClient) ObjectGetInit(ctx context.Context, p client.PrmObjectGet) (res *client.ObjectReader, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-02-25 09:20:49 +00:00
|
|
|
res, err = c.ObjectGetInit(ctx, p)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
2021-06-21 14:56:19 +00:00
|
|
|
})
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
return
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *multiClient) ObjectRangeInit(ctx context.Context, p client.PrmObjectRange) (res *client.ObjectRangeReader, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-02-25 09:20:49 +00:00
|
|
|
res, err = c.ObjectRangeInit(ctx, p)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
2021-06-21 14:56:19 +00:00
|
|
|
})
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
return
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *multiClient) ObjectHead(ctx context.Context, p client.PrmObjectHead) (res *client.ResObjectHead, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-02-25 09:20:49 +00:00
|
|
|
res, err = c.ObjectHead(ctx, p)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
2021-06-21 14:56:19 +00:00
|
|
|
})
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
return
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *multiClient) ObjectHash(ctx context.Context, p client.PrmObjectHash) (res *client.ResObjectHash, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-02-25 09:20:49 +00:00
|
|
|
res, err = c.ObjectHash(ctx, p)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
2021-06-21 14:56:19 +00:00
|
|
|
})
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
return
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *multiClient) ObjectSearchInit(ctx context.Context, p client.PrmObjectSearch) (res *client.ObjectListReader, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-02-25 09:20:49 +00:00
|
|
|
res, err = c.ObjectSearchInit(ctx, p)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
2021-06-21 14:56:19 +00:00
|
|
|
})
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
return
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *multiClient) AnnounceLocalTrust(ctx context.Context, prm client.PrmAnnounceLocalTrust) (res *client.ResAnnounceLocalTrust, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-01-21 16:53:06 +00:00
|
|
|
res, err = c.AnnounceLocalTrust(ctx, prm)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
2021-06-21 14:56:19 +00:00
|
|
|
})
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
return
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *multiClient) AnnounceIntermediateTrust(ctx context.Context, prm client.PrmAnnounceIntermediateTrust) (res *client.ResAnnounceIntermediateTrust, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-01-21 16:53:06 +00:00
|
|
|
res, err = c.AnnounceIntermediateTrust(ctx, prm)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
2021-06-21 14:56:19 +00:00
|
|
|
})
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
return
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (x *multiClient) Raw() *rawclient.Client {
|
|
|
|
panic("multiClient.Raw() must not be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *multiClient) Conn() io.Closer {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *multiClient) Close() error {
|
|
|
|
x.mtx.RLock()
|
|
|
|
|
|
|
|
{
|
|
|
|
for _, c := range x.clients {
|
|
|
|
_ = c.Conn().Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
x.mtx.RUnlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *multiClient) RawForAddress(addr network.Address) *rawclient.Client {
|
2021-07-13 11:58:26 +00:00
|
|
|
return x.client(addr).Raw()
|
|
|
|
}
|
|
|
|
|
2022-01-13 15:01:50 +00:00
|
|
|
func (x *multiClient) client(addr network.Address) clientcore.Client {
|
2021-06-21 14:56:19 +00:00
|
|
|
x.mtx.Lock()
|
|
|
|
|
2021-07-13 11:02:24 +00:00
|
|
|
strAddr := addr.String()
|
|
|
|
|
|
|
|
c, cached := x.clients[strAddr]
|
|
|
|
if !cached {
|
|
|
|
c = x.createForAddress(addr)
|
|
|
|
}
|
2021-06-21 14:56:19 +00:00
|
|
|
|
|
|
|
x.mtx.Unlock()
|
|
|
|
|
2021-07-13 11:58:26 +00:00
|
|
|
return c
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|