From e5fc20b6a7b4c3fb060cf1b453d117dd41af4de5 Mon Sep 17 00:00:00 2001 From: diegomrsantos Date: Thu, 15 Dec 2022 11:23:29 +0100 Subject: [PATCH] Update libp2p/services/autonatservice.nim Co-authored-by: Tanguy --- libp2p/services/autonatservice.nim | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/libp2p/services/autonatservice.nim b/libp2p/services/autonatservice.nim index 03506df211..9fc2345538 100644 --- a/libp2p/services/autonatservice.nim +++ b/libp2p/services/autonatservice.nim @@ -77,21 +77,15 @@ proc handleAnswer(self: AutonatService, ans: NetworkReachability) {.async.} = self.answerDeque.addLast(ans) let reachableCount = toSeq(self.answerDeque.items).countIt(it == NetworkReachability.Reachable) - let confidence = reachableCount / self.maxQueueSize - if confidence >= self.minConfidence: - self.networkReachability = NetworkReachability.Reachable - self.confidence = some(confidence) - libp2p_autonat_reachability_confidence.set(value = confidence, labelValues = ["Reachable"]) - else: - let notReachableCount = toSeq(self.answerDeque.items).countIt(it == NetworkReachability.NotReachable) - let confidence = notReachableCount / self.maxQueueSize - if confidence >= self.minConfidence: - self.networkReachability = NetworkReachability.NotReachable + self.networkReachability = Unknown + self.confidence = none(float) + const reachabilityPriority = [Reachable, Unreachable] + for reachability in reachabilityPriority: + let confidence = self.answers.countIt(reachability) / self.maxQueueSize + libp2p_autonat_reachability_confidence.set(value = confidence, labelValues = [$reachability]) + if self.confidence.isNone and confidence >= self.minConfidence: + self.networkReachability = reachability self.confidence = some(confidence) - libp2p_autonat_reachability_confidence.set(confidence, ["NotReachable"]) - else: - self.networkReachability = NetworkReachability.Unknown - self.confidence = none(float) if not isNil(self.statusAndConfidenceHandler): await self.statusAndConfidenceHandler(self.networkReachability, self.confidence)