From 0d5b07f886239fcaf047d53c501364aec054df8b Mon Sep 17 00:00:00 2001 From: HynoR <20227709+HynoR@users.noreply.github.com> Date: Tue, 25 Nov 2025 11:53:00 +0800 Subject: [PATCH 1/2] feat: Implement Gzip compression for successful API responses --- agent/app/api/v2/file.go | 6 ++++- agent/app/api/v2/helper/helper.go | 39 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/agent/app/api/v2/file.go b/agent/app/api/v2/file.go index 15a7df0c171c..44753f332713 100644 --- a/agent/app/api/v2/file.go +++ b/agent/app/api/v2/file.go @@ -841,7 +841,11 @@ func (b *BaseApi) ReadFileByLine(c *gin.Context) { helper.InternalServer(c, err) return } - helper.SuccessWithData(c, res) + if res.TotalLines > 100 { + helper.SuccessWithDataGzipped(c, res) + } else { + helper.SuccessWithData(c, res) + } } // @Tags File diff --git a/agent/app/api/v2/helper/helper.go b/agent/app/api/v2/helper/helper.go index 6a4f0e8c885c..0cc04fb58b6f 100644 --- a/agent/app/api/v2/helper/helper.go +++ b/agent/app/api/v2/helper/helper.go @@ -1,10 +1,13 @@ package helper import ( + "compress/gzip" "context" + "encoding/json" "fmt" "net/http" "strconv" + "strings" "github.com/1Panel-dev/1Panel/agent/global" "gorm.io/gorm" @@ -46,6 +49,42 @@ func SuccessWithData(ctx *gin.Context, data interface{}) { ctx.Abort() } +func SuccessWithDataGzipped(ctx *gin.Context, data interface{}) { + acceptEncoding := ctx.GetHeader("Accept-Encoding") + if !strings.Contains(acceptEncoding, "gzip") { + SuccessWithData(ctx, data) + return + } + if data == nil { + data = gin.H{} + } + res := dto.Response{ + Code: http.StatusOK, + Data: data, + } + + jsonBytes, err := json.Marshal(res) + if err != nil { + ctx.String(http.StatusInternalServerError, "json marshal error") + return + } + + ctx.Header("Content-Encoding", "gzip") + ctx.Header("Content-Type", "application/json; charset=utf-8") + + gz := gzip.NewWriter(ctx.Writer) + defer gz.Close() + + _, err = gz.Write(jsonBytes) + if err != nil { + ctx.String(http.StatusInternalServerError, "gzip write error") + return + } + + ctx.Status(http.StatusOK) + ctx.Abort() +} + func Success(ctx *gin.Context) { res := dto.Response{ Code: http.StatusOK, From 39d0972aa4bc3dc552f9be94965c30451234e40f Mon Sep 17 00:00:00 2001 From: HynoR <20227709+HynoR@users.noreply.github.com> Date: Tue, 25 Nov 2025 12:24:41 +0800 Subject: [PATCH 2/2] refactor: Optimize Gzip compression handling in API responses using sync.Pool --- agent/app/api/v2/helper/helper.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/agent/app/api/v2/helper/helper.go b/agent/app/api/v2/helper/helper.go index 0cc04fb58b6f..3439a9893351 100644 --- a/agent/app/api/v2/helper/helper.go +++ b/agent/app/api/v2/helper/helper.go @@ -8,6 +8,7 @@ import ( "net/http" "strconv" "strings" + "sync" "github.com/1Panel-dev/1Panel/agent/global" "gorm.io/gorm" @@ -49,9 +50,14 @@ func SuccessWithData(ctx *gin.Context, data interface{}) { ctx.Abort() } +var gzipWriterPool = sync.Pool{ + New: func() interface{} { + return gzip.NewWriter(nil) + }, +} + func SuccessWithDataGzipped(ctx *gin.Context, data interface{}) { - acceptEncoding := ctx.GetHeader("Accept-Encoding") - if !strings.Contains(acceptEncoding, "gzip") { + if !strings.Contains(ctx.GetHeader("Accept-Encoding"), "gzip") { SuccessWithData(ctx, data) return } @@ -62,26 +68,21 @@ func SuccessWithDataGzipped(ctx *gin.Context, data interface{}) { Code: http.StatusOK, Data: data, } - jsonBytes, err := json.Marshal(res) if err != nil { - ctx.String(http.StatusInternalServerError, "json marshal error") + ErrorWithDetail(ctx, http.StatusInternalServerError, "ErrInternalServer", err) return } ctx.Header("Content-Encoding", "gzip") ctx.Header("Content-Type", "application/json; charset=utf-8") - - gz := gzip.NewWriter(ctx.Writer) - defer gz.Close() - - _, err = gz.Write(jsonBytes) - if err != nil { - ctx.String(http.StatusInternalServerError, "gzip write error") - return - } - ctx.Status(http.StatusOK) + + gz := gzipWriterPool.Get().(*gzip.Writer) + gz.Reset(ctx.Writer) + _, _ = gz.Write(jsonBytes) + _ = gz.Close() + gzipWriterPool.Put(gz) ctx.Abort() }