Skip to content

Commit bfe2a1b

Browse files
committed
refactor(client): 移除心跳机制并使用HTTP/2保活
移除客户端心跳机制,改用HTTP/2的ReadIdleTimeout和PingTimeout实现连接保活 更新相关依赖版本
1 parent f419c0e commit bfe2a1b

File tree

4 files changed

+16
-32
lines changed

4 files changed

+16
-32
lines changed

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
connectrpc.com/connect v1.19.0
77
github.com/spf13/cobra v1.10.1
88
github.com/spf13/viper v1.21.0
9+
golang.org/x/net v0.46.0
910
google.golang.org/protobuf v1.36.9
1011
)
1112

@@ -20,7 +21,7 @@ require (
2021
github.com/spf13/pflag v1.0.10 // indirect
2122
github.com/subosito/gotenv v1.6.0 // indirect
2223
go.yaml.in/yaml/v3 v3.0.4 // indirect
23-
golang.org/x/sys v0.36.0 // indirect
24-
golang.org/x/text v0.29.0 // indirect
24+
golang.org/x/sys v0.37.0 // indirect
25+
golang.org/x/text v0.30.0 // indirect
2526
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
2627
)

go.sum

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8
4343
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
4444
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
4545
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
46-
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
47-
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
48-
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
49-
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
46+
golang.org/x/net v0.46.0 h1:giFlY12I07fugqwPuWJi68oOnpfqFnJIJzaIIm2JVV4=
47+
golang.org/x/net v0.46.0/go.mod h1:Q9BGdFy1y4nkUwiLvT5qtyhAnEHgnQ/zd8PfU6nc210=
48+
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
49+
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
50+
golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=
51+
golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM=
5052
google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw=
5153
google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
5254
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

internal/client/client.go

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import (
1818
"github.com/orange-juzipi/cert-deploy/pb/deployPB"
1919
"github.com/orange-juzipi/cert-deploy/pb/deployPB/deployPBconnect"
2020
"github.com/orange-juzipi/cert-deploy/pkg/logger"
21+
"golang.org/x/net/http2"
2122
)
2223

2324
const (
24-
heartbeatInterval = 5 * time.Second
2525
downloadTimeout = 5 * time.Minute
2626
maxReconnectDelay = 5 * time.Minute
2727
)
@@ -50,11 +50,13 @@ func NewClient(ctx context.Context) (*Client, error) {
5050
}
5151

5252
httpClient := &http.Client{
53-
Transport: &http.Transport{
54-
TLSHandshakeTimeout: 10 * time.Second,
55-
ExpectContinueTimeout: 1 * time.Second,
56-
IdleConnTimeout: 90 * time.Second,
53+
Transport: &http2.Transport{
54+
// 若连接在 30s 内无任何帧往来,自动发送 HTTP/2 PING
55+
ReadIdleTimeout: 30 * time.Second,
56+
// PING 发出后 15s 内没响应就视为断开
57+
PingTimeout: 10 * time.Second,
5758
},
59+
Timeout: 0,
5860
}
5961

6062
client := &Client{
@@ -203,24 +205,6 @@ func (c *Client) handleNotifyStream(stream *connect.ServerStreamForClient[deploy
203205
}
204206
}
205207

206-
// StartHeartbeat 启动心跳
207-
func (c *Client) StartHeartbeat() {
208-
ticker := time.NewTicker(heartbeatInterval)
209-
defer ticker.Stop()
210-
211-
for {
212-
select {
213-
case <-c.ctx.Done():
214-
logger.Info("停止发送心跳")
215-
return
216-
case <-ticker.C:
217-
if err := c.register(); err != nil {
218-
continue
219-
}
220-
}
221-
}
222-
}
223-
224208
// deployCertificate 部署证书
225209
func (c *Client) deployCertificate(domain, downloadURL string) {
226210
deployer := NewCertDeployer(c)

internal/scheduler/scheduler.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ func Start(ctx context.Context) {
3535
logger.Fatal("创建调度器失败", "error", err)
3636
}
3737

38-
// 启动心跳
39-
go scheduler.client.StartHeartbeat()
40-
4138
// 等待上下文取消
4239
<-ctx.Done()
4340

0 commit comments

Comments
 (0)