Skip to content

Commit

Permalink
Update libp2p/services/autonatservice.nim
Browse files Browse the repository at this point in the history
Co-authored-by: Tanguy <tanguy@status.im>
  • Loading branch information
diegomrsantos and Menduist authored Dec 15, 2022
1 parent c3ef435 commit e5fc20b
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions libp2p/services/autonatservice.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e5fc20b

Please sign in to comment.