Skip to content

Commit

Permalink
SIGHUP to trigger reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
cmmarslender committed Nov 13, 2024
1 parent 2b17d6e commit d71b6fc
Showing 1 changed file with 40 additions and 17 deletions.
57 changes: 40 additions & 17 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"fmt"
"net/http"
"net/url"
"os"
"os/signal"
"syscall"
"time"

"github.com/go-sql-driver/mysql"
Expand Down Expand Up @@ -356,35 +359,55 @@ 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
}
}
}
}()

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()
Expand Down

0 comments on commit d71b6fc

Please sign in to comment.