This commit is contained in:
2025-05-21 11:22:57 -07:00
parent e5546fedd4
commit 3ac8e9b450
2 changed files with 63 additions and 22 deletions
+62 -21
View File
@@ -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
}
}
}