From 1af9b6d18bdfe7c14a9eaa21062be624d0b81b90 Mon Sep 17 00:00:00 2001 From: Alexander Chuprov Date: Mon, 25 Mar 2024 10:35:17 +0300 Subject: [PATCH] [#155] sdk-go: Add buffer support for payloadSizeLimiter Signed-off-by: Alexander Chuprov --- client/object_put.go | 3 +++ client/object_put_transformer.go | 12 ++++++++++++ object/transformer/transformer.go | 10 +++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/client/object_put.go b/client/object_put.go index 213ec7f..bf27f4c 100644 --- a/client/object_put.go +++ b/client/object_put.go @@ -4,6 +4,7 @@ import ( "context" "crypto/ecdsa" + buffPool "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/pool" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object" oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id" @@ -36,6 +37,8 @@ type PrmObjectPutInit struct { WithoutHomomorphHash bool Key *ecdsa.PrivateKey + + Pool *buffPool.BufferPool } // SetCopiesNumber sets number of object copies that is enough to consider put successful. diff --git a/client/object_put_transformer.go b/client/object_put_transformer.go index 523603c..2347a4f 100644 --- a/client/object_put_transformer.go +++ b/client/object_put_transformer.go @@ -3,6 +3,7 @@ package client import ( "context" + buffPool "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/pool" apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/transformer" @@ -26,6 +27,7 @@ func (c *Client) objectPutInitTransformer(prm PrmObjectPutInit) (*objectWriterTr MaxSize: prm.MaxSize, WithoutHomomorphicHash: prm.WithoutHomomorphHash, NetworkState: prm.EpochSource, + Pool: prm.Pool, }) return &w, nil } @@ -114,6 +116,7 @@ func (it *internalTarget) tryPutSingle(ctx context.Context, o *object.Object) (b } if err == nil { + it.returnBuffPool(o.Payload()) id, _ := o.ID() it.res = &ResObjectPut{ statusRes: res.statusRes, @@ -126,3 +129,12 @@ func (it *internalTarget) tryPutSingle(ctx context.Context, o *object.Object) (b } return true, err } + +func (it *internalTarget) returnBuffPool(playback []byte) { + if it.prm.Pool == nil { + return + } + var buffer buffPool.Buffer + buffer.Data = playback + it.prm.Pool.Put(&buffer) +} diff --git a/object/transformer/transformer.go b/object/transformer/transformer.go index 4b27de8..37d5471 100644 --- a/object/transformer/transformer.go +++ b/object/transformer/transformer.go @@ -6,6 +6,7 @@ import ( "crypto/sha256" "fmt" + buffPool "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/pool" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/checksum" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object" oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id" @@ -45,6 +46,7 @@ type Params struct { // functionality. Primary usecases are providing file size when putting an object // with the frostfs-cli or using Content-Length header in gateways. SizeHint uint64 + Pool *buffPool.BufferPool } // NewPayloadSizeLimiter returns ObjectTarget instance that restricts payload length @@ -137,7 +139,13 @@ func (s *payloadSizeLimiter) initializeCurrent() { payloadSize = remaining % s.MaxSize } } - s.payload = make([]byte, 0, payloadSize) + + if s.Pool == nil { + s.payload = make([]byte, 0, payloadSize) + } else { + buffer := s.Pool.Get(uint32(payloadSize)) + s.payload = buffer.Data[:0] + } } func (s *payloadSizeLimiter) initPayloadHashers() {