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() { func InitReq() {
proxy.InitChunkedReq(cfg.Server.BufferSize) proxy.InitReq(cfg.Server.BufferSize)
} }
func init() { func init() {
@ -100,7 +100,7 @@ func init() {
flag.Parse() flag.Parse()
loadConfig() loadConfig()
setupLogger(cfg) setupLogger(cfg)
InitChunkedReq() InitReq()
loadlist(cfg) loadlist(cfg)
setupRateLimit(cfg) setupRateLimit(cfg)

View file

@ -14,13 +14,14 @@ import (
var chunkedBufferSize int var chunkedBufferSize int
var ( var (
client *http.Client cclient *http.Client
tr *http.Transport ctr *http.Transport
) )
func InitChunkedReq(cfgBufferSize int) { func InitReq(cfgBufferSize int) {
initChunkedBufferSize(cfgBufferSize) initChunkedBufferSize(cfgBufferSize)
initChunkedHTTPClient() initChunkedHTTPClient()
initGitHTTPClient()
} }
func initChunkedBufferSize(cfgBufferSize int) { func initChunkedBufferSize(cfgBufferSize int) {
@ -32,13 +33,13 @@ func initChunkedBufferSize(cfgBufferSize int) {
} }
func initChunkedHTTPClient() { func initChunkedHTTPClient() {
tr = &http.Transport{ ctr = &http.Transport{
MaxIdleConns: 100, MaxIdleConns: 100,
MaxConnsPerHost: 60, MaxConnsPerHost: 60,
IdleConnTimeout: 20 * time.Second, IdleConnTimeout: 20 * time.Second,
} }
client = &http.Client{ cclient = &http.Client{
Transport: tr, 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头) removeWSHeader(headReq) // 删除Conection Upgrade头, 避免与HTTP/2冲突(检查是否存在Upgrade头)
AuthPassThrough(c, cfg, headReq) AuthPassThrough(c, cfg, headReq)
headResp, err := client.Do(headReq) headResp, err := cclient.Do(headReq)
if err != nil { if err != nil {
HandleError(c, fmt.Sprintf("Failed to send request: %v", err)) HandleError(c, fmt.Sprintf("Failed to send request: %v", err))
return return
@ -91,7 +92,7 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri
removeWSHeader(req) // 删除Conection Upgrade头, 避免与HTTP/2冲突(检查是否存在Upgrade头) removeWSHeader(req) // 删除Conection Upgrade头, 避免与HTTP/2冲突(检查是否存在Upgrade头)
AuthPassThrough(c, cfg, req) AuthPassThrough(c, cfg, req)
resp, err := client.Do(req) resp, err := cclient.Do(req)
if err != nil { if err != nil {
HandleError(c, fmt.Sprintf("发送请求失败: %v", err)) HandleError(c, fmt.Sprintf("发送请求失败: %v", err))
return return

View file

@ -6,16 +6,33 @@ import (
"ghproxy/config" "ghproxy/config"
"io" "io"
"net/http" "net/http"
"time"
"github.com/gin-gonic/gin" "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) { func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode string) {
method := c.Request.Method method := c.Request.Method
logInfo("%s %s %s %s %s", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto) logInfo("%s %s %s %s %s", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto)
// 创建HTTP客户端 // 创建HTTP客户端
client := &http.Client{} //client := &http.Client{}
// 发送HEAD请求, 预获取Content-Length // 发送HEAD请求, 预获取Content-Length
headReq, err := http.NewRequest("HEAD", u, nil) 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) setRequestHeaders(c, headReq)
AuthPassThrough(c, cfg, headReq) AuthPassThrough(c, cfg, headReq)
headResp, err := client.Do(headReq) headResp, err := gclient.Do(headReq)
if err != nil { if err != nil {
HandleError(c, fmt.Sprintf("Failed to send request: %v", err)) HandleError(c, fmt.Sprintf("Failed to send request: %v", err))
return return
@ -55,7 +72,7 @@ func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode s
setRequestHeaders(c, req) setRequestHeaders(c, req)
AuthPassThrough(c, cfg, req) AuthPassThrough(c, cfg, req)
resp, err := client.Do(req) resp, err := gclient.Do(req)
if err != nil { if err != nil {
HandleError(c, fmt.Sprintf("Failed to send request: %v", err)) HandleError(c, fmt.Sprintf("Failed to send request: %v", err))
return return