From 3ac8e9b450b783cf14501943c005e81097ecdebc Mon Sep 17 00:00:00 2001 From: sneak Date: Wed, 21 May 2025 11:22:57 -0700 Subject: [PATCH] bugfix --- Makefile | 2 +- main.go | 83 ++++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index e97d1dd..5b45de5 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ copy: main.go scp ./main.go root@las1stor1:/tmp/x run: copy - ssh root@las1stor1 -v "cd /tmp/x && go run ." + ssh -t root@las1stor1 -v "cd /tmp/x && go run ." clean: rm main.go diff --git a/main.go b/main.go index 1725db8..d77774e 100644 --- a/main.go +++ b/main.go @@ -50,15 +50,15 @@ const ( /*────────────────── runtime struct ─────────────*/ type InterfaceStatus struct { - Name, Label, IPInfo string - Reachable map[string]bool - Loss map[string]float64 - TCP map[string][]float64 + Name, Label, IPInfo string + Reachable map[string]bool + Loss map[string]float64 + TCP map[string][]float64 TotalICMPReq, TotalICMPRep int - DroppedCount int - LastDrop, LastPing time.Time - SpinFrame int - mu sync.RWMutex + DroppedCount int + LastDrop, LastPing time.Time + SpinFrame int + mu sync.RWMutex } /*────────────────── colour styles ──────────────*/ @@ -373,22 +373,42 @@ func reachLoop(ctx context.Context, st *InterfaceStatus, hosts []string) { wg.Add(1) go func(host string) { defer wg.Done() - st.mu.Lock(); st.TotalICMPReq++; st.mu.Unlock() + st.mu.Lock() + st.TotalICMPReq++ + st.mu.Unlock() ok := pingOnce(st.Name, host) - mu.Lock(); res[host] = ok; mu.Unlock() + mu.Lock() + res[host] = ok + mu.Unlock() st.mu.Lock() if ok { - st.TotalICMPRep++; st.spin() + st.TotalICMPRep++ + st.spin() } else { - st.DroppedCount++; st.LastDrop = time.Now() + st.DroppedCount++ + st.LastDrop = time.Now() } st.mu.Unlock() }(h) } wg.Wait() - st.mu.Lock(); st.Reachable = res; st.LastPing = time.Now(); st.mu.Unlock() + st.mu.Lock() + st.Reachable = res + st.LastPing = time.Now() + // Reset DroppedCount if all hosts are reachable + allReachable := true + for _, ok := range res { + if !ok { + allReachable = false + break + } + } + if allReachable { + st.DroppedCount = 0 + } + st.mu.Unlock() } } } @@ -408,11 +428,20 @@ func lossLoop(ctx context.Context, st *InterfaceStatus, hosts []string) { go func(host string) { defer wg.Done() lp := lossPercent(st.Name, host) - mu.Lock(); res[host] = lp; if lp == 0 { st.spin() }; mu.Unlock() + mu.Lock() + res[host] = lp + if lp == 0 { + st.spin() + } + mu.Unlock() }(h) } wg.Wait() - st.mu.Lock(); for k, v := range res { st.Loss[k] = v }; st.mu.Unlock() + st.mu.Lock() + for k, v := range res { + st.Loss[k] = v + } + st.mu.Unlock() } } } @@ -427,9 +456,15 @@ func tcpLoop(ctx context.Context, st *InterfaceStatus, hosts []string) { for _, hp := range hosts { ms := float64(tcpDuration(st.Name, hp).Milliseconds()) st.mu.Lock() - if ms < float64(tcpTimeout.Milliseconds()) { st.spin() } - hist := st.TCP[hp]; if len(hist) >= statsHistory { hist = hist[1:] } - st.TCP[hp] = append(hist, ms); st.mu.Unlock() + if ms < float64(tcpTimeout.Milliseconds()) { + st.spin() + } + hist := st.TCP[hp] + if len(hist) >= statsHistory { + hist = hist[1:] + } + st.TCP[hp] = append(hist, ms) + st.mu.Unlock() } } } @@ -489,8 +524,13 @@ func main() { } a, b := newStatus(*ifaceA, *labelA), newStatus(*ifaceB, *labelB) - scr, err := tcell.NewScreen(); if err != nil { panic(err) } - if err = scr.Init(); err != nil { panic(err) } + scr, err := tcell.NewScreen() + if err != nil { + panic(err) + } + if err = scr.Init(); err != nil { + panic(err) + } ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -504,7 +544,8 @@ func main() { if ev := scr.PollEvent(); ev != nil { if ke, ok := ev.(*tcell.EventKey); ok { if ke.Key() == tcell.KeyCtrlC || ke.Rune() == 'q' { - cancel(); return + cancel() + return } } }