Skip to content

Commit 2b17d6e

Browse files
authored
Close old clients when reconnecting due to timeouts (#198)
1 parent da3ef84 commit 2b17d6e

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/chia-network/chia-exporter
33
go 1.21
44

55
require (
6-
github.com/chia-network/go-chia-libs v0.16.1
6+
github.com/chia-network/go-chia-libs v0.17.0
77
github.com/chia-network/go-modules v0.0.8
88
github.com/go-sql-driver/mysql v1.8.1
99
github.com/oschwald/maxminddb-golang v1.13.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
44
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
55
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
66
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
7-
github.com/chia-network/go-chia-libs v0.16.1 h1:oMOfvGodh7aCXX0xGFGU6NtmC+u/bz5BUptUbirH1jI=
8-
github.com/chia-network/go-chia-libs v0.16.1/go.mod h1:npTqaFSjTdMxE7hc0LOmWJmWGqcs+IERarK5fDxXk/I=
7+
github.com/chia-network/go-chia-libs v0.17.0 h1:MwzUxprIDyKNWTbac32BGc8G2p6FH8erbaug64xXl8c=
8+
github.com/chia-network/go-chia-libs v0.17.0/go.mod h1:npTqaFSjTdMxE7hc0LOmWJmWGqcs+IERarK5fDxXk/I=
99
github.com/chia-network/go-modules v0.0.8 h1:VATMxehRISOhaRwPo/GL735IKWW0G7sUYH2OmBofsfE=
1010
github.com/chia-network/go-modules v0.0.8/go.mod h1:OdvlWftyJc3+i3QYv5cfQsiQASL7Em7fJnzdmPmj07M=
1111
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=

internal/metrics/metrics.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ func NewMetrics(port uint16, logLevel log.Level, version string) (*Metrics, erro
138138
}
139139

140140
func (m *Metrics) setNewClient() error {
141+
if m.client != nil {
142+
err := m.client.Close()
143+
if err != nil {
144+
log.Error("Error closing old client", "error", err)
145+
}
146+
}
141147
client, err := rpc.NewClient(rpc.ConnectionModeWebsocket, rpc.WithAutoConfig(), rpc.WithBaseURL(&url.URL{
142148
Scheme: "wss",
143149
Host: viper.GetString("hostname"),
@@ -353,7 +359,6 @@ func (m *Metrics) OpenWebsocket() error {
353359
for {
354360
// If we don't get any events for 5 minutes, we'll reset the connection
355361
time.Sleep(10 * time.Second)
356-
357362
if m.lastReceive.Before(time.Now().Add(-5 * time.Minute)) {
358363
cancel()
359364
log.Info("Websocket connection seems down. Recreating...")
@@ -372,7 +377,7 @@ func (m *Metrics) OpenWebsocket() error {
372377

373378
// Got the new connection open, so stop the loop on the old connection
374379
// since we called this function again and a new loop was created
375-
break
380+
return
376381
}
377382
}
378383
}()
@@ -382,9 +387,7 @@ func (m *Metrics) OpenWebsocket() error {
382387

383388
// CloseWebsocket closes the websocket connection
384389
func (m *Metrics) CloseWebsocket() error {
385-
// @TODO reenable once fixed in the upstream dep
386-
//return m.client.DaemonService.CloseConnection()
387-
return nil
390+
return m.client.Close()
388391
}
389392

390393
// StartServer starts the metrics server

0 commit comments

Comments
 (0)