mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-02-07 11:36:25 +02:00
Some optimizations
This commit is contained in:
parent
f679a7ddec
commit
e453d9db51
2
gzip.go
2
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()
|
||||
|
21
process.go
21
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)
|
||||
|
24
server.go
24
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)
|
||||
|
||||
gzipData(data, buf)
|
||||
dataToRespond = buf.Bytes()
|
||||
}
|
||||
buf.Reset()
|
||||
|
||||
rw.Header().Set("Content-Length", strconv.Itoa(len(dataToRespond)))
|
||||
rw.WriteHeader(200)
|
||||
rw.Write(dataToRespond)
|
||||
gzipData(data, buf)
|
||||
|
||||
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))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user