浏览代码

No bufpool in svg

Viktor Sokolov 2 月之前
父节点
当前提交
71142911e0
共有 3 个文件被更改,包括 17 次插入13 次删除
  1. 1 1
      imagedata/image_data.go
  2. 15 11
      svg/svg.go
  3. 1 1
      vips/source.go

+ 1 - 1
imagedata/image_data.go

@@ -82,10 +82,10 @@ func (d *imageDataAsyncBuffer) Reader() io.ReadSeeker {
 // Close closes the response body (hence, response) and the async buffer itself
 func (d *imageDataAsyncBuffer) Close() error {
 	d.cancelOnce.Do(func() {
+		d.b.Close()
 		for _, cancel := range d.cancel {
 			cancel()
 		}
-		d.b.Close()
 	})
 
 	return nil

+ 15 - 11
svg/svg.go

@@ -2,33 +2,37 @@ package svg
 
 import (
 	"bytes"
-	"context"
+	"errors"
 	"io"
 	"strings"
+	"sync"
 
 	"github.com/tdewolff/parse/v2"
 	"github.com/tdewolff/parse/v2/xml"
 
-	"github.com/imgproxy/imgproxy/v3/bufpool"
-	"github.com/imgproxy/imgproxy/v3/config"
 	"github.com/imgproxy/imgproxy/v3/imagedata"
 	"github.com/imgproxy/imgproxy/v3/imagetype"
 )
 
-var downloadBufPool *bufpool.Pool = bufpool.New("download", config.Workers, config.DownloadBufferSize)
-
-func BorrowBuffer() (*bytes.Buffer, context.CancelFunc) {
-	buf := downloadBufPool.Get(0, false)
-	cancel := func() { downloadBufPool.Put(buf) }
-
-	return buf, cancel
+var pool = sync.Pool{
+	New: func() any {
+		return bytes.NewBuffer(nil)
+	},
 }
 
 func Sanitize(data imagedata.ImageData) (imagedata.ImageData, error) {
 	r := data.Reader()
 	l := xml.NewLexer(parse.NewInput(r))
 
-	buf, cancel := BorrowBuffer()
+	buf, ok := pool.Get().(*bytes.Buffer)
+	if !ok {
+		return nil, errors.New("svg.Sanitize: failed to get buffer from pool")
+	}
+	buf.Reset()
+
+	cancel := func() {
+		pool.Put(buf)
+	}
 
 	ignoreTag := 0
 

+ 1 - 1
vips/source.go

@@ -33,7 +33,7 @@ func imgproxyReaderSeek(handle C.uintptr_t, offset C.int64_t, whence int) C.int6
 	h := cgo.Handle(handle)
 	r, ok := h.Value().(io.ReadSeeker)
 	if !ok {
-		vipsError("imgproxyReaderSeek", "failed to cast handle to *source")
+		vipsError("imgproxyReaderSeek", "failed to cast handle to io.ReadSeeker")
 		return -1
 	}