From 206ff967c78320dde4e2d077052178f14cbb43bd Mon Sep 17 00:00:00 2001 From: TangYang Date: Tue, 28 May 2024 08:36:45 +0000 Subject: [PATCH] add HERTZ_EXP_FASTGO_ENABLE environment variable to enable fastgo comopression and decompression Signed-off-by: TangYang --- pkg/common/compress/compress.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/common/compress/compress.go b/pkg/common/compress/compress.go index b3250ce4b..31554884a 100644 --- a/pkg/common/compress/compress.go +++ b/pkg/common/compress/compress.go @@ -46,6 +46,8 @@ import ( "compress/gzip" "fmt" "io" + "os" + "strconv" "sync" "github.com/intel/fastgo" @@ -57,6 +59,14 @@ import ( "github.com/cloudwego/hertz/pkg/network" ) +var fastgoEnable bool + +func init() { + if fastgo.Optimized() { + fastgoEnable, _ = strconv.ParseBool(os.Getenv("HERTZ_EXP_FASTGO_ENABLE")) + } +} + const CompressDefaultCompression = 6 // flate.DefaultCompression var ( @@ -144,7 +154,7 @@ func (r *byteSliceReader) Read(p []byte) (int, error) { func acquireGzipReader(r io.Reader) (Reader, error) { v := readerPool.Get() if v == nil { - if fastgo.Optimized() { + if fastgoEnable { return igzip.NewReader(r) } @@ -228,7 +238,7 @@ func acquireRealGzipWriter(w io.Writer, level int) Writer { if v == nil { var zw Writer var err error - if fastgo.Optimized() && level <= 2 { + if fastgoEnable && level <= 2 { zw, err = igzip.NewWriterLevel(w, level) } else { zw, err = gzip.NewWriterLevel(w, level)