Compare commits

...

1 Commits

Author SHA1 Message Date
WJQSERVER
460b7514a9 2.0.3 (#37)
* 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>
2025-01-24 19:15:52 +08:00
8 changed files with 50 additions and 11 deletions

View File

@@ -1,5 +1,21 @@
# 更新日志
2.0.3
---
- RELEASE: v2.0.3正式版发布;
- CHANGE: 优化`HTTP Client`参数, 使其更符合本项目使用场景
25w07b
---
- PRE-RELEASE: 此版本是v2.0.3的预发布版本,请勿在生产环境中使用;
- CHANGE: 改进`HTTP Client`参数
25w07a
---
- PRE-RELEASE: 此版本是v2.0.3的预发布版本,请勿在生产环境中使用;
- CHANGE: 为`HTTP Client`增加复用, 对性能有所优化
- CHANGE: 优化`HTTP Client`参数, 使其更符合本项目使用场景
2.0.2
---
- RELEASE: v2.0.2正式版发布; 此版本是v2.0.1改进

View File

@@ -1 +1 @@
25w06b
25w07b

View File

@@ -111,7 +111,7 @@ whitelistFile = "/data/ghproxy/config/whitelist.json" # 白名单文件路径
[rateLimit]
enabled = false # 是否开启速率限制
rateMrthod = "total" # "ip" or "total" 速率限制方式
rateMethod = "total" # "ip" or "total" 速率限制方式
ratePerMinute = 180 # 每分钟限制请求数量
burst = 5 # 突发请求数量
```

View File

@@ -1 +1 @@
2.0.2
2.0.3

2
go.mod
View File

@@ -4,7 +4,7 @@ go 1.23.5
require (
github.com/BurntSushi/toml v1.4.0
github.com/WJQSERVER-STUDIO/go-utils/logger v1.1.0
github.com/WJQSERVER-STUDIO/go-utils/logger v1.1.1
github.com/gin-gonic/gin v1.10.0
golang.org/x/time v0.9.0
)

4
go.sum
View File

@@ -1,7 +1,7 @@
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/WJQSERVER-STUDIO/go-utils/logger v1.1.0 h1:OUrAOWb8xK0kxpWextJYUasmol+5KKqG2az52X2ae64=
github.com/WJQSERVER-STUDIO/go-utils/logger v1.1.0/go.mod h1:sAqHVYSucoUnycyHMAZc1fMH5dS2bQwgwo8NUiAIcyk=
github.com/WJQSERVER-STUDIO/go-utils/logger v1.1.1 h1:YS3q54SroxQpEM7c12ZKjLNAaSq++bNpxTujs9cTZ9c=
github.com/WJQSERVER-STUDIO/go-utils/logger v1.1.1/go.mod h1:oW884JCCPDU6c906LI0uKXndWLiRvjb9LkGYC2cqRO8=
github.com/bytedance/sonic v1.12.7 h1:CQU8pxOy9HToxhndH0Kx/S1qU/CuS9GnKYrGioDcU1Q=
github.com/bytedance/sonic v1.12.7/go.mod h1:tnbal4mxOMju17EGfknm2XyYcpyCnIROYOEYuemj13I=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=

View File

@@ -91,8 +91,8 @@ func setupRateLimit(cfg *config.Config) {
}
}
func initBufferSize() {
proxy.InitChunkedBufferSize(cfg.Server.BufferSize)
func InitChunkedReq() {
proxy.InitChunkedReq(cfg.Server.BufferSize)
}
func init() {
@@ -100,7 +100,7 @@ func init() {
flag.Parse()
loadConfig()
setupLogger(cfg)
initBufferSize()
InitChunkedReq()
loadlist(cfg)
setupRateLimit(cfg)

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