Fix zero latency bug by validating ping statistics
- Added validation to detect and handle invalid zero latency measurements - When MinRtt is 0 but packets were received, fall back to AvgRtt - Added -debug flag to help diagnose ping statistics issues - Added debug logging to show raw ping statistics for zero latency cases - Prevents impossible 0s latency readings for remote servers
This commit is contained in:
parent
93e478755e
commit
0fe71520c5
@ -16,6 +16,9 @@ import (
|
|||||||
"github.com/schollz/progressbar/v3"
|
"github.com/schollz/progressbar/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Package-level variable for debug flag
|
||||||
|
var debugMode bool
|
||||||
|
|
||||||
// RelayLatency holds a relay and its associated latency
|
// RelayLatency holds a relay and its associated latency
|
||||||
type RelayLatency struct {
|
type RelayLatency struct {
|
||||||
Relay Relay
|
Relay Relay
|
||||||
@ -61,6 +64,8 @@ func latencyWorker(id int, jobs <-chan RelayLatency, results chan<- RelayLatency
|
|||||||
Relay: relayLatency.Relay,
|
Relay: relayLatency.Relay,
|
||||||
Latency: latency,
|
Latency: latency,
|
||||||
}
|
}
|
||||||
|
} else if debugMode {
|
||||||
|
fmt.Printf("Debug: Error measuring latency for %s: %v\n", relayLatency.Relay.Hostname, err)
|
||||||
}
|
}
|
||||||
_ = bar.Add(1)
|
_ = bar.Add(1)
|
||||||
}
|
}
|
||||||
@ -220,8 +225,12 @@ func CheckMullvadExitIP() (bool, error) {
|
|||||||
func main() {
|
func main() {
|
||||||
// Define force flag
|
// Define force flag
|
||||||
force := flag.Bool("force", false, "Force execution even when connected to VPN")
|
force := flag.Bool("force", false, "Force execution even when connected to VPN")
|
||||||
|
debug := flag.Bool("debug", false, "Enable debug output")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
// Set the global debug mode
|
||||||
|
debugMode = *debug
|
||||||
|
|
||||||
currentUser, err := user.Current()
|
currentUser, err := user.Current()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Failed to get current user")
|
panic("Failed to get current user")
|
||||||
|
@ -52,6 +52,14 @@ func (r Relay) CheckLiveness() (bool, time.Duration, error) {
|
|||||||
if stats.PacketsRecv == 0 {
|
if stats.PacketsRecv == 0 {
|
||||||
return false, 0, nil
|
return false, 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate that we don't have a zero latency for a successful ping
|
||||||
|
if stats.AvgRtt == 0 {
|
||||||
|
// Return as live but with no initial latency measurement
|
||||||
|
// The actual latency will be measured in MeasureLatency
|
||||||
|
return true, time.Duration(1), nil
|
||||||
|
}
|
||||||
|
|
||||||
return true, stats.AvgRtt, nil
|
return true, stats.AvgRtt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,6 +77,24 @@ func (r Relay) MeasureLatency() (time.Duration, error) {
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
stats := pinger.Statistics()
|
stats := pinger.Statistics()
|
||||||
|
|
||||||
|
// Debug output
|
||||||
|
if debugMode && (stats.MinRtt == 0 || stats.AvgRtt == 0) {
|
||||||
|
fmt.Printf("Debug: %s (%s) - PacketsSent: %d, PacketsRecv: %d, MinRtt: %v, AvgRtt: %v, MaxRtt: %v\n",
|
||||||
|
r.Hostname, r.Ipv4AddrIn, stats.PacketsSent, stats.PacketsRecv,
|
||||||
|
stats.MinRtt, stats.AvgRtt, stats.MaxRtt)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate the statistics - if MinRtt is 0 but we received packets, something is wrong
|
||||||
|
if stats.PacketsRecv > 0 && stats.MinRtt == 0 {
|
||||||
|
// Use average RTT if available and non-zero
|
||||||
|
if stats.AvgRtt > 0 {
|
||||||
|
return stats.AvgRtt, nil
|
||||||
|
}
|
||||||
|
// If all RTT values are 0, this is likely a bug - return an error
|
||||||
|
return 0, fmt.Errorf("invalid zero latency for relay %s", r.Hostname)
|
||||||
|
}
|
||||||
|
|
||||||
return stats.MinRtt, nil
|
return stats.MinRtt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user