Skip to content

Commit

Permalink
client: External statistic in accounting
Browse files Browse the repository at this point in the history
Signed-off-by: Evgenii Baidakov <[email protected]>
  • Loading branch information
smallhive committed Jun 22, 2023
1 parent d2dc7fb commit ba5c952
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
15 changes: 12 additions & 3 deletions client/accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package client

import (
"context"
"time"

v2accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
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"
)

Expand Down Expand Up @@ -36,13 +38,20 @@ func (x *PrmBalanceGet) SetAccount(id user.ID) {
// Return errors:
// - [ErrMissingAccount]
// - [ErrMissingSigner]
func (c *Client) BalanceGet(ctx context.Context, prm PrmBalanceGet) (accounting.Decimal, error) {
func (c *Client) BalanceGet(ctx context.Context, prm PrmBalanceGet) (res accounting.Decimal, err error) {
ts := time.Now()
defer func() {
c.sendStatistic(stat.MethodBalanceGet, time.Since(ts), err)
}()

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

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

Expand All @@ -61,8 +70,7 @@ func (c *Client) BalanceGet(ctx context.Context, prm PrmBalanceGet) (accounting.
// init call context

var (
cc contextCall
res accounting.Decimal
cc contextCall
)

c.initCallContext(&cc)
Expand Down Expand Up @@ -90,6 +98,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: 2 additions & 8 deletions pool/accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pool

import (
"context"
"time"

"github.com/nspcc-dev/neofs-sdk-go/accounting"
"github.com/nspcc-dev/neofs-sdk-go/client"
Expand All @@ -12,15 +11,10 @@ import (
//
// See details in [client.Client.BalanceGet].
func (p *Pool) BalanceGet(ctx context.Context, prm client.PrmBalanceGet) (accounting.Decimal, error) {
c, statUpdater, err := p.sdkClient()
c, _, err := p.sdkClient()
if err != nil {
return accounting.Decimal{}, err
}

start := time.Now()
acc, err := c.BalanceGet(ctx, prm)
statUpdater.incRequests(time.Since(start), methodBalanceGet)
statUpdater.updateErrorRate(err)

return acc, err
return c.BalanceGet(ctx, prm)
}
8 changes: 6 additions & 2 deletions pool/pool_aio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/nspcc-dev/neofs-sdk-go/object"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
"github.com/nspcc-dev/neofs-sdk-go/session"
"github.com/nspcc-dev/neofs-sdk-go/stat"
"github.com/nspcc-dev/neofs-sdk-go/user"
"github.com/nspcc-dev/neofs-sdk-go/waiter"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -115,12 +116,15 @@ func TestPoolInterfaceWithAIO(t *testing.T) {

nodeAddr := "grpc://localhost:8080"

agg := stat.NewAggregator()

opts := InitParameters{
signer: signer,
nodeParams: []NodeParam{
{1, nodeAddr, 1},
},
clientRebalanceInterval: 30 * time.Second,
statisticCallback: agg.Collect,
}

pool, err := NewPool(opts)
Expand All @@ -140,8 +144,8 @@ func TestPoolInterfaceWithAIO(t *testing.T) {
_, err = pool.BalanceGet(ctx, cmd)
require.NoError(t, err)

stat := pool.Statistic()
nodeStat, err := stat.Node(nodeAddr)
st := pool.Statistic()
nodeStat, err := st.Node(nodeAddr)
require.NoError(t, err)

require.Equal(t, uint64(1), nodeStat.methods[methodBalanceGet].allRequests)
Expand Down

0 comments on commit ba5c952

Please sign in to comment.