From 114f68e00f519275a01c8aa53bda72a43a9525a0 Mon Sep 17 00:00:00 2001
From: Pea Nut <peanut2@systemli.org>
Date: Wed, 18 Dec 2024 11:33:19 +0100
Subject: [PATCH] [v5] Quickfix for devision by 0 in updateLocationQualityMap

In some cases the normalized quality is NaN. Then, the json (application
state) can not be decoded and everything fails:
INF avgRttNormalized := (rtt - minAvgRtt) / (maxAvgRtt - minAvgRtt)
INF avgRttNormalized := (126 - 126) / (126 - 126) => NaN
This will be fixed in a nice way in the future when we decided more
about gateway selection (and mybe have a quality given by menshen)
---
 pkg/vpn/menshen/location.go | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/pkg/vpn/menshen/location.go b/pkg/vpn/menshen/location.go
index 62c21309..e90c5d6d 100644
--- a/pkg/vpn/menshen/location.go
+++ b/pkg/vpn/menshen/location.go
@@ -141,6 +141,9 @@ func (m *Menshen) updateLocationQualityMap(transport string) {
 	// normalize values (from rtt in ms to a number between 0 and 1)
 	for location, rtt := range qualityMap {
 		avgRttNormalized := (rtt - minAvgRtt) / (maxAvgRtt - minAvgRtt)
+		if math.IsNaN(avgRttNormalized) {
+			avgRttNormalized = 0
+		}
 		// higher latency is bad, so 1 - avgRttNormalized
 		qualityMap[location] = 1 - avgRttNormalized
 	}
-- 
GitLab