some code cleanup; retry dns resolve
This commit is contained in:
35
rtt.go
35
rtt.go
@@ -71,13 +71,15 @@ func formatLinePolybar(stats *PingStats) {
|
||||
packetColor = fmt.Sprintf("%%{F#%s}", *CritColor)
|
||||
}
|
||||
|
||||
fmt.Printf("%s\uE4E2 %d \uE43C %dms %s\uF1B2 %d%%\n", rttColor, stats.AvgRtt.Milliseconds(), stats.StdDev.Milliseconds(), packetColor, int(math.Round(stats.PacketLoss)))
|
||||
fmt.Printf("%s\uE4E2 %d \uE43C %dms %s\uF1B2 %d%%\n", rttColor, stats.AvgRtt.Milliseconds(),
|
||||
stats.StdDev.Milliseconds(), packetColor, int(math.Round(stats.PacketLoss)))
|
||||
}
|
||||
|
||||
func formatLineWaybar(stats *PingStats) {
|
||||
res := new(WaybarOut)
|
||||
|
||||
res.Text = fmt.Sprintf("\uE4E2 %d \uE43C %dms \uF1B2 %d%%", stats.AvgRtt.Milliseconds(), stats.StdDev.Milliseconds(), int(math.Round(stats.PacketLoss)))
|
||||
res.Text = fmt.Sprintf("\uE4E2 %d \uE43C %dms \uF1B2 %d%%", stats.AvgRtt.Milliseconds(),
|
||||
stats.StdDev.Milliseconds(), int(math.Round(stats.PacketLoss)))
|
||||
res.Class = "good"
|
||||
|
||||
switch {
|
||||
@@ -143,7 +145,12 @@ func main() {
|
||||
tr := 0
|
||||
lr := time.Now().UTC()
|
||||
|
||||
pinger, err := probing.NewPinger(*Host)
|
||||
var pinger *probing.Pinger
|
||||
var err error
|
||||
for pinger, err = probing.NewPinger(*Host); err != nil; {
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
|
||||
pinger.RecordRtts = false
|
||||
|
||||
pinger.OnRecv = func(pkt *probing.Packet) {
|
||||
@@ -152,11 +159,12 @@ func main() {
|
||||
lr = time.Now().UTC()
|
||||
ts = pinger.PacketsSent
|
||||
tr = pinger.PacketsRecv
|
||||
if err != nil {
|
||||
switch {
|
||||
case err != nil:
|
||||
fmt.Println(err)
|
||||
} else if *Waybar {
|
||||
case *Waybar:
|
||||
formatLineWaybar(pStats)
|
||||
} else {
|
||||
default:
|
||||
formatLinePolybar(pStats)
|
||||
}
|
||||
}
|
||||
@@ -167,11 +175,12 @@ func main() {
|
||||
pStats, err := lp.Stats(pinger.PacketsSent-ts, pinger.PacketsRecv-tr)
|
||||
ts = pinger.PacketsSent
|
||||
tr = pinger.PacketsRecv
|
||||
if err != nil {
|
||||
switch {
|
||||
case err != nil:
|
||||
fmt.Println(err)
|
||||
} else if *Waybar {
|
||||
case *Waybar:
|
||||
formatLineWaybar(pStats)
|
||||
} else {
|
||||
default:
|
||||
formatLinePolybar(pStats)
|
||||
}
|
||||
}
|
||||
@@ -188,12 +197,8 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
killLoop:
|
||||
for {
|
||||
select {
|
||||
case <-killSignals:
|
||||
break killLoop
|
||||
}
|
||||
for range killSignals {
|
||||
break
|
||||
}
|
||||
pinger.Stop()
|
||||
}
|
||||
|
Reference in New Issue
Block a user