From d71b6fcc669efc6e840f02af31b37db2b034045b Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Tue, 12 Nov 2024 20:27:24 -0600 Subject: [PATCH 1/2] SIGHUP to trigger reconnect --- internal/metrics/metrics.go | 57 ++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 17 deletions(-) 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() From d8524282eafe3e1943820c8244b7c5fa3cc49b4a Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Tue, 12 Nov 2024 20:27:51 -0600 Subject: [PATCH 2/2] Go chia libs 0.17.1 --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 13eec73..bf18944 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/chia-network/chia-exporter go 1.21 require ( - github.com/chia-network/go-chia-libs v0.17.0 + github.com/chia-network/go-chia-libs v0.17.1 github.com/chia-network/go-modules v0.0.8 github.com/go-sql-driver/mysql v1.8.1 github.com/oschwald/maxminddb-golang v1.13.1 diff --git a/go.sum b/go.sum index 5b8069c..6a32950 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -github.com/chia-network/go-chia-libs v0.17.0 h1:MwzUxprIDyKNWTbac32BGc8G2p6FH8erbaug64xXl8c= -github.com/chia-network/go-chia-libs v0.17.0/go.mod h1:npTqaFSjTdMxE7hc0LOmWJmWGqcs+IERarK5fDxXk/I= +github.com/chia-network/go-chia-libs v0.17.1 h1:idFKs8C1DdrmK2AQDBI0SiLWovit1/GxPZ591Jxfau4= +github.com/chia-network/go-chia-libs v0.17.1/go.mod h1:npTqaFSjTdMxE7hc0LOmWJmWGqcs+IERarK5fDxXk/I= github.com/chia-network/go-modules v0.0.8 h1:VATMxehRISOhaRwPo/GL735IKWW0G7sUYH2OmBofsfE= github.com/chia-network/go-modules v0.0.8/go.mod h1:OdvlWftyJc3+i3QYv5cfQsiQASL7Em7fJnzdmPmj07M= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=