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)
|
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) {
|
func formatLineWaybar(stats *PingStats) {
|
||||||
res := new(WaybarOut)
|
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"
|
res.Class = "good"
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
@@ -143,7 +145,12 @@ func main() {
|
|||||||
tr := 0
|
tr := 0
|
||||||
lr := time.Now().UTC()
|
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.RecordRtts = false
|
||||||
|
|
||||||
pinger.OnRecv = func(pkt *probing.Packet) {
|
pinger.OnRecv = func(pkt *probing.Packet) {
|
||||||
@@ -152,11 +159,12 @@ func main() {
|
|||||||
lr = time.Now().UTC()
|
lr = time.Now().UTC()
|
||||||
ts = pinger.PacketsSent
|
ts = pinger.PacketsSent
|
||||||
tr = pinger.PacketsRecv
|
tr = pinger.PacketsRecv
|
||||||
if err != nil {
|
switch {
|
||||||
|
case err != nil:
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
} else if *Waybar {
|
case *Waybar:
|
||||||
formatLineWaybar(pStats)
|
formatLineWaybar(pStats)
|
||||||
} else {
|
default:
|
||||||
formatLinePolybar(pStats)
|
formatLinePolybar(pStats)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -167,11 +175,12 @@ func main() {
|
|||||||
pStats, err := lp.Stats(pinger.PacketsSent-ts, pinger.PacketsRecv-tr)
|
pStats, err := lp.Stats(pinger.PacketsSent-ts, pinger.PacketsRecv-tr)
|
||||||
ts = pinger.PacketsSent
|
ts = pinger.PacketsSent
|
||||||
tr = pinger.PacketsRecv
|
tr = pinger.PacketsRecv
|
||||||
if err != nil {
|
switch {
|
||||||
|
case err != nil:
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
} else if *Waybar {
|
case *Waybar:
|
||||||
formatLineWaybar(pStats)
|
formatLineWaybar(pStats)
|
||||||
} else {
|
default:
|
||||||
formatLinePolybar(pStats)
|
formatLinePolybar(pStats)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -188,12 +197,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
killLoop:
|
for range killSignals {
|
||||||
for {
|
break
|
||||||
select {
|
|
||||||
case <-killSignals:
|
|
||||||
break killLoop
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pinger.Stop()
|
pinger.Stop()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user