From 10902c0b828926faac3d2361a1671313643c0d37 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 1 Feb 2021 15:19:15 +0300 Subject: [PATCH] [#328] container/load: Implement RemoteWriterProvider for controller Define component on app-side that implements RemoteWriterProvider with next logic: * if ServerInfo is nil, call internal dead end provider; * if address in ServerInfo equals to local node's address, do nothing; * otherwise create API client instance via client cache and return the wrapper over it. Signed-off-by: Leonard Lyubich --- cmd/neofs-node/container.go | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/cmd/neofs-node/container.go b/cmd/neofs-node/container.go index 6dd9b79da..49c979bd8 100644 --- a/cmd/neofs-node/container.go +++ b/cmd/neofs-node/container.go @@ -2,6 +2,7 @@ package main import ( "context" + "crypto/ecdsa" "strconv" apiClient "github.com/nspcc-dev/neofs-api-go/pkg/client" @@ -14,9 +15,12 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/client/container" "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper" + "github.com/nspcc-dev/neofs-node/pkg/network" + "github.com/nspcc-dev/neofs-node/pkg/network/cache" containerTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/container/grpc" containerService "github.com/nspcc-dev/neofs-node/pkg/services/container" loadcontroller "github.com/nspcc-dev/neofs-node/pkg/services/container/announcement/load/controller" + loadroute "github.com/nspcc-dev/neofs-node/pkg/services/container/announcement/load/route" containerMorph "github.com/nspcc-dev/neofs-node/pkg/services/container/morph" "github.com/nspcc-dev/neofs-node/pkg/util/logger" "github.com/pkg/errors" @@ -77,6 +81,53 @@ func (*morphLoadWriter) Close() error { return nil } +type nopLoadWriter struct{} + +func (nopLoadWriter) Put(containerSDK.UsedSpaceAnnouncement) error { + return nil +} + +func (nopLoadWriter) Close() error { + return nil +} + +type remoteLoadAnnounceProvider struct { + key *ecdsa.PrivateKey + + loadAddrSrc network.LocalAddressSource + + clientCache *cache.ClientCache + + deadEndProvider loadcontroller.WriterProvider +} + +func (r *remoteLoadAnnounceProvider) InitRemote(srv loadroute.ServerInfo) (loadcontroller.WriterProvider, error) { + if srv == nil { + return r.deadEndProvider, nil + } + + addr := srv.Address() + + if r.loadAddrSrc.LocalAddress().String() == srv.Address() { + // if local => return no-op writer + return loadcontroller.SimpleWriterProvider(new(nopLoadWriter)), nil + } + + ipAddr, err := network.IPAddrFromMultiaddr(addr) + if err != nil { + return nil, errors.Wrap(err, "could not convert address to IP format") + } + + c, err := r.clientCache.Get(r.key, ipAddr) + if err != nil { + return nil, errors.Wrap(err, "could not initialize API client") + } + + return &remoteLoadAnnounceWriterProvider{ + client: c, + }, nil +} + type remoteLoadAnnounceWriterProvider struct { client *apiClient.Client }