From 110d60038f2b68574fae0e4ee93ab9fcd744bb9b Mon Sep 17 00:00:00 2001
From: Luca Moser <moser.luca@gmail.com>
Date: Mon, 29 Jun 2020 16:09:35 +0200
Subject: [PATCH] make the portcheck less flimsy

---
 plugins/portcheck/plugin.go | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/plugins/portcheck/plugin.go b/plugins/portcheck/plugin.go
index ebfe045b..6bcf907e 100644
--- a/plugins/portcheck/plugin.go
+++ b/plugins/portcheck/plugin.go
@@ -1,6 +1,7 @@
 package portcheck
 
 import (
+	"math/rand"
 	"net"
 	"sync"
 
@@ -65,14 +66,16 @@ func checkAutopeeringConnection() {
 
 	disc.Start(srv)
 	defer disc.Close()
-
-	for _, master := range autopeering.Discovery().GetMasterPeers() {
-		err = disc.Ping(master)
-		if err == nil {
-			log.Infof("Pong received from %s", master.IP())
+	
+	const retryCount = 10
+	entryNodes := autopeering.Discovery().GetMasterPeers()
+	for i := 0; i < retryCount; i++ {
+		randEntryNode := entryNodes[rand.Intn(len(entryNodes))]
+		if err = disc.Ping(randEntryNode); err == nil {
+			log.Infof("Pong received from %s", randEntryNode.IP())
 			break
 		}
-		log.Warnf("Error pinging entry node %s: %s", master.IP(), err)
+		log.Warnf("Error pinging entry node %s (attempts left %d/%d): %s", randEntryNode.IP(), retryCount-i+1, retryCount, err)
 	}
 
 	if err != nil {
-- 
GitLab