* Add support for reusing the Go net/http Client.

* Enhance HTTP Client parameters.

* 25w07a

* update deps

* Optimize HTTP Client parameters.

* 25w07b

* 2.0.3

* Update README.md

---------

Co-authored-by: 里見 灯花 <172008506+satomitouka@users.noreply.github.com>
This commit is contained in:
WJQSERVER 2025-01-24 19:15:52 +08:00 committed by GitHub
parent c90140a898
commit 460b7514a9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 50 additions and 11 deletions

View file

@ -6,13 +6,24 @@ import (
"ghproxy/config"
"io"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
var chunkedBufferSize int
func InitChunkedBufferSize(cfgBufferSize int) {
var (
client *http.Client
tr *http.Transport
)
func InitChunkedReq(cfgBufferSize int) {
initChunkedBufferSize(cfgBufferSize)
initChunkedHTTPClient()
}
func initChunkedBufferSize(cfgBufferSize int) {
if cfgBufferSize == 0 {
chunkedBufferSize = 4096 // 默认缓冲区大小
} else {
@ -20,12 +31,23 @@ func InitChunkedBufferSize(cfgBufferSize int) {
}
}
func initChunkedHTTPClient() {
tr = &http.Transport{
MaxIdleConns: 100,
MaxConnsPerHost: 60,
IdleConnTimeout: 20 * time.Second,
}
client = &http.Client{
Transport: tr,
}
}
func ChunkedProxyRequest(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)
@ -84,6 +106,7 @@ 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 {
logError("%s %s %s %s %s 响应复制错误: %v", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto, err)
}