Optimize Gitreq

This commit is contained in:
WJQSERVER 2025-01-25 12:26:48 +08:00
parent 112c0e3320
commit 073be0c4d6
3 changed files with 32 additions and 14 deletions

View file

@ -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)

View file

@ -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

View file

@ -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