[#199] putsvc: Refactor streamer pool

Resolve staticcheck linter for putBytesPool

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-04-03 13:09:44 +03:00 committed by Gitea
parent 14d894178e
commit cecea8053a
2 changed files with 13 additions and 9 deletions

View file

@ -28,7 +28,7 @@ type distributedTarget struct {
obj *objectSDK.Object obj *objectSDK.Object
objMeta object.ContentMeta objMeta object.ContentMeta
payload []byte payload *payload
nodeTargetInitializer func(nodeDesc) preparedObjectTarget nodeTargetInitializer func(nodeDesc) preparedObjectTarget
@ -122,7 +122,7 @@ func (t *distributedTarget) WriteHeader(obj *objectSDK.Object) error {
} }
func (t *distributedTarget) Write(p []byte) (n int, err error) { func (t *distributedTarget) Write(p []byte) (n int, err error) {
t.payload = append(t.payload, p...) t.payload.Data = append(t.payload.Data, p...)
return len(p), nil return len(p), nil
} }
@ -133,7 +133,7 @@ func (t *distributedTarget) Close() (*transformer.AccessIdentifiers, error) {
t.payload = nil t.payload = nil
}() }()
t.obj.SetPayload(t.payload) t.obj.SetPayload(t.payload.Data)
var err error var err error

View file

@ -6,15 +6,19 @@ import (
const defaultAllocSize = 1024 const defaultAllocSize = 1024
type payload struct {
Data []byte
}
var putBytesPool = &sync.Pool{ var putBytesPool = &sync.Pool{
New: func() any { return make([]byte, 0, defaultAllocSize) }, New: func() any { return &payload{Data: make([]byte, 0, defaultAllocSize)} },
} }
func getPayload() []byte { func getPayload() *payload {
return putBytesPool.Get().([]byte) return putBytesPool.Get().(*payload)
} }
func putPayload(p []byte) { func putPayload(p *payload) {
//nolint:staticcheck p.Data = p.Data[:0]
putBytesPool.Put(p[:0]) putBytesPool.Put(p)
} }