Skip to content

Commit

Permalink
fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
jagheterfredrik committed Mar 17, 2024
1 parent 3478be8 commit 18e3552
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
15 changes: 6 additions & 9 deletions app/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err
panic("Connection to MQTT lost")
}

func LaunchBridge(configPath string) {
func RunBridge(configPath string) {
c := LoadConfig(configPath)
w := wallbox.New()
w.RefreshData()
Expand Down Expand Up @@ -99,6 +99,9 @@ func LaunchBridge(configPath string) {
"added_energy": ratelimit.NewDeltaRateLimit(10, 50),
}

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)

for {
select {
case <-ticker.C:
Expand All @@ -116,18 +119,12 @@ func LaunchBridge(configPath string) {
published[key] = payload
}
}
case <-interrupt():
case <-interrupt:
fmt.Println("Interrupted. Exiting...")
token := client.Publish(availabilityTopic, 1, true, "offline")
token.Wait()
client.Disconnect(250)
os.Exit(0)
return
}
}
}

func interrupt() <-chan os.Signal {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
return interrupt
}
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"github.com/jagheterfredrik/wallbox-mqtt-bridge/app"
"os"

bridge "github.com/jagheterfredrik/wallbox-mqtt-bridge/app"
)

func main() {
Expand All @@ -12,8 +13,7 @@ func main() {
firstArgument := os.Args[1]
if firstArgument == "--config" {
bridge.RunTuiSetup()
os.Exit(0)
} else {
bridge.LaunchBridge(firstArgument)
bridge.RunBridge(firstArgument)
}
}

0 comments on commit 18e3552

Please sign in to comment.