Skip to content

Commit

Permalink
Mem leak fix
Browse files Browse the repository at this point in the history
  • Loading branch information
aceberg committed Aug 25, 2024
1 parent 90e4c07 commit 17b6545
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
4 changes: 3 additions & 1 deletion internal/web/api.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package web

import (
// "log"
"log/slog"
"net/http"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -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")
}

Expand Down
4 changes: 2 additions & 2 deletions internal/web/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
}
8 changes: 7 additions & 1 deletion internal/web/routines-upd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

func updateRoutines() {
slog.Debug("Restarting go routines")

close(quitScan)

Expand All @@ -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)
}
23 changes: 14 additions & 9 deletions internal/web/scan-routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 17b6545

Please sign in to comment.