mirror of
https://github.com/WJQSERVER-STUDIO/ghproxy.git
synced 2026-02-03 08:11:11 +08:00
Add support for reusing the Go net/http Client.
This commit is contained in:
parent
c90140a898
commit
a84e10bb84
2 changed files with 25 additions and 5 deletions
6
main.go
6
main.go
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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,20 @@ func InitChunkedBufferSize(cfgBufferSize int) {
|
|||
}
|
||||
}
|
||||
|
||||
func initChunkedHTTPClient() {
|
||||
tr = &http.Transport{
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 30 * 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 +103,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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue