@@ -54,9 +54,10 @@ type Options struct {
5454 Cookies []http.Cookie // HTTP Cookies
5555
5656 // ===== 客户端配置 / Client Configuration =====
57- Timeout time.Duration // 请求超时时间 / Request timeout
58- MaxConns int // 最大连接数(连接池大小)/ Maximum connections (connection pool size)
59- Verify bool // 是否验证TLS证书 / Whether to verify TLS certificates
57+ Timeout time.Duration // 请求超时时间(整个请求的总超时)/ Request timeout (total timeout for the whole request)
58+ ConnTimeout time.Duration // TCP 连接建立超时(拨号超时)/ TCP connection establishment timeout (dial timeout)
59+ MaxConns int // 最大连接数(连接池大小)/ Maximum connections (connection pool size)
60+ Verify bool // 是否验证TLS证书 / Whether to verify TLS certificates
6061
6162 // ===== 传输层配置 / Transport Layer Configuration =====
6263 Transport http.RoundTripper // 自定义传输层 / Custom transport
@@ -114,12 +115,13 @@ type Option func(*Options)
114115// - Options: 合并后的配置选项 / Merged configuration options
115116func newOptions (opts []Option , extends ... Option ) Options {
116117 opt := Options {
117- URL : "http://127.0.0.1:80" ,
118- RawQuery : make (url.Values ),
119- Header : make (http.Header ),
120- Timeout : 30 * time .Second ,
121- MaxConns : 100 ,
122- Proxy : http .ProxyFromEnvironment ,
118+ URL : "http://127.0.0.1:80" ,
119+ RawQuery : make (url.Values ),
120+ Header : make (http.Header ),
121+ Timeout : 30 * time .Second ,
122+ ConnTimeout : 10 * time .Second ,
123+ MaxConns : 100 ,
124+ Proxy : http .ProxyFromEnvironment ,
123125
124126 OnStart : func (s * http.Server ) { log .Printf ("http(s) serve %s" , s .Addr ) },
125127 OnShutdown : func (s * http.Server ) { log .Printf ("http shutdown" ) },
@@ -228,6 +230,12 @@ func Method(method string) Option {
228230// requests.Body("data"),
229231// )
230232//
233+ // ⚠️ Unix Socket 限制 / Unix Socket Limitation:
234+ // - Unix socket 路径只能在「会话级」(requests.New)设置,因为 Transport 在创建会话时即固化拨号逻辑。
235+ // - 在「请求级」传入 unix:// 不会改变已建好的 Transport,不会生效;如需切换 socket 请新建 Session。
236+ // - The Unix socket path can only be set at the "session level" (requests.New), because the Transport
237+ // fixes its dial logic when the session is created. Passing unix:// at the "request level" does NOT
238+ // take effect; create a new Session to switch sockets.
231239// 参数 / Parameters:
232240// - url: 目标 URL 地址 / Target URL address
233241//
@@ -361,6 +369,10 @@ func Body(body any) Option {
361369// - 发送大量数据时,减少网络传输量 / Reduce network transmission when sending large data
362370// - 服务器支持 gzip 解压时 / When server supports gzip decompression
363371//
372+ // 错误处理 / Error Handling:
373+ // - 压缩出错不再 panic,也不中断请求:仅记录日志,跳过压缩(不设置 body 与编码头)
374+ // - Compression errors no longer panic nor abort the request: just logged, compression is skipped (body and encoding headers left unset)
375+ //
364376// 示例 / Example:
365377//
366378// largeData := strings.Repeat("data", 10000)
@@ -548,6 +560,33 @@ func Timeout(timeout time.Duration) Option {
548560 }
549561}
550562
563+ // ConnTimeout 设置 TCP 连接建立(拨号)超时时间
564+ // ConnTimeout sets the TCP connection establishment (dial) timeout duration
565+ //
566+ // 参数 / Parameters:
567+ // - timeout: 拨号超时时间 / Dial timeout duration
568+ //
569+ // 说明 / Notes:
570+ // - 仅控制建立连接(TCP 握手 + DNS 解析)阶段的超时
571+ // - Only controls the timeout for the connection establishment phase (TCP handshake + DNS)
572+ // - 与 Timeout 不同:Timeout 控制整个请求的总耗时,ConnTimeout 仅控制拨号
573+ // - Differs from Timeout: Timeout controls the total request duration, ConnTimeout only the dial phase
574+ // - 默认值为 10 秒 / Default is 10 seconds
575+ // - 仅在会话级(New)配置生效,因 Transport 在创建时固化拨号逻辑
576+ // - Only effective at session level (New), since the Transport fixes dial logic at creation time
577+ //
578+ // 示例 / Example:
579+ //
580+ // sess := requests.New(
581+ // requests.URL("https://api.example.com"),
582+ // requests.ConnTimeout(3*time.Second), // 3秒拨号超时 / 3s dial timeout
583+ // )
584+ func ConnTimeout (timeout time.Duration ) Option {
585+ return func (o * Options ) {
586+ o .ConnTimeout = timeout
587+ }
588+ }
589+
551590// Verify 设置是否验证 TLS/SSL 证书
552591// Verify sets whether to verify TLS/SSL certificates
553592//
0 commit comments