diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index ac8916b..d3795c4 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -7,6 +7,9 @@ import ( "fmt" "net/http" "net/url" + "os" + "os/signal" + "syscall" "time" "github.com/go-sql-driver/mysql" @@ -356,28 +359,30 @@ func (m *Metrics) OpenWebsocket() error { m.lastReceive = time.Now() go func() { - for { - // If we don't get any events for 5 minutes, we'll reset the connection - time.Sleep(10 * time.Second) - if m.lastReceive.Before(time.Now().Add(-5 * time.Minute)) { - cancel() - log.Info("Websocket connection seems down. Recreating...") - m.disconnectHandler() - err := m.setNewClient() - if err != nil { - log.Errorf("Error creating new client: %s", err.Error()) - continue - } + sighup := make(chan os.Signal, 1) + signal.Notify(sighup, syscall.SIGHUP) - err = m.OpenWebsocket() + for { + select { + case <-sighup: + log.Info("Got SIGHUP. Recreating websocket connection...") + err := m.doTimeoutReconnect(cancel) if err != nil { - log.Errorf("Error opening websocket on new client: %s", err.Error()) + log.Errorf(err.Error()) continue } - - // Got the new connection open, so stop the loop on the old connection - // since we called this function again and a new loop was created return + case <-time.After(10 * time.Second): + // If we don't get any events for 5 minutes, we'll reset the connection + if m.lastReceive.Before(time.Now().Add(-5 * time.Minute)) { + log.Info("Websocket connection seems down. Recreating...") + err := m.doTimeoutReconnect(cancel) + if err != nil { + log.Errorf(err.Error()) + continue + } + return + } } } }() @@ -385,6 +390,24 @@ func (m *Metrics) OpenWebsocket() error { return nil } +func (m *Metrics) doTimeoutReconnect(cancel context.CancelFunc) error { + cancel() + m.disconnectHandler() + err := m.setNewClient() + if err != nil { + return fmt.Errorf("error creating new client: %w", err) + } + + err = m.OpenWebsocket() + if err != nil { + return fmt.Errorf("error opening websocket on new client: %w", err) + } + + // Got the new connection open, so stop the loop on the old connection + // since we called this function again and a new loop was created + return nil +} + // CloseWebsocket closes the websocket connection func (m *Metrics) CloseWebsocket() error { return m.client.Close()