Skip to content

Commit

Permalink
Reset all counters to zero on disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
NorbertHeusser committed Nov 9, 2022
1 parent e097dec commit 7b6e487
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ func runServer(c *cli.Context) {
}
}
opts.OnConnectionLost = func(client mqtt.Client, err error) {
log.Printf("Error: Connection to %s lost: %s", c.String("endpoint"), err)
log.Printf("Error: Connection to %s lost: %s, resetting counters", c.String("endpoint"), err)
resetMetrics()
}
client := mqtt.NewClient(opts)

Expand All @@ -194,6 +195,19 @@ func runServer(c *cli.Context) {
fatalfOnError(err, "Failed to bind on %s: ", c.GlobalString("bind-address"))
}

func resetMetrics() {
for topic, _ := range counterMetrics {
if counterMetrics[topic] != nil {
counterMetrics[topic].Set(0)
}
}
for topic, _ := range gaugeMetrics {
if counterMetrics[topic] != nil {
counterMetrics[topic].Set(0)
}
}
}

// $SYS/broker/bytes/received
func processUpdate(topic, payload string) {
//log.Printf("Got broker update with topic %s and data %s", topic, payload)
Expand Down

0 comments on commit 7b6e487

Please sign in to comment.