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 {