Dmitrii Stepanov
0400153b7d
Dynamically growing an unbounded buffers can cause a large amount of memory to be pinned and never be freed. Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
30 lines
447 B
Go
30 lines
447 B
Go
package putsvc
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
const (
|
|
defaultAllocSize = 1024
|
|
poolSliceMaxSize = 128 * 1024
|
|
)
|
|
|
|
type payload struct {
|
|
Data []byte
|
|
}
|
|
|
|
var putBytesPool = &sync.Pool{
|
|
New: func() any { return &payload{Data: make([]byte, 0, defaultAllocSize)} },
|
|
}
|
|
|
|
func getPayload() *payload {
|
|
return putBytesPool.Get().(*payload)
|
|
}
|
|
|
|
func putPayload(p *payload) {
|
|
if cap(p.Data) > poolSliceMaxSize {
|
|
return
|
|
}
|
|
p.Data = p.Data[:0]
|
|
putBytesPool.Put(p)
|
|
}
|