Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIGHUP to trigger reconnect #199

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
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
Loading