Skip to content

Commit 3cd0cc0

Browse files
authored
improve timeout logic (#176)
1 parent 36f40de commit 3cd0cc0

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

cmd/agent/main.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,7 @@ func doTask(task *pb.Task) *pb.TaskResult {
485485
func reportStateDaemon(stateClient pb.NezhaService_ReportSystemStateClient, cancel context.CancelFunc) {
486486
var err error
487487
for {
488-
_, err = doWithTimeout(func() (*int, error) {
489-
lastReportHostInfo, lastReportIPInfo, err = reportState(stateClient, lastReportHostInfo, lastReportIPInfo)
490-
return nil, err
491-
}, time.Second*10)
488+
lastReportHostInfo, lastReportIPInfo, err = reportState(stateClient, lastReportHostInfo, lastReportIPInfo)
492489
if err != nil {
493490
printf("reportStateDaemon exit: %v", err)
494491
cancel()
@@ -504,10 +501,12 @@ func reportState(statClient pb.NezhaService_ReportSystemStateClient, host, ip ti
504501
}
505502
if initialized {
506503
monitor.TrackNetworkSpeed()
507-
if err := statClient.Send(monitor.GetState(agentConfig.SkipConnectionCount, agentConfig.SkipProcsCount).PB()); err != nil {
504+
if _, err := doWithTimeout(func() (*pb.Receipt, error) {
505+
return nil, statClient.Send(monitor.GetState(agentConfig.SkipConnectionCount, agentConfig.SkipProcsCount).PB())
506+
}, time.Second*10); err != nil {
508507
return host, ip, err
509508
}
510-
_, err := statClient.Recv()
509+
_, err := doWithTimeout(statClient.Recv, time.Second*10)
511510
if err != nil {
512511
return host, ip, err
513512
}
@@ -534,7 +533,9 @@ func reportHost() bool {
534533
}
535534
defer hostStatus.Store(false)
536535
if client != nil && initialized {
537-
receipt, err := client.ReportSystemInfo2(context.Background(), monitor.GetHost().PB())
536+
receipt, err := doWithTimeout(func() (*pb.Uint64Receipt, error) {
537+
return client.ReportSystemInfo2(context.Background(), monitor.GetHost().PB())
538+
}, time.Second*10)
538539
if err != nil {
539540
printf("ReportSystemInfo2 error: %v", err)
540541
return false
@@ -563,7 +564,9 @@ func reportGeoIP(use6, forceUpdate bool) bool {
563564
return true
564565
}
565566

566-
geoip, err := client.ReportGeoIP(context.Background(), pbg)
567+
geoip, err := doWithTimeout(func() (*pb.GeoIP, error) {
568+
return client.ReportGeoIP(context.Background(), pbg)
569+
}, time.Second*10)
567570
if err != nil {
568571
return false
569572
}

0 commit comments

Comments
 (0)