From 18000b94fbd17c693fd43a59b7704557f99c8f6e Mon Sep 17 00:00:00 2001 From: WJQSERVER Date: Sat, 18 Jan 2025 08:49:19 +0800 Subject: [PATCH] add bufferSize config --- config/config.go | 11 ++++++----- config/config.toml | 1 + proxy/chunkreq.go | 6 +++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/config/config.go b/config/config.go index ea325a4..0747d62 100644 --- a/config/config.go +++ b/config/config.go @@ -16,11 +16,12 @@ type Config struct { } type ServerConfig struct { - Port int `toml:"port"` - Host string `toml:"host"` - SizeLimit int `toml:"sizeLimit"` - EnableH2C string `toml:"enableH2C"` - Debug bool `toml:"debug"` + Port int `toml:"port"` + Host string `toml:"host"` + SizeLimit int `toml:"sizeLimit"` + EnableH2C string `toml:"enableH2C"` + BufferSize int `toml:"bufferSize"` + Debug bool `toml:"debug"` } type PagesConfig struct { diff --git a/config/config.toml b/config/config.toml index 5aff583..5e648ae 100644 --- a/config/config.toml +++ b/config/config.toml @@ -3,6 +3,7 @@ host = "127.0.0.1" port = 8080 sizeLimit = 125 # MB enableH2C = "on" # "on" or "off" +bufferSize = 4096 # Bytes debug = false [pages] diff --git a/proxy/chunkreq.go b/proxy/chunkreq.go index 4bc687c..7555b00 100644 --- a/proxy/chunkreq.go +++ b/proxy/chunkreq.go @@ -72,14 +72,14 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri CopyResponseHeaders(resp, c, cfg) c.Status(resp.StatusCode) - if err := chunkedCopyResponseBody(c, resp.Body); err != nil { + if err := chunkedCopyResponseBody(c, cfg, resp.Body); err != nil { logError("%s %s %s %s %s 响应复制错误: %v", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto, err) } } // 复制响应体 -func chunkedCopyResponseBody(c *gin.Context, respBody io.Reader) error { - buf := make([]byte, 4096) +func chunkedCopyResponseBody(c *gin.Context, cfg *config.Config, respBody io.Reader) error { + buf := make([]byte, cfg.Server.BufferSize) for { n, err := respBody.Read(buf) if n > 0 {