From b81c745bc4ad94a720d43c74e1dfacb6eb048577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Moreau?= <cem.moreau@gmail.com> Date: Sat, 20 Jan 2018 19:10:33 +0100 Subject: [PATCH] [enh] #1254 Enhance the connection to outcoming WS2P peers --- app/modules/ws2p/lib/WS2PCluster.ts | 61 ++++++++++++++++++----------- app/modules/ws2p/lib/constants.ts | 2 + 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/app/modules/ws2p/lib/WS2PCluster.ts b/app/modules/ws2p/lib/WS2PCluster.ts index 101e0c89d..3b912104f 100644 --- a/app/modules/ws2p/lib/WS2PCluster.ts +++ b/app/modules/ws2p/lib/WS2PCluster.ts @@ -427,32 +427,49 @@ export class WS2PCluster { let i = 0 let countPublicNodesWithSameKey:number = 1 // Necessary if maxPrivate = 0 let endpointsNodesWithSameKey:WS2PEndpoint[] = [] - while (i < peers.length && (this.clientsCount() < this.maxLevel1Size || this.numberOfConnectedPublicNodesWithSameKey() < countPublicNodesWithSameKey) ) { - const p = peers[i] - if (p.pubkey === this.server.conf.pair.pub) { - endpointsNodesWithSameKey = p.getAllWS2PEndpoints(canReachTorEndpoint, canReachClearEndpoint, myUUID) - countPublicNodesWithSameKey = endpointsNodesWithSameKey.length - for (const api of endpointsNodesWithSameKey) { - try { - // We do not connect to local host - if (api.uuid !== myUUID) { - await this.connectToRemoteWS(api.version, api.host, api.port, api.path, this.messageHandler, p.pubkey, api.uuid) + // Group the peers by bunches + const bunchsOfPeers = peers.reduce((bundles:PeerDTO[][], p:PeerDTO) => { + let bundleIndex = (bundles.length || 1) - 1 + // Maximum size of a bundle of peers + if (bundles[bundleIndex] && bundles[bundleIndex].length >= WS2PConstants.INITIAL_CONNECTION_PEERS_BUNDLE_SIZE) { + bundleIndex++ + } + // We create the bundle of it doesn't exist yet + if (!bundles[bundleIndex]) { + bundles[bundleIndex] = [] + } + // We feed it with this peer + bundles[bundleIndex].push(p) + return bundles + }, []) + while (i < bunchsOfPeers.length && (this.clientsCount() < this.maxLevel1Size || this.numberOfConnectedPublicNodesWithSameKey() < countPublicNodesWithSameKey) ) { + this.server.logger.info("WS2P: init: bundle of peers %s/%s", i+1, bunchsOfPeers.length) + await Promise.all(bunchsOfPeers[i].map(async p => { + if (p.pubkey === this.server.conf.pair.pub) { + endpointsNodesWithSameKey = p.getAllWS2PEndpoints(canReachTorEndpoint, canReachClearEndpoint, myUUID) + countPublicNodesWithSameKey = endpointsNodesWithSameKey.length + for (const api of endpointsNodesWithSameKey) { + try { + // We do not connect to local host + if (api.uuid !== myUUID) { + await this.connectToRemoteWS(api.version, api.host, api.port, api.path, this.messageHandler, p.pubkey, api.uuid) + } + } catch (e) { + this.server.logger.debug('WS2P: init: failed connection') } - } catch (e) { - this.server.logger.debug('WS2P: init: failed connection') } - } - } else { - const api = p.getOnceWS2PEndpoint(canReachTorEndpoint, canReachClearEndpoint) - if (api) { - try { - // We do not connect to local host - await this.connectToRemoteWS(api.version, api.host, api.port, api.path, this.messageHandler, p.pubkey, api.uuid) - } catch (e) { - this.server.logger.debug('WS2P: init: failed connection') + } else { + const api = p.getOnceWS2PEndpoint(canReachTorEndpoint, canReachClearEndpoint) + if (api) { + try { + // We do not connect to local host + await this.connectToRemoteWS(api.version, api.host, api.port, api.path, this.messageHandler, p.pubkey, api.uuid) + } catch (e) { + this.server.logger.debug('WS2P: init: failed connection') + } } } - } + })) i++ // Trim the eventual extra connections setTimeout(() => this.removeLowPriorityConnections(prefered), WS2PConstants.CONNEXION_TIMEOUT) diff --git a/app/modules/ws2p/lib/constants.ts b/app/modules/ws2p/lib/constants.ts index 84bda573f..664026793 100644 --- a/app/modules/ws2p/lib/constants.ts +++ b/app/modules/ws2p/lib/constants.ts @@ -78,5 +78,7 @@ export const WS2PConstants = { HOST_ONION_REGEX: CommonConstants.HOST_ONION_REGEX, FULL_ADDRESS_ONION_REGEX: CommonConstants.WS_FULL_ADDRESS_ONION_REGEX, + INITIAL_CONNECTION_PEERS_BUNDLE_SIZE: 5, + HEADS_SPREAD_TIMEOUT: 100 // Wait 100ms before sending a bunch of signed heads } \ No newline at end of file -- GitLab