[#265] innerring: Add client cache

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-12-18 16:30:13 +03:00 committed by Alex Vanin
parent 3d3d058b05
commit 22cffbf529
3 changed files with 36 additions and 0 deletions

View file

@ -196,6 +196,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
AuditContract: server.contracts.audit, AuditContract: server.contracts.audit,
MorphClient: server.morphClient, MorphClient: server.morphClient,
IRList: server, IRList: server,
ClientCache: newClientCache(server.key),
}) })
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -2,6 +2,7 @@ package audit
import ( import (
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
SDKClient "github.com/nspcc-dev/neofs-api-go/pkg/client"
"github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/nspcc-dev/neofs-node/pkg/morph/event" "github.com/nspcc-dev/neofs-node/pkg/morph/event"
"github.com/panjf2000/ants/v2" "github.com/panjf2000/ants/v2"
@ -16,6 +17,11 @@ type (
InnerRingSize() int32 InnerRingSize() int32
} }
// NeoFSClientCache is an interface for cache of neofs RPC clients
NeoFSClientCache interface {
Get(address string, opts ...SDKClient.Option) (*SDKClient.Client, error)
}
// Processor of events related with data audit. // Processor of events related with data audit.
Processor struct { Processor struct {
log *zap.Logger log *zap.Logger
@ -24,6 +30,7 @@ type (
auditContract util.Uint160 auditContract util.Uint160
morphClient *client.Client morphClient *client.Client
irList Indexer irList Indexer
clientCache NeoFSClientCache
} }
// Params of the processor constructor. // Params of the processor constructor.
@ -33,6 +40,7 @@ type (
AuditContract util.Uint160 AuditContract util.Uint160
MorphClient *client.Client MorphClient *client.Client
IRList Indexer IRList Indexer
ClientCache NeoFSClientCache
} }
) )
@ -50,6 +58,8 @@ func New(p *Params) (*Processor, error) {
return nil, errors.New("ir/audit: neo:morph client is not set") return nil, errors.New("ir/audit: neo:morph client is not set")
case p.IRList == nil: case p.IRList == nil:
return nil, errors.New("ir/audit: global state is not set") return nil, errors.New("ir/audit: global state is not set")
case p.ClientCache == nil:
return nil, errors.New("ir/audit: neofs RPC client cache is not set")
} }
pool, err := ants.NewPool(ProcessorPoolSize, ants.WithNonblocking(true)) pool, err := ants.NewPool(ProcessorPoolSize, ants.WithNonblocking(true))
@ -64,6 +74,7 @@ func New(p *Params) (*Processor, error) {
auditContract: p.AuditContract, auditContract: p.AuditContract,
morphClient: p.MorphClient, morphClient: p.MorphClient,
irList: p.IRList, irList: p.IRList,
clientCache: p.ClientCache,
}, nil }, nil
} }

24
pkg/innerring/rpc.go Normal file
View file

@ -0,0 +1,24 @@
package innerring
import (
"crypto/ecdsa"
"github.com/nspcc-dev/neofs-api-go/pkg/client"
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
)
type ClientCache struct {
cache *cache.ClientCache
key *ecdsa.PrivateKey
}
func newClientCache(key *ecdsa.PrivateKey) *ClientCache {
return &ClientCache{
cache: cache.NewSDKClientCache(),
key: key,
}
}
func (c *ClientCache) Get(address string, opts ...client.Option) (*client.Client, error) {
return c.cache.Get(c.key, address, opts...)
}