Skip to content

Commit

Permalink
Close old clients when reconnecting due to timeouts (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmmarslender authored Nov 13, 2024
1 parent da3ef84 commit 2b17d6e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
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.16.1
github.com/chia-network/go-chia-libs v0.17.0
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.16.1 h1:oMOfvGodh7aCXX0xGFGU6NtmC+u/bz5BUptUbirH1jI=
github.com/chia-network/go-chia-libs v0.16.1/go.mod h1:npTqaFSjTdMxE7hc0LOmWJmWGqcs+IERarK5fDxXk/I=
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-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
13 changes: 8 additions & 5 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ func NewMetrics(port uint16, logLevel log.Level, version string) (*Metrics, erro
}

func (m *Metrics) setNewClient() error {
if m.client != nil {
err := m.client.Close()
if err != nil {
log.Error("Error closing old client", "error", err)
}
}
client, err := rpc.NewClient(rpc.ConnectionModeWebsocket, rpc.WithAutoConfig(), rpc.WithBaseURL(&url.URL{
Scheme: "wss",
Host: viper.GetString("hostname"),
Expand Down Expand Up @@ -353,7 +359,6 @@ func (m *Metrics) OpenWebsocket() error {
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...")
Expand All @@ -372,7 +377,7 @@ func (m *Metrics) OpenWebsocket() error {

// 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
break
return
}
}
}()
Expand All @@ -382,9 +387,7 @@ func (m *Metrics) OpenWebsocket() error {

// CloseWebsocket closes the websocket connection
func (m *Metrics) CloseWebsocket() error {
// @TODO reenable once fixed in the upstream dep
//return m.client.DaemonService.CloseConnection()
return nil
return m.client.Close()
}

// StartServer starts the metrics server
Expand Down

0 comments on commit 2b17d6e

Please sign in to comment.