55 "context"
66 "encoding/json"
77 "fmt"
8+ "net"
89 "net/http"
910 "time"
1011
@@ -16,6 +17,9 @@ import (
1617 "github.com/tencent-connect/botgo/version"
1718)
1819
20+ // MaxIdleConns 默认指定空闲连接池大小
21+ const MaxIdleConns = 3000
22+
1923type openAPI struct {
2024 token * token.Token
2125 timeout time.Duration
@@ -68,6 +72,7 @@ func (o *openAPI) Transport(ctx context.Context, method, url string, body interf
6872// 初始化 client
6973func (o * openAPI ) setupClient () {
7074 o .restyClient = resty .New ().
75+ SetTransport (createTransport (nil , MaxIdleConns )). // 自定义 transport
7176 SetLogger (log .DefaultLogger ).
7277 SetDebug (o .debug ).
7378 SetTimeout (o .timeout ).
@@ -119,3 +124,24 @@ func respInfo(resp *resty.Response) string {
119124 string (resp .Body ()),
120125 )
121126}
127+
128+ func createTransport (localAddr net.Addr , idleConns int ) * http.Transport {
129+ dialer := & net.Dialer {
130+ Timeout : 60 * time .Second ,
131+ KeepAlive : 60 * time .Second ,
132+ }
133+ if localAddr != nil {
134+ dialer .LocalAddr = localAddr
135+ }
136+ return & http.Transport {
137+ Proxy : http .ProxyFromEnvironment ,
138+ DialContext : dialer .DialContext ,
139+ ForceAttemptHTTP2 : true ,
140+ MaxIdleConns : idleConns ,
141+ IdleConnTimeout : 90 * time .Second ,
142+ TLSHandshakeTimeout : 10 * time .Second ,
143+ ExpectContinueTimeout : 1 * time .Second ,
144+ MaxIdleConnsPerHost : idleConns ,
145+ MaxConnsPerHost : idleConns ,
146+ }
147+ }
0 commit comments