From 8e1bcbe99022b4986f1d84c995e5f34a89f38034 Mon Sep 17 00:00:00 2001 From: qjfoidnh Date: Fri, 17 Oct 2025 02:57:00 +0800 Subject: [PATCH] debug --- .../pcsdownload/download_task_unit.go | 6 +-- requester/http_client.go | 49 ++++++++++++++++--- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/internal/pcsfunctions/pcsdownload/download_task_unit.go b/internal/pcsfunctions/pcsdownload/download_task_unit.go index da36381..ee0380f 100644 --- a/internal/pcsfunctions/pcsdownload/download_task_unit.go +++ b/internal/pcsfunctions/pcsdownload/download_task_unit.go @@ -233,9 +233,9 @@ func (dtu *DownloadTaskUnit) download(downloadURL string, client *requester.HTTP // panHTTPClient 获取包含特定User-Agent的HTTPClient func (dtu *DownloadTaskUnit) panHTTPClient() *requester.HTTPClient { - if client == nil { - client = pcsconfig.Config.PanHTTPClient() - } + //if client == nil { + client := pcsconfig.Config.PanHTTPClient() + //} client.CheckRedirect = func(req *http.Request, via []*http.Request) error { // 去掉 Referer if !pcsconfig.Config.EnableHTTPS { diff --git a/requester/http_client.go b/requester/http_client.go index 88dfc00..4a2f20e 100644 --- a/requester/http_client.go +++ b/requester/http_client.go @@ -1,7 +1,10 @@ package requester import ( + "context" "crypto/tls" + "fmt" + "net" "net/http" "net/http/cookiejar" "time" @@ -15,12 +18,44 @@ type HTTPClient struct { UserAgent string } +type traceConn struct { + net.Conn + id string +} + +func (c traceConn) Close() error { + fmt.Printf("CONN[%s] Close() called, going to FIN -> TIME_WAIT", c.id) + return c.Conn.Close() +} + +// 拨号时包装 +func dialTrace(ctx context.Context, network, addr string) (net.Conn, error) { + d := net.Dialer{} + raw, err := d.DialContext(ctx, network, addr) + if err != nil { + return nil, err + } + id := fmt.Sprintf("%s->%s@%d", raw.LocalAddr(), raw.RemoteAddr(), time.Now().UnixNano()) + fmt.Printf("CONN[%s] DialContext success", id) + return traceConn{Conn: raw, id: id}, nil +} + // NewHTTPClient 返回 HTTPClient 的指针, // 预设了一些配置 func NewHTTPClient() *HTTPClient { + fmt.Println("new one") h := &HTTPClient{ Client: http.Client{ Timeout: 50 * time.Second, + Transport: &http.Transport{ + DialContext: dialTrace, + MaxIdleConns: 20, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + // 关键:打开状态回调 + DisableKeepAlives: false, + }, }, UserAgent: UserAgent, } @@ -81,13 +116,13 @@ func (h *HTTPClient) ResetCookiejar() { func (h *HTTPClient) SetHTTPSecure(b bool) { h.https = b h.lazyInit() -// if b { -// h.transport.TLSClientConfig = nil -// } else { - h.transport.TLSClientConfig = &tls.Config{ - InsecureSkipVerify: true, - } -// } + // if b { + // h.transport.TLSClientConfig = nil + // } else { + h.transport.TLSClientConfig = &tls.Config{ + InsecureSkipVerify: true, + } + // } } // SetKeepAlive 设置 Keep-Alive