From b497b294020da4fe2012a6b37f96b49cd13f15b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A5=B6=E7=88=B8?= Date: Sun, 9 Feb 2020 22:04:59 +0800 Subject: [PATCH] fix: close connection when reconnect --- cmd/agent/main.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 6cb2562..79f031c 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -59,10 +59,6 @@ func run(cmd *cobra.Command, args []string) { ClientID: clientID, ClientSecret: clientSecret, } - retry := func() { - time.Sleep(delayWhenError) - log.Println("Try to reconnect ...") - } // 上报服务器信息 go reportState() @@ -71,6 +67,15 @@ func run(cmd *cobra.Command, args []string) { var conn *grpc.ClientConn var hc pb.NezhaService_HeartbeatClient + retry := func() { + log.Println("Error to close connection ...") + if conn != nil { + conn.Close() + } + time.Sleep(delayWhenError) + log.Println("Try to reconnect ...") + } + for { conn, err = grpc.Dial(server, grpc.WithInsecure(), grpc.WithPerRPCCredentials(&auth)) if err != nil {