Mem leak fix

This commit is contained in:
aceberg 2024-08-25 20:48:10 +07:00
parent 90e4c07a37
commit 17b6545eaa
4 changed files with 26 additions and 13 deletions

View file

@ -1,7 +1,7 @@
package web package web
import ( import (
// "log" "log/slog"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -49,6 +49,8 @@ func apiHostDel(c *gin.Context) {
db.Delete(appConfig.DBPath, "now", host.ID) db.Delete(appConfig.DBPath, "now", host.ID)
allHosts = db.Select(appConfig.DBPath, "now") allHosts = db.Select(appConfig.DBPath, "now")
slog.Info("Deleting from DB", "host", host)
c.IndentedJSON(http.StatusOK, "OK") c.IndentedJSON(http.StatusOK, "OK")
} }

View file

@ -47,7 +47,7 @@ func saveConfigHandler(c *gin.Context) {
conf.Write(appConfig) conf.Write(appConfig)
slog.Info("writing new config to " + appConfig.ConfPath) slog.Info("Writing new config to " + appConfig.ConfPath)
updateRoutines() // routines-upd.go updateRoutines() // routines-upd.go
@ -76,7 +76,7 @@ func saveInfluxHandler(c *gin.Context) {
conf.Write(appConfig) conf.Write(appConfig)
slog.Info("writing new config to " + appConfig.ConfPath) slog.Info("Writing new config to " + appConfig.ConfPath)
c.Redirect(http.StatusFound, "/config") c.Redirect(http.StatusFound, "/config")
} }

View file

@ -7,6 +7,7 @@ import (
) )
func updateRoutines() { func updateRoutines() {
slog.Debug("Restarting go routines")
close(quitScan) close(quitScan)
@ -26,15 +27,20 @@ func setLogLevel() {
switch appConfig.LogLevel { switch appConfig.LogLevel {
case "debug": case "debug":
slog.Info("Log level=DEBUG")
level = slog.LevelDebug level = slog.LevelDebug
case "info": case "info":
slog.Info("Log level=INFO")
level = slog.LevelInfo level = slog.LevelInfo
case "warn": case "warn":
slog.Info("Log level=WARN")
level = slog.LevelWarn level = slog.LevelWarn
case "error": case "error":
slog.Info("Log level=ERROR")
level = slog.LevelError level = slog.LevelError
default: default:
// invalid log level, handle error slog.Error("Invalid log level. Setting default level INFO")
slog.SetLogLoggerLevel(slog.LevelInfo)
} }
slog.SetLogLoggerLevel(level) slog.SetLogLoggerLevel(level)
} }

View file

@ -12,20 +12,25 @@ import (
"github.com/aceberg/WatchYourLAN/internal/notify" "github.com/aceberg/WatchYourLAN/internal/notify"
) )
var foundHostsMap map[string]models.Host
var aHost, fHost models.Host
var exists bool
func startScan(quit chan bool) { func startScan(quit chan bool) {
var lastDate time.Time var lastDate, nowDate, plusDate time.Time
var foundHosts []models.Host
for { for {
select { select {
case <-quit: case <-quit:
return return
default: default:
nowDate := time.Now() nowDate = time.Now()
plusDate := lastDate.Add(time.Duration(appConfig.Timeout) * time.Second) plusDate = lastDate.Add(time.Duration(appConfig.Timeout) * time.Second)
if nowDate.After(plusDate) { if nowDate.After(plusDate) {
foundHosts := arp.Scan(appConfig.Ifaces) foundHosts = arp.Scan(appConfig.Ifaces)
compareHosts(foundHosts) compareHosts(foundHosts)
allHosts = db.Select(appConfig.DBPath, "now") allHosts = db.Select(appConfig.DBPath, "now")
histHosts = db.Select(appConfig.DBPath, "history") histHosts = db.Select(appConfig.DBPath, "history")
@ -41,14 +46,14 @@ func startScan(quit chan bool) {
func compareHosts(foundHosts []models.Host) { func compareHosts(foundHosts []models.Host) {
// Make map of found hosts // Make map of found hosts
foundHostsMap := make(map[string]models.Host) foundHostsMap = make(map[string]models.Host)
for _, fHost := range foundHosts { for _, fHost = range foundHosts {
foundHostsMap[fHost.Mac] = fHost foundHostsMap[fHost.Mac] = fHost
} }
for _, aHost := range allHosts { for _, aHost = range allHosts {
fHost, exists := foundHostsMap[aHost.Mac] fHost, exists = foundHostsMap[aHost.Mac]
if exists { if exists {
aHost.Iface = fHost.Iface aHost.Iface = fHost.Iface
@ -70,7 +75,7 @@ func compareHosts(foundHosts []models.Host) {
} }
} }
for _, fHost := range foundHostsMap { for _, fHost = range foundHostsMap {
msg := fmt.Sprintf("Unknown host IP: '%s', MAC: '%s', Hw: '%s', Iface: '%s'", fHost.IP, fHost.Mac, fHost.Hw, fHost.Iface) msg := fmt.Sprintf("Unknown host IP: '%s', MAC: '%s', Hw: '%s', Iface: '%s'", fHost.IP, fHost.Mac, fHost.Hw, fHost.Iface)
slog.Warn(msg) slog.Warn(msg)