1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-03 10:43:58 +02:00

Delayed download buffer grow

This commit is contained in:
DarthSim 2022-08-01 19:48:23 +06:00
parent a9dda9ca4e
commit ad5bd69f80
3 changed files with 22 additions and 7 deletions

View File

@ -1,6 +1,8 @@
# Changelog
## [Unreleased]
### Fix
- Fix memory bloat in some cases.
## [3.7.0] - 2022-07-27
### Add

View File

@ -73,7 +73,7 @@ func (p *Pool) calibrateAndClean() {
metrics.SetBufferMaxSize(p.name, p.maxSize)
}
func (p *Pool) Get(size int) *bytes.Buffer {
func (p *Pool) Get(size int, grow bool) *bytes.Buffer {
p.mutex.Lock()
defer p.mutex.Unlock()
@ -115,7 +115,10 @@ func (p *Pool) Get(size int) *bytes.Buffer {
buf.Reset()
growSize := imath.Max(size, p.defaultSize)
growSize := p.defaultSize
if grow {
growSize = imath.Max(size, growSize)
}
if growSize > buf.Cap() {
buf.Grow(growSize)
@ -145,7 +148,7 @@ func (p *Pool) Put(buf *bytes.Buffer) {
if b == nil {
p.buffers[i] = buf
if buf.Cap() > 0 {
if buf.Len() > 0 {
metrics.ObserveBufferSize(p.name, buf.Cap())
}

View File

@ -44,7 +44,7 @@ func readAndCheckImage(r io.Reader, contentLength int) (*ImageData, error) {
return nil, ErrSourceFileTooBig
}
buf := downloadBufPool.Get(contentLength)
buf := downloadBufPool.Get(contentLength, false)
cancel := func() { downloadBufPool.Put(buf) }
if config.MaxSrcFileSize > 0 {
@ -54,17 +54,27 @@ func readAndCheckImage(r io.Reader, contentLength int) (*ImageData, error) {
br := bufreader.New(r, buf)
meta, err := imagemeta.DecodeMeta(br)
if err == imagemeta.ErrFormat {
return nil, ErrSourceImageTypeNotSupported
}
if err != nil {
buf.Reset()
cancel()
if err == imagemeta.ErrFormat {
return nil, ErrSourceImageTypeNotSupported
}
return nil, checkTimeoutErr(err)
}
if err = security.CheckDimensions(meta.Width(), meta.Height()); err != nil {
buf.Reset()
cancel()
return nil, err
}
if contentLength > buf.Cap() {
buf.Grow(contentLength - buf.Len())
}
if err = br.Flush(); err != nil {
cancel()
return nil, checkTimeoutErr(err)