From 8542417d40877a22da5d829681ceacf814c41706 Mon Sep 17 00:00:00 2001 From: aceberg <1502200+aceberg@users.noreply.github.com> Date: Sun, 25 Aug 2024 23:39:26 +0700 Subject: [PATCH] pprof --- cmd/WatchYourLAN/main.go | 11 +++++++++++ internal/web/api.go | 2 ++ internal/web/const-var.go | 4 ++-- internal/web/routines-upd.go | 1 - internal/web/scan-routine.go | 15 +++++---------- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/cmd/WatchYourLAN/main.go b/cmd/WatchYourLAN/main.go index 96de135..40acd6d 100644 --- a/cmd/WatchYourLAN/main.go +++ b/cmd/WatchYourLAN/main.go @@ -2,7 +2,9 @@ package main import ( "flag" + // "net/http" + // _ "net/http/pprof" _ "time/tzdata" "github.com/aceberg/WatchYourLAN/internal/web" @@ -16,5 +18,14 @@ func main() { nodePtr := flag.String("n", nodePath, "Path to node modules") flag.Parse() + // pprof - memory leak detect + // go tool pprof -alloc_space http://localhost:8085/debug/pprof/heap + // (pprof) web + // (pprof) list db.Select + // + // go func() { + // http.ListenAndServe("localhost:8085", nil) + // }() + web.Gui(*dirPtr, *nodePtr) // webgui.go } diff --git a/internal/web/api.go b/internal/web/api.go index 8752895..63dd8c1 100644 --- a/internal/web/api.go +++ b/internal/web/api.go @@ -21,6 +21,8 @@ func apiAll(c *gin.Context) { func apiHistory(c *gin.Context) { var hosts []models.Host + histHosts := db.Select(appConfig.DBPath, "history") + mac := c.Param("mac") if mac != "/" { diff --git a/internal/web/const-var.go b/internal/web/const-var.go index 7cc526e..e8cbf92 100644 --- a/internal/web/const-var.go +++ b/internal/web/const-var.go @@ -10,8 +10,8 @@ var ( // appConfig - config for Web Gui appConfig models.Conf - allHosts []models.Host - histHosts []models.Host + allHosts []models.Host + // histHosts []models.Host quitScan chan bool ) diff --git a/internal/web/routines-upd.go b/internal/web/routines-upd.go index d8d6a24..7fcdd56 100644 --- a/internal/web/routines-upd.go +++ b/internal/web/routines-upd.go @@ -16,7 +16,6 @@ func updateRoutines() { db.Create(appConfig.DBPath) allHosts = db.Select(appConfig.DBPath, "now") - histHosts = db.Select(appConfig.DBPath, "history") quitScan = make(chan bool) go startScan(quitScan) // scan-routine.go diff --git a/internal/web/scan-routine.go b/internal/web/scan-routine.go index 5f76c44..57e4b2f 100644 --- a/internal/web/scan-routine.go +++ b/internal/web/scan-routine.go @@ -12,10 +12,6 @@ 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, nowDate, plusDate time.Time var foundHosts []models.Host @@ -33,7 +29,6 @@ func startScan(quit chan bool) { foundHosts = arp.Scan(appConfig.Ifaces) compareHosts(foundHosts) allHosts = db.Select(appConfig.DBPath, "now") - histHosts = db.Select(appConfig.DBPath, "history") lastDate = time.Now() } @@ -46,14 +41,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 @@ -75,7 +70,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)