From 44af23c162c7c80d5578c83ac5bfe0c9b32bd58c Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sat, 19 Sep 2020 17:28:19 +0100 Subject: [PATCH] restrict forwarding of queries to sqrt of neighboring connections. Thanks to 'qtm' for the idea --- .../connection/UltrapeerConnectionManager.groovy | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy b/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy index ef9736f6..0e60a360 100644 --- a/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy @@ -21,6 +21,8 @@ class UltrapeerConnectionManager extends ConnectionManager { final Map peerConnections = new ConcurrentHashMap() final Map leafConnections = new ConcurrentHashMap() + + private final Random random = new Random() UltrapeerConnectionManager() {} @@ -44,8 +46,16 @@ class UltrapeerConnectionManager extends ConnectionManager { if (e.replyTo != me.destination && e.receivedOn != me.destination && !leafConnections.containsKey(e.receivedOn)) e.firstHop = false + final int connCount = peerConnections.size() + if (connCount == 0) + return + final int treshold = (int)(Math.sqrt(connCount)) + 1 peerConnections.values().each { - if (e.getReceivedOn() != it.getEndpoint().getDestination()) + // 1. do not send query back to originator + // 2. if firstHop forward to everyone + // 3. otherwise to randomized sqrt of neighbors + if (e.getReceivedOn() != it.getEndpoint().getDestination() && + (e.firstHop || random.nextInt(connCount) < treshold)) it.sendQuery(e) } }