88 "net/http"
99 "net/url"
1010 "os"
11+ "sync"
1112 "sync/atomic"
1213 "time"
1314
@@ -35,7 +36,9 @@ type Client struct {
3536 connectClient deployPBconnect.DeployServiceClient
3637 ctx context.Context
3738 accessKey string
38- lastDisconnectLogged atomic.Bool // 记录是否已打印断开连接日志
39+ lastDisconnectLogged atomic.Bool // 记录是否已打印断开连接日志
40+ systemInfo * system.SystemInfo // 缓存的系统信息
41+ systemInfoOnce sync.Once // 确保系统信息只获取一次
3942}
4043
4144func NewClient (ctx context.Context ) (* Client , error ) {
@@ -48,15 +51,27 @@ func NewClient(ctx context.Context) (*Client, error) {
4851 }
4952
5053 // 配置 HTTP 客户端
51- httpClient := & http.Client {}
54+ httpClient := & http.Client {
55+ Timeout : 30 * time .Second ,
56+ }
5257 if cfg .Server .Env == "local" {
5358 p := new (http.Protocols )
5459 p .SetUnencryptedHTTP2 (true )
5560 httpClient = & http.Client {
61+ Timeout : 30 * time .Second ,
5662 Transport : & http.Transport {
57- Protocols : p ,
63+ Protocols : p ,
64+ MaxIdleConns : 100 ,
65+ MaxIdleConnsPerHost : 10 ,
66+ IdleConnTimeout : 90 * time .Second ,
5867 },
5968 }
69+ } else {
70+ httpClient .Transport = & http.Transport {
71+ MaxIdleConns : 100 ,
72+ MaxIdleConnsPerHost : 10 ,
73+ IdleConnTimeout : 90 * time .Second ,
74+ }
6075 }
6176
6277 client := & Client {
@@ -75,6 +90,15 @@ func NewClient(ctx context.Context) (*Client, error) {
7590 return client , nil
7691}
7792
93+ // getSystemInfo 获取系统信息(带缓存)
94+ func (c * Client ) getSystemInfo () (* system.SystemInfo , error ) {
95+ var err error
96+ c .systemInfoOnce .Do (func () {
97+ c .systemInfo , err = system .GetSystemInfo ()
98+ })
99+ return c .systemInfo , err
100+ }
101+
78102// StartConnectNotify 启动连接通知
79103func (c * Client ) StartConnectNotify () {
80104 reconnectDelay := time .Second
@@ -110,10 +134,10 @@ func (c *Client) StartConnectNotify() {
110134 consecutiveFailures = 0
111135 reconnectDelay = time .Second
112136
113- // 获取系统信息
114- systemInfo , err := system . GetSystemInfo ()
137+ // 获取系统信息(使用缓存)
138+ systemInfo , err := c . getSystemInfo ()
115139 if err != nil {
116- logger .Error ("获取系统信息失败: %v " , err )
140+ logger .Error ("获取系统信息失败" , "error " , err )
117141 stream .CloseRequest ()
118142 time .Sleep (reconnectDelay )
119143 continue
@@ -138,31 +162,32 @@ func (c *Client) StartConnectNotify() {
138162
139163 // 注册客户端
140164 if err := stream .Send (registerReq ); err != nil {
141- // logger.Error("注册失败", "error", err)
142165 stream .CloseRequest ()
143166 time .Sleep (reconnectDelay )
144167 continue
145168 }
146169
147- // 处理消息流
148- err = c .handleNotifyStream (stream )
149-
150- // 流断开
151- if err != nil {
152- if errors .Is (err , context .Canceled ) || errors .Is (err , context .DeadlineExceeded ) {
153- return
154- }
155-
156- isConnected .Store (false )
157- c .lastDisconnectLogged .Store (true )
170+ // 流断开,先检查主 context 是否被取消(而不是检查错误类型)
171+ // 因为错误链中可能包含 context.Canceled,但实际是连接断开导致的
172+ select {
173+ case <- c .ctx .Done ():
174+ logger .Info ("主 context 已取消,退出连接循环" )
175+ return
176+ default :
177+ }
158178
159- // 等待后重连
160- time .Sleep (reconnectDelay )
161- reconnectDelay = min (reconnectDelay * 2 , maxReconnectDelay )
162- continue
179+ // 处理消息流
180+ if err := c .handleNotifyStream (stream ); err != nil {
181+ // logger.Error("连接断开", "error", err)
163182 }
164183
165- return
184+ // 标记断开连接
185+ isConnected .Store (false )
186+ c .lastDisconnectLogged .Store (true )
187+
188+ // 等待后重连
189+ time .Sleep (reconnectDelay )
190+ reconnectDelay = min (reconnectDelay * 2 , maxReconnectDelay )
166191 }
167192}
168193
@@ -197,7 +222,9 @@ func (c *Client) handleNotifyStream(stream *connect.BidiStreamForClientSimple[de
197222 if ! isConnected .Load () {
198223 isConnected .Store (true )
199224
225+ // 如果之前断开过连接,打印重连成功日志
200226 if c .lastDisconnectLogged .Load () {
227+ // logger.Info("重新连接成功")
201228 c .lastDisconnectLogged .Store (false )
202229 }
203230 }
@@ -249,7 +276,7 @@ func (c *Client) sendHeartbeat(ctx context.Context, stream *connect.BidiStreamFo
249276 Version : config .Version ,
250277 })
251278 if err != nil {
252- logger .Error ("发送心跳失败: %v " , err )
279+ // logger.Error("发送心跳失败", "error ", err)
253280 return
254281 }
255282 }
0 commit comments