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() {
|
func InitChunkedReq() {
|
||||||
proxy.InitChunkedBufferSize(cfg.Server.BufferSize)
|
proxy.InitChunkedReq(cfg.Server.BufferSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
@ -100,7 +100,7 @@ func init() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
loadConfig()
|
loadConfig()
|
||||||
setupLogger(cfg)
|
setupLogger(cfg)
|
||||||
initBufferSize()
|
InitChunkedReq()
|
||||||
loadlist(cfg)
|
loadlist(cfg)
|
||||||
setupRateLimit(cfg)
|
setupRateLimit(cfg)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,24 @@ import (
|
||||||
"ghproxy/config"
|
"ghproxy/config"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
var chunkedBufferSize int
|
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 {
|
if cfgBufferSize == 0 {
|
||||||
chunkedBufferSize = 4096 // 默认缓冲区大小
|
chunkedBufferSize = 4096 // 默认缓冲区大小
|
||||||
} else {
|
} 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) {
|
func ChunkedProxyRequest(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)
|
||||||
|
|
@ -84,6 +103,7 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri
|
||||||
CopyResponseHeaders(resp, c, cfg)
|
CopyResponseHeaders(resp, c, cfg)
|
||||||
|
|
||||||
c.Status(resp.StatusCode)
|
c.Status(resp.StatusCode)
|
||||||
|
|
||||||
if err := chunkedCopyResponseBody(c, resp.Body); err != nil {
|
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)
|
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