From edca87906db1f99008b8f68c1d90cea655450cb2 Mon Sep 17 00:00:00 2001 From: wjqserver <114663932+WJQSERVER@users.noreply.github.com> Date: Sun, 6 Jul 2025 17:59:24 +0800 Subject: [PATCH] update deps & use copyb high perfromance pool & switch to stream json encoder --- context.go | 21 ++++++++++++++------- go.mod | 6 +++--- go.sum | 14 ++++++-------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/context.go b/context.go index c9586dd..0e3b29a 100644 --- a/context.go +++ b/context.go @@ -267,15 +267,22 @@ func (c *Context) String(code int, format string, values ...interface{}) { func (c *Context) JSON(code int, obj interface{}) { c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8") c.Writer.WriteHeader(code) - // JSON 编码 - jsonBytes, err := json.Marshal(obj) - if err != nil { + /* + // JSON 编码 + jsonBytes, err := json.Marshal(obj) + if err != nil { + c.AddError(fmt.Errorf("failed to marshal JSON: %w", err)) + //c.String(http.StatusInternalServerError, "Internal Server Error: Failed to marshal JSON") + c.ErrorUseHandle(http.StatusInternalServerError, fmt.Errorf("failed to marshal JSON: %w", err)) + return + } + c.Writer.Write(jsonBytes) + */ + if err := json.MarshalWrite(c.Writer, obj); err != nil { c.AddError(fmt.Errorf("failed to marshal JSON: %w", err)) - //c.String(http.StatusInternalServerError, "Internal Server Error: Failed to marshal JSON") c.ErrorUseHandle(http.StatusInternalServerError, fmt.Errorf("failed to marshal JSON: %w", err)) return } - c.Writer.Write(jsonBytes) } // GOB 向响应写入GOB数据 @@ -447,7 +454,7 @@ func (c *Context) GetReqBodyFull() ([]byte, error) { return nil, nil } defer c.Request.Body.Close() // 确保请求体被关闭 - data, err := io.ReadAll(c.Request.Body) + data, err := copyb.ReadAll(c.Request.Body) if err != nil { c.AddError(fmt.Errorf("failed to read request body: %w", err)) return nil, fmt.Errorf("failed to read request body: %w", err) @@ -461,7 +468,7 @@ func (c *Context) GetReqBodyBuffer() (*bytes.Buffer, error) { return nil, nil } defer c.Request.Body.Close() // 确保请求体被关闭 - data, err := io.ReadAll(c.Request.Body) + data, err := copyb.ReadAll(c.Request.Body) if err != nil { c.AddError(fmt.Errorf("failed to read request body: %w", err)) return nil, fmt.Errorf("failed to read request body: %w", err) diff --git a/go.mod b/go.mod index 4fb176d..18194de 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,10 @@ module github.com/infinite-iroha/touka go 1.24.4 require ( - github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.4 - github.com/WJQSERVER-STUDIO/httpc v0.7.1 + github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.6 + github.com/WJQSERVER-STUDIO/httpc v0.8.0 github.com/fenthope/reco v0.0.3 - github.com/go-json-experiment/json v0.0.0-20250517221953-25912455fbc8 + github.com/go-json-experiment/json v0.0.0-20250626171732-1a886bd29d1b ) require github.com/valyala/bytebufferpool v1.0.0 // indirect diff --git a/go.sum b/go.sum index 6e1be02..f90f774 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,10 @@ -github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.4 h1:JLtFd00AdFg/TP+dtvIzLkdHwKUGPOAijN1sMtEYoFg= -github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.4/go.mod h1:FZ6XE+4TKy4MOfX1xWKe6Rwsg0ucYFCdNh1KLvyKTfc= -github.com/WJQSERVER-STUDIO/httpc v0.7.0 h1:iHhqlxppJBjlmvsIjvLZKRbWXqSdbeSGGofjHGmqGJc= -github.com/WJQSERVER-STUDIO/httpc v0.7.0/go.mod h1:M7KNUZjjhCkzzcg9lBPs9YfkImI+7vqjAyjdA19+joE= -github.com/WJQSERVER-STUDIO/httpc v0.7.1 h1:D3NlfY52pwKIOSzkdRrLinUynyKELrcPZEO8QjlBq2M= -github.com/WJQSERVER-STUDIO/httpc v0.7.1/go.mod h1:M7KNUZjjhCkzzcg9lBPs9YfkImI+7vqjAyjdA19+joE= +github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.6 h1:/50VJYXd6jcu+p5BnEBDyiX0nAyGxas1W3DCnrYMxMY= +github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.6/go.mod h1:FZ6XE+4TKy4MOfX1xWKe6Rwsg0ucYFCdNh1KLvyKTfc= +github.com/WJQSERVER-STUDIO/httpc v0.8.0 h1:G7inJ5EEsg5+BkeFiNIo/6+Mj7Ygiq85yMT3Ld7frJY= +github.com/WJQSERVER-STUDIO/httpc v0.8.0/go.mod h1:50297rvgppmgPbZEtWzTWgkomoqPREkGy9T3Y/NqN7o= github.com/fenthope/reco v0.0.3 h1:RmnQ0D9a8PWtwOODawitTe4BztTnS9wYwrDbipISNq4= github.com/fenthope/reco v0.0.3/go.mod h1:mDkGLHte5udWTIcjQTxrABRcf56SSdxBOCLgrRDwI/Y= -github.com/go-json-experiment/json v0.0.0-20250517221953-25912455fbc8 h1:o8UqXPI6SVwQt04RGsqKp3qqmbOfTNMqDrWsc4O47kk= -github.com/go-json-experiment/json v0.0.0-20250517221953-25912455fbc8/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M= +github.com/go-json-experiment/json v0.0.0-20250626171732-1a886bd29d1b h1:ooF9/NzXkXL3OOLRwtPuQT/D7Kx2S5w/Kl1GnMF9h2s= +github.com/go-json-experiment/json v0.0.0-20250626171732-1a886bd29d1b/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=