Răsfoiți Sursa

Some optimizations

DarthSim 6 ani în urmă
părinte
comite
e453d9db51
3 a modificat fișierele cu 32 adăugiri și 13 ștergeri
  1. 2 0
      gzip.go
  2. 18 3
      process.go
  3. 12 10
      server.go

+ 2 - 0
gzip.go

@@ -18,6 +18,8 @@ var gzipPool = sync.Pool{
 
 func gzipData(data []byte, w io.Writer) {
 	gz := gzipPool.Get().(*gzip.Writer)
+	defer gzipPool.Put(gz)
+
 	gz.Reset(w)
 	gz.Write(data)
 	gz.Close()

+ 18 - 3
process.go

@@ -51,6 +51,8 @@ func initVips() {
 	C.vips_cache_set_max_mem(0)
 	C.vips_cache_set_max(0)
 
+	C.vips_concurrency_set(1)
+
 	if len(os.Getenv("IMGPROXY_VIPS_LEAK_CHECK")) > 0 {
 		C.vips_leak_set(C.gboolean(1))
 	}
@@ -491,6 +493,9 @@ func transformGif(ctx context.Context, img **C.struct__VipsImage, po *processing
 }
 
 func processImage(ctx context.Context) ([]byte, error) {
+	runtime.LockOSThread()
+	defer runtime.UnlockOSThread()
+
 	if newRelicEnabled {
 		newRelicCancel := startNewRelicSegment(ctx, "Processing image")
 		defer newRelicCancel()
@@ -692,14 +697,21 @@ func vipsImageHasAlpha(img *C.struct__VipsImage) bool {
 
 func vipsGetInt(img *C.struct__VipsImage, name string) (int, error) {
 	var i C.int
-	if C.vips_image_get_int(img, C.CString(name), &i) != 0 {
+
+	cname := C.CString(name)
+	defer C.free(unsafe.Pointer(cname))
+
+	if C.vips_image_get_int(img, cname, &i) != 0 {
 		return 0, vipsError()
 	}
 	return int(i), nil
 }
 
 func vipsSetInt(img *C.struct__VipsImage, name string, value int) {
-	C.vips_image_set_int(img, C.CString(name), C.int(value))
+	cname := C.CString(name)
+	defer C.free(unsafe.Pointer(cname))
+
+	C.vips_image_set_int(img, cname, C.int(value))
 }
 
 func vipsPremultiply(img **C.struct__VipsImage) (C.VipsBandFormat, error) {
@@ -848,7 +860,10 @@ func vipsImportColourProfile(img **C.struct__VipsImage) error {
 			return err
 		}
 
-		if C.vips_icc_import_go(*img, &tmp, C.CString(profile)) != 0 {
+		cprofile := C.CString(profile)
+		defer C.free(unsafe.Pointer(cprofile))
+
+		if C.vips_icc_import_go(*img, &tmp, cprofile) != 0 {
 			return vipsError()
 		}
 		C.swap_and_clear(img, tmp)

+ 12 - 10
server.go

@@ -117,22 +117,24 @@ func respondWithImage(ctx context.Context, reqID string, r *http.Request, rw htt
 	rw.Header().Set("Content-Type", mimes[po.Format])
 	rw.Header().Set("Content-Disposition", contentDispositions[po.Format])
 
-	dataToRespond := data
-
 	if conf.GZipCompression > 0 && strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
-		rw.Header().Set("Content-Encoding", "gzip")
-
 		buf := responseBufPool.Get().(*bytes.Buffer)
-		buf.Reset()
 		defer responseBufPool.Put(buf)
 
+		buf.Reset()
+
 		gzipData(data, buf)
-		dataToRespond = buf.Bytes()
-	}
 
-	rw.Header().Set("Content-Length", strconv.Itoa(len(dataToRespond)))
-	rw.WriteHeader(200)
-	rw.Write(dataToRespond)
+		rw.Header().Set("Content-Encoding", "gzip")
+		rw.Header().Set("Content-Length", strconv.Itoa(buf.Len()))
+
+		rw.WriteHeader(200)
+		buf.WriteTo(rw)
+	} else {
+		rw.Header().Set("Content-Length", strconv.Itoa(len(data)))
+		rw.WriteHeader(200)
+		rw.Write(data)
+	}
 
 	logResponse(200, fmt.Sprintf("[%s] Processed in %s: %s; %+v", reqID, getTimerSince(ctx), getImageURL(ctx), po))
 }