Skip to content

Commit

Permalink
Make pool statistics optional (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
cthulhu-rider authored Jul 6, 2023
2 parents fbff444 + 41bd8e5 commit 6acff6a
Show file tree
Hide file tree
Showing 28 changed files with 1,891 additions and 529 deletions.
20 changes: 17 additions & 3 deletions client/accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ import (
rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc"
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-sdk-go/accounting"
"github.com/nspcc-dev/neofs-sdk-go/stat"
"github.com/nspcc-dev/neofs-sdk-go/user"
)

var (
// special variable for test purposes, to overwrite real RPC calls.
rpcAPIBalance = rpcapi.Balance
)

// PrmBalanceGet groups parameters of BalanceGet operation.
type PrmBalanceGet struct {
prmCommonMeta
Expand All @@ -37,13 +43,20 @@ func (x *PrmBalanceGet) SetAccount(id user.ID) {
// - [ErrMissingAccount]
// - [ErrMissingSigner]
func (c *Client) BalanceGet(ctx context.Context, prm PrmBalanceGet) (accounting.Decimal, error) {
var err error
defer func() {
c.sendStatistic(stat.MethodBalanceGet, err)()
}()

switch {
case !prm.accountSet:
return accounting.Decimal{}, ErrMissingAccount
err = ErrMissingAccount
return accounting.Decimal{}, err
}

if c.prm.signer == nil {
return accounting.Decimal{}, ErrMissingSigner
err = ErrMissingSigner
return accounting.Decimal{}, err
}

// form request body
Expand All @@ -69,7 +82,7 @@ func (c *Client) BalanceGet(ctx context.Context, prm PrmBalanceGet) (accounting.
cc.meta = prm.prmCommonMeta
cc.req = &req
cc.call = func() (responseV2, error) {
return rpcapi.Balance(&c.c, &req, client.WithContext(ctx))
return rpcAPIBalance(&c.c, &req, client.WithContext(ctx))
}
cc.result = func(r responseV2) {
resp := r.(*v2accounting.BalanceResponse)
Expand All @@ -90,6 +103,7 @@ func (c *Client) BalanceGet(ctx context.Context, prm PrmBalanceGet) (accounting.

// process call
if !cc.processCall() {
err = cc.err
return accounting.Decimal{}, cc.err
}

Expand Down
10 changes: 8 additions & 2 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import (
"github.com/nspcc-dev/neofs-api-go/v2/session"
)

var (
// special variables for test purposes only, to overwrite real RPC calls.
rpcAPINetMapSnapshot = rpcapi.NetMapSnapshot
rpcAPICreateSession = rpcapi.CreateSession
)

// interface of NeoFS API server. Exists for test purposes only.
type neoFSAPIServer interface {
createSession(cli *client.Client, req *session.CreateRequest, opts ...client.CallOption) (*session.CreateResponse, error)
Expand All @@ -29,7 +35,7 @@ func rpcErr(e error) error {
// executes NetmapService.NetmapSnapshot RPC declared in NeoFS API protocol
// using underlying client.Client.
func (x *coreServer) netMapSnapshot(ctx context.Context, req v2netmap.SnapshotRequest) (*v2netmap.SnapshotResponse, error) {
resp, err := rpcapi.NetMapSnapshot((*client.Client)(x), &req, client.WithContext(ctx))
resp, err := rpcAPINetMapSnapshot((*client.Client)(x), &req, client.WithContext(ctx))
if err != nil {
return nil, rpcErr(err)
}
Expand All @@ -38,7 +44,7 @@ func (x *coreServer) netMapSnapshot(ctx context.Context, req v2netmap.SnapshotRe
}

func (x *coreServer) createSession(cli *client.Client, req *session.CreateRequest, opts ...client.CallOption) (*session.CreateResponse, error) {
resp, err := rpcapi.CreateSession(cli, req, opts...)
resp, err := rpcAPICreateSession(cli, req, opts...)
if err != nil {
return nil, rpcErr(err)
}
Expand Down
36 changes: 27 additions & 9 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package client
import (
"context"
"crypto/tls"
"errors"
"fmt"
"time"

v2accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting"
"github.com/nspcc-dev/neofs-api-go/v2/rpc"
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
"github.com/nspcc-dev/neofs-sdk-go/stat"
)

// Client represents virtual connection to the NeoFS network to communicate
Expand Down Expand Up @@ -48,6 +46,9 @@ type Client struct {
c client.Client

server neoFSAPIServer

endpoint string
nodeKey []byte
}

var errNonNeoSigner = fmt.Errorf("%w: expected ECDSA_DETERMINISTIC_SHA256 scheme", neofscrypto.ErrIncorrectSigner)
Expand Down Expand Up @@ -90,6 +91,7 @@ func (c *Client) Dial(prm PrmDial) error {
if prm.endpoint == "" {
return ErrMissingServer
}
c.endpoint = prm.endpoint

if prm.timeoutDialSet {
if prm.timeoutDial <= 0 {
Expand Down Expand Up @@ -119,15 +121,13 @@ func (c *Client) Dial(prm PrmDial) error {
prm.parentCtx = context.Background()
}

// TODO: (neofs-api-go#382) perform generic dial stage of the client.Client
_, err := rpc.Balance(&c.c, new(v2accounting.BalanceRequest),
client.WithContext(prm.parentCtx),
)
// return context errors since they signal about dial problem
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
endpointInfo, err := c.EndpointInfo(prm.parentCtx, PrmEndpointInfo{})
if err != nil {
return err
}

c.nodeKey = endpointInfo.NodeInfo().PublicKey()

return nil
}

Expand Down Expand Up @@ -168,6 +168,17 @@ func (c *Client) Close() error {
return c.c.Conn().Close()
}

func (c *Client) sendStatistic(m stat.Method, err error) func() {
if c.prm.statisticCallback == nil {
return func() {}
}

ts := time.Now()
return func() {
c.prm.statisticCallback(c.nodeKey, c.endpoint, m, time.Since(ts), err)
}
}

// PrmInit groups initialization parameters of Client instances.
//
// See also [New].
Expand All @@ -177,6 +188,8 @@ type PrmInit struct {
cbRespInfo func(ResponseMetaInfo) error

netMagic uint64

statisticCallback stat.OperationCallback
}

// SetDefaultSigner sets Client private signer to be used for the protocol
Expand All @@ -196,6 +209,11 @@ func (x *PrmInit) SetResponseInfoCallback(f func(ResponseMetaInfo) error) {
x.cbRespInfo = f
}

// SetStatisticCallback makes the Client to pass [stat.OperationCallback] for the external statistic.
func (x *PrmInit) SetStatisticCallback(statisticCallback stat.OperationCallback) {
x.statisticCallback = statisticCallback
}

// PrmDial groups connection parameters for the Client.
//
// See also Dial.
Expand Down
7 changes: 6 additions & 1 deletion client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
"github.com/nspcc-dev/neofs-sdk-go/crypto/test"
"github.com/stretchr/testify/require"
)

Expand All @@ -31,7 +32,11 @@ func newClient(t *testing.T, signer neofscrypto.Signer, server neoFSAPIServer) *
}

func TestClient_DialContext(t *testing.T) {
var c Client
var prmInit PrmInit
prmInit.SetDefaultSigner(test.RandomSignerRFC6979(t))

c, err := New(prmInit)
require.NoError(t, err)

// try to connect to any host
var prm PrmDial
Expand Down
Loading

0 comments on commit 6acff6a

Please sign in to comment.