Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse buffers #525

Merged
merged 5 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bearer/bearer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func TestToken_ReadFromV2(t *testing.T) {

var s neofscrypto.Signature

require.NoError(t, s.CalculateMarshalled(signer, &body))
require.NoError(t, s.CalculateMarshalled(signer, &body, nil))

s.WriteToV2(&sig)

Expand Down
37 changes: 37 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"fmt"
"sync"
"time"

"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
Expand All @@ -13,6 +14,11 @@ import (
"github.com/nspcc-dev/neofs-sdk-go/stat"
)

const (
// max GRPC message size.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"default"?

defaultBufferSize = 4194304 // 4MB
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

)

// Client represents virtual connection to the NeoFS network to communicate
// with NeoFS server using NeoFS API protocol. It is designed to provide
// an abstraction interface from the protocol details of data transfer over
Expand Down Expand Up @@ -51,6 +57,8 @@ type Client struct {

endpoint string
nodeKey []byte

buffers *sync.Pool
}

// New creates an instance of Client initialized with the given parameters.
Expand All @@ -65,6 +73,21 @@ func New(prm PrmInit) (*Client, error) {

prm.signer = neofsecdsa.SignerRFC6979(pk.PrivateKey)

if prm.buffers != nil {
c.buffers = prm.buffers
} else {
size := prm.signMessageBufferSizes
if size == 0 {
size = defaultBufferSize
}

c.buffers = &sync.Pool{}
c.buffers.New = func() any {
b := make([]byte, size)
return &b
}
}

c.prm = prm
return c, nil
}
Expand Down Expand Up @@ -175,6 +198,20 @@ type PrmInit struct {
netMagic uint64

statisticCallback stat.OperationCallback

signMessageBufferSizes uint64
buffers *sync.Pool
}

// SetSignMessageBufferSizes sets single buffer size to the buffers pool inside client.
// This pool are using in GRPC message signing process and helps to reduce memory allocations.
func (x *PrmInit) SetSignMessageBufferSizes(size uint64) {
x.signMessageBufferSizes = size
}

// SetSignMessageBuffers sets buffers which are using in GRPC message signing process and helps to reduce memory allocations.
func (x *PrmInit) SetSignMessageBuffers(buffers *sync.Pool) {
x.buffers = buffers
}

// SetResponseInfoCallback makes the Client to pass ResponseMetaInfo from each
Expand Down
15 changes: 14 additions & 1 deletion client/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ type contextCall struct {

// function of writing response fields to the resulting structure (optional)
result func(v2 responseV2)

buf []byte
bufCleanCallback func()
}

type request interface {
Expand Down Expand Up @@ -151,7 +154,7 @@ func (x *contextCall) writeRequest() bool {
x.req.SetVerificationHeader(nil)

// sign the request
x.err = signServiceMessage(x.signer, x.req)
x.err = signServiceMessage(x.signer, x.req, x.buf)
if x.err != nil {
x.err = fmt.Errorf("sign request: %w", x.err)
return false
Expand Down Expand Up @@ -255,6 +258,10 @@ func (x *contextCall) processCall() bool {

// write request
ok := x.writeRequest()
if x.bufCleanCallback != nil {
x.bufCleanCallback()
}

if !ok {
return false
}
Expand All @@ -279,6 +286,12 @@ func (c *Client) initCallContext(ctx *contextCall) {
ctx.signer = c.prm.signer
ctx.callbackResp = c.prm.cbRespInfo
ctx.netMagic = c.prm.netMagic

buf := c.buffers.Get().(*[]byte)
ctx.buf = *buf
ctx.bufCleanCallback = func() {
c.buffers.Put(buf)
}
}

// ExecRaw executes f with underlying github.com/nspcc-dev/neofs-api-go/v2/rpc/client.Client
Expand Down
2 changes: 1 addition & 1 deletion client/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ func (c *Client) ContainerSetEACL(ctx context.Context, table eacl.Table, signer
eaclV2 := table.ToV2()

var sig neofscrypto.Signature
err = sig.CalculateMarshalled(signer, eaclV2)
err = sig.CalculateMarshalled(signer, eaclV2, nil)
if err != nil {
err = fmt.Errorf("calculate signature: %w", err)
return err
Expand Down
34 changes: 17 additions & 17 deletions client/container_statistic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func TestClientStatistic_AccountBalance(t *testing.T) {
resp.SetBody(&body)
resp.SetMetaHeader(&meta)

err := signServiceMessage(signer, &resp)
err := signServiceMessage(signer, &resp, nil)
if err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}
Expand Down Expand Up @@ -199,7 +199,7 @@ func TestClientStatistic_ContainerPut(t *testing.T) {
resp.SetBody(&body)
resp.SetMetaHeader(&meta)

err := signServiceMessage(signer, &resp)
err := signServiceMessage(signer, &resp, nil)
if err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}
Expand Down Expand Up @@ -251,7 +251,7 @@ func TestClientStatistic_ContainerGet(t *testing.T) {
resp.SetBody(&body)
resp.SetMetaHeader(&meta)

if err = signServiceMessage(signer, &resp); err != nil {
if err = signServiceMessage(signer, &resp, nil); err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}

Expand Down Expand Up @@ -281,7 +281,7 @@ func TestClientStatistic_ContainerList(t *testing.T) {
resp.SetBody(&body)
resp.SetMetaHeader(&meta)

if err := signServiceMessage(signer, &resp); err != nil {
if err := signServiceMessage(signer, &resp, nil); err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}

Expand Down Expand Up @@ -311,7 +311,7 @@ func TestClientStatistic_ContainerDelete(t *testing.T) {
resp.SetBody(&body)
resp.SetMetaHeader(&meta)

if err := signServiceMessage(signer, &resp); err != nil {
if err := signServiceMessage(signer, &resp, nil); err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}

Expand Down Expand Up @@ -344,7 +344,7 @@ func TestClientStatistic_ContainerEacl(t *testing.T) {
resp.SetBody(&body)
resp.SetMetaHeader(&meta)

if err := signServiceMessage(signer, &resp); err != nil {
if err := signServiceMessage(signer, &resp, nil); err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}

Expand Down Expand Up @@ -374,7 +374,7 @@ func TestClientStatistic_ContainerSetEacl(t *testing.T) {
resp.SetBody(&body)
resp.SetMetaHeader(&meta)

if err := signServiceMessage(signer, &resp); err != nil {
if err := signServiceMessage(signer, &resp, nil); err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}

Expand Down Expand Up @@ -405,7 +405,7 @@ func TestClientStatistic_ContainerAnnounceUsedSpace(t *testing.T) {
resp.SetBody(&body)
resp.SetMetaHeader(&meta)

if err := signServiceMessage(signer, &resp); err != nil {
if err := signServiceMessage(signer, &resp, nil); err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}

Expand Down Expand Up @@ -451,7 +451,7 @@ func TestClientStatistic_ContainerSyncContainerWithNetwork(t *testing.T) {
resp.SetBody(&body)
resp.SetMetaHeader(&meta)

if err := signServiceMessage(signer, &resp); err != nil {
if err := signServiceMessage(signer, &resp, nil); err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}

Expand Down Expand Up @@ -490,7 +490,7 @@ func TestClientStatistic_ContainerEndpointInfo(t *testing.T) {
resp.SetBody(&body)
resp.SetMetaHeader(&meta)

if err := signServiceMessage(signer, &resp); err != nil {
if err := signServiceMessage(signer, &resp, nil); err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}

Expand Down Expand Up @@ -522,7 +522,7 @@ func TestClientStatistic_ContainerNetMapSnapshot(t *testing.T) {
resp.SetBody(&body)
resp.SetMetaHeader(&meta)

if err := signServiceMessage(signer, &resp); err != nil {
if err := signServiceMessage(signer, &resp, nil); err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}

Expand Down Expand Up @@ -556,7 +556,7 @@ func TestClientStatistic_CreateSession(t *testing.T) {
resp.SetBody(&body)
resp.SetMetaHeader(&meta)

if err := signServiceMessage(signer, &resp); err != nil {
if err := signServiceMessage(signer, &resp, nil); err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}

Expand Down Expand Up @@ -648,7 +648,7 @@ func TestClientStatistic_ObjectDelete(t *testing.T) {
resp.SetBody(&body)
resp.SetMetaHeader(&meta)

if err := signServiceMessage(signer, &resp); err != nil {
if err := signServiceMessage(signer, &resp, nil); err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}

Expand Down Expand Up @@ -718,7 +718,7 @@ func TestClientStatistic_ObjectHead(t *testing.T) {
resp.SetBody(&body)
resp.SetMetaHeader(&meta)

if err := signServiceMessage(signer, &resp); err != nil {
if err := signServiceMessage(signer, &resp, nil); err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}

Expand Down Expand Up @@ -789,7 +789,7 @@ func TestClientStatistic_ObjectHash(t *testing.T) {
resp.SetBody(&body)
resp.SetMetaHeader(&meta)

if err := signServiceMessage(signer, &resp); err != nil {
if err := signServiceMessage(signer, &resp, nil); err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}

Expand Down Expand Up @@ -859,7 +859,7 @@ func TestClientStatistic_AnnounceIntermediateTrust(t *testing.T) {
resp.SetBody(&body)
resp.SetMetaHeader(&meta)

if err := signServiceMessage(signer, &resp); err != nil {
if err := signServiceMessage(signer, &resp, nil); err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}

Expand Down Expand Up @@ -891,7 +891,7 @@ func TestClientStatistic_MethodAnnounceLocalTrust(t *testing.T) {
resp.SetBody(&body)
resp.SetMetaHeader(&meta)

if err := signServiceMessage(signer, &resp); err != nil {
if err := signServiceMessage(signer, &resp, nil); err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}

Expand Down
4 changes: 3 additions & 1 deletion client/netmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ func (c *Client) NetMapSnapshot(ctx context.Context, _ PrmNetMapSnapshot) (netma
req.SetBody(&body)
c.prepareRequest(&req, &meta)

err = signServiceMessage(c.prm.signer, &req)
buf := c.buffers.Get().(*[]byte)
err = signServiceMessage(c.prm.signer, &req, *buf)
c.buffers.Put(buf)
if err != nil {
err = fmt.Errorf("sign request: %w", err)
return netmap.NetMap{}, err
Expand Down
2 changes: 1 addition & 1 deletion client/netmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (x *serverNetMap) netMapSnapshot(_ context.Context, req v2netmap.SnapshotRe
resp.SetMetaHeader(&meta)

if x.signResponse {
err = signServiceMessage(x.signer, &resp)
err = signServiceMessage(x.signer, &resp, nil)
if err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}
Expand Down
4 changes: 3 additions & 1 deletion client/object_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ func (c *Client) ObjectDelete(ctx context.Context, containerID cid.ID, objectID
req.SetBody(&body)
c.prepareRequest(&req, &prm.meta)

err = signServiceMessage(signer, &req)
buf := c.buffers.Get().(*[]byte)
err = signServiceMessage(signer, &req, *buf)
c.buffers.Put(buf)
if err != nil {
err = fmt.Errorf("sign request: %w", err)
return oid.ID{}, err
Expand Down
14 changes: 9 additions & 5 deletions client/object_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,9 @@ func (c *Client) ObjectGetInit(ctx context.Context, containerID cid.ID, objectID

req.SetBody(&body)
c.prepareRequest(&req, &prm.meta)

err = signServiceMessage(signer, &req)
buf := c.buffers.Get().(*[]byte)
err = signServiceMessage(signer, &req, *buf)
c.buffers.Put(buf)
if err != nil {
err = fmt.Errorf("sign request: %w", err)
return hdr, nil, err
Expand Down Expand Up @@ -369,8 +370,9 @@ func (c *Client) ObjectHead(ctx context.Context, containerID cid.ID, objectID oi
req.SetBody(&body)
c.prepareRequest(&req, &prm.meta)

// sign the request
err = signServiceMessage(signer, &req)
buf := c.buffers.Get().(*[]byte)
err = signServiceMessage(signer, &req, *buf)
c.buffers.Put(buf)
if err != nil {
err = fmt.Errorf("sign request: %w", err)
return nil, err
Expand Down Expand Up @@ -616,7 +618,9 @@ func (c *Client) ObjectRangeInit(ctx context.Context, containerID cid.ID, object
req.SetBody(&body)
c.prepareRequest(&req, &prm.meta)

err = signServiceMessage(signer, &req)
buf := c.buffers.Get().(*[]byte)
err = signServiceMessage(signer, &req, *buf)
c.buffers.Put(buf)
if err != nil {
err = fmt.Errorf("sign request: %w", err)
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion client/object_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ func (c *Client) ObjectHash(ctx context.Context, containerID cid.ID, objectID oi
c.prepareRequest(&req, &prm.meta)
req.SetBody(&prm.body)

err = signServiceMessage(signer, &req)
buf := c.buffers.Get().(*[]byte)
err = signServiceMessage(signer, &req, *buf)
c.buffers.Put(buf)
if err != nil {
err = fmt.Errorf("sign request: %w", err)
return nil, err
Expand Down
Loading
Loading