From 073be0c4d6098bce327a833bdf1a28b0e6e11b84 Mon Sep 17 00:00:00 2001 From: WJQSERVER Date: Sat, 25 Jan 2025 12:26:48 +0800 Subject: [PATCH] Optimize Gitreq --- main.go | 6 +++--- proxy/chunkreq.go | 17 +++++++++-------- proxy/gitreq.go | 23 ++++++++++++++++++++--- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 4cd1968..d7e5c6d 100644 --- a/main.go +++ b/main.go @@ -91,8 +91,8 @@ func setupRateLimit(cfg *config.Config) { } } -func InitChunkedReq() { - proxy.InitChunkedReq(cfg.Server.BufferSize) +func InitReq() { + proxy.InitReq(cfg.Server.BufferSize) } func init() { @@ -100,7 +100,7 @@ func init() { flag.Parse() loadConfig() setupLogger(cfg) - InitChunkedReq() + InitReq() loadlist(cfg) setupRateLimit(cfg) diff --git a/proxy/chunkreq.go b/proxy/chunkreq.go index e94f5ec..02893b9 100644 --- a/proxy/chunkreq.go +++ b/proxy/chunkreq.go @@ -14,13 +14,14 @@ import ( var chunkedBufferSize int var ( - client *http.Client - tr *http.Transport + cclient *http.Client + ctr *http.Transport ) -func InitChunkedReq(cfgBufferSize int) { +func InitReq(cfgBufferSize int) { initChunkedBufferSize(cfgBufferSize) initChunkedHTTPClient() + initGitHTTPClient() } func initChunkedBufferSize(cfgBufferSize int) { @@ -32,13 +33,13 @@ func initChunkedBufferSize(cfgBufferSize int) { } func initChunkedHTTPClient() { - tr = &http.Transport{ + ctr = &http.Transport{ MaxIdleConns: 100, MaxConnsPerHost: 60, IdleConnTimeout: 20 * time.Second, } - client = &http.Client{ - Transport: tr, + cclient = &http.Client{ + Transport: ctr, } } @@ -59,7 +60,7 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri removeWSHeader(headReq) // 删除Conection Upgrade头, 避免与HTTP/2冲突(检查是否存在Upgrade头) AuthPassThrough(c, cfg, headReq) - headResp, err := client.Do(headReq) + headResp, err := cclient.Do(headReq) if err != nil { HandleError(c, fmt.Sprintf("Failed to send request: %v", err)) return @@ -91,7 +92,7 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri removeWSHeader(req) // 删除Conection Upgrade头, 避免与HTTP/2冲突(检查是否存在Upgrade头) AuthPassThrough(c, cfg, req) - resp, err := client.Do(req) + resp, err := cclient.Do(req) if err != nil { HandleError(c, fmt.Sprintf("发送请求失败: %v", err)) return diff --git a/proxy/gitreq.go b/proxy/gitreq.go index a429c6c..424951e 100644 --- a/proxy/gitreq.go +++ b/proxy/gitreq.go @@ -6,16 +6,33 @@ import ( "ghproxy/config" "io" "net/http" + "time" "github.com/gin-gonic/gin" ) +var ( + gclient *http.Client + gtr *http.Transport +) + +func initGitHTTPClient() { + gtr = &http.Transport{ + MaxIdleConns: 30, + MaxConnsPerHost: 30, + IdleConnTimeout: 30 * time.Second, + } + gclient = &http.Client{ + Transport: gtr, + } +} + func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode string) { method := c.Request.Method logInfo("%s %s %s %s %s", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto) // 创建HTTP客户端 - client := &http.Client{} + //client := &http.Client{} // 发送HEAD请求, 预获取Content-Length headReq, err := http.NewRequest("HEAD", u, nil) @@ -26,7 +43,7 @@ func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode s setRequestHeaders(c, headReq) AuthPassThrough(c, cfg, headReq) - headResp, err := client.Do(headReq) + headResp, err := gclient.Do(headReq) if err != nil { HandleError(c, fmt.Sprintf("Failed to send request: %v", err)) return @@ -55,7 +72,7 @@ func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode s setRequestHeaders(c, req) AuthPassThrough(c, cfg, req) - resp, err := client.Do(req) + resp, err := gclient.Do(req) if err != nil { HandleError(c, fmt.Sprintf("Failed to send request: %v", err)) return