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

View file

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

View file

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

View file

@ -12,20 +12,25 @@ import (
"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) {
var lastDate time.Time
var lastDate, nowDate, plusDate time.Time
var foundHosts []models.Host
for {
select {
case <-quit:
return
default:
nowDate := time.Now()
plusDate := lastDate.Add(time.Duration(appConfig.Timeout) * time.Second)
nowDate = time.Now()
plusDate = lastDate.Add(time.Duration(appConfig.Timeout) * time.Second)
if nowDate.After(plusDate) {
foundHosts := arp.Scan(appConfig.Ifaces)
foundHosts = arp.Scan(appConfig.Ifaces)
compareHosts(foundHosts)
allHosts = db.Select(appConfig.DBPath, "now")
histHosts = db.Select(appConfig.DBPath, "history")
@ -41,14 +46,14 @@ func startScan(quit chan bool) {
func compareHosts(foundHosts []models.Host) {
// Make map of found hosts
foundHostsMap := make(map[string]models.Host)
for _, fHost := range foundHosts {
foundHostsMap = make(map[string]models.Host)
for _, fHost = range foundHosts {
foundHostsMap[fHost.Mac] = fHost
}
for _, aHost := range allHosts {
for _, aHost = range allHosts {
fHost, exists := foundHostsMap[aHost.Mac]
fHost, exists = foundHostsMap[aHost.Mac]
if exists {
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)
slog.Warn(msg)