From 444302056d00c274dcb79bfe694afe8aa7f4c545 Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Wed, 11 Sep 2024 21:19:24 -0500 Subject: [PATCH] =?UTF-8?q?If=20the=20websocket=20fails=20to=20connect=20i?= =?UTF-8?q?nitially,=20create=20a=20whole=20new=20clien=E2=80=A6=20(#42)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * If the websocket fails to connect initially, create a whole new client (so certs are reloaded) in case we have the wrong certs (this solves an edge case in operator where we grab certs between inits) * Fix lint * Move the go dnscheck outside the loop --- cmd/serve.go | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/cmd/serve.go b/cmd/serve.go index 9d133b4..010426c 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -20,13 +20,26 @@ var serveCmd = &cobra.Command{ log.Fatalf("Error parsing log level: %s\n", err.Error()) } - h, err := healthcheck.NewHealthcheck(uint16(viper.GetInt("healthcheck-port")), level) - if err != nil { - log.Fatalln(err.Error()) + var h *healthcheck.Healthcheck + + // Loop until we get a connection or cancel + // It just retries every 5 seconds to connect to the RPC server until it succeeds or the app is stopped + for { + h, err = healthcheck.NewHealthcheck(uint16(viper.GetInt("healthcheck-port")), level) + if err != nil { + log.Fatalln(err.Error()) + } + + err = startWebsocket(h) + if err != nil { + log.Printf("error starting websocket. Creating new client and trying again in 5 seconds: %s\n", err.Error()) + time.Sleep(5 * time.Second) + continue + } + + break } - // Run this in the background, so the metrics healthz endpoint can come up while waiting for Chia - go startWebsocket(h) go h.DNSCheckLoop() log.Fatalln(h.StartServer()) @@ -37,17 +50,10 @@ func init() { rootCmd.AddCommand(serveCmd) } -func startWebsocket(h *healthcheck.Healthcheck) { - // Loop until we get a connection or cancel - // This enables starting the healthcheck app even if the chia RPC service is not up/responding - // It just retries every 5 seconds to connect to the RPC server until it succeeds or the app is stopped - for { - err := h.OpenWebsocket() - if err != nil { - log.Println(err.Error()) - time.Sleep(5 * time.Second) - continue - } - break +func startWebsocket(h *healthcheck.Healthcheck) error { + err := h.OpenWebsocket() + if err != nil { + return err } + return nil }