Skip to content
Snippets Groups Projects

[enh] #1254 Enhance the connection to outcoming WS2P peers

2 files
+ 41
22
Compare changes
  • Side-by-side
  • Inline

Files

+ 39
22
@@ -427,32 +427,49 @@ export class WS2PCluster {
@@ -427,32 +427,49 @@ export class WS2PCluster {
let i = 0
let i = 0
let countPublicNodesWithSameKey:number = 1 // Necessary if maxPrivate = 0
let countPublicNodesWithSameKey:number = 1 // Necessary if maxPrivate = 0
let endpointsNodesWithSameKey:WS2PEndpoint[] = []
let endpointsNodesWithSameKey:WS2PEndpoint[] = []
while (i < peers.length && (this.clientsCount() < this.maxLevel1Size || this.numberOfConnectedPublicNodesWithSameKey() < countPublicNodesWithSameKey) ) {
// Group the peers by bunches
const p = peers[i]
const bunchsOfPeers = peers.reduce((bundles:PeerDTO[][], p:PeerDTO) => {
if (p.pubkey === this.server.conf.pair.pub) {
let bundleIndex = (bundles.length || 1) - 1
endpointsNodesWithSameKey = p.getAllWS2PEndpoints(canReachTorEndpoint, canReachClearEndpoint, myUUID)
// Maximum size of a bundle of peers
countPublicNodesWithSameKey = endpointsNodesWithSameKey.length
if (bundles[bundleIndex] && bundles[bundleIndex].length >= WS2PConstants.INITIAL_CONNECTION_PEERS_BUNDLE_SIZE) {
for (const api of endpointsNodesWithSameKey) {
bundleIndex++
try {
}
// We do not connect to local host
// We create the bundle of it doesn't exist yet
if (api.uuid !== myUUID) {
if (!bundles[bundleIndex]) {
await this.connectToRemoteWS(api.version, api.host, api.port, api.path, this.messageHandler, p.pubkey, api.uuid)
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 {
} else {
const api = p.getOnceWS2PEndpoint(canReachTorEndpoint, canReachClearEndpoint)
const api = p.getOnceWS2PEndpoint(canReachTorEndpoint, canReachClearEndpoint)
if (api) {
if (api) {
try {
try {
// We do not connect to local host
// We do not connect to local host
await this.connectToRemoteWS(api.version, api.host, api.port, api.path, this.messageHandler, p.pubkey, api.uuid)
await this.connectToRemoteWS(api.version, api.host, api.port, api.path, this.messageHandler, p.pubkey, api.uuid)
} catch (e) {
} catch (e) {
this.server.logger.debug('WS2P: init: failed connection')
this.server.logger.debug('WS2P: init: failed connection')
}
}
}
}
}
}
}))
i++
i++
// Trim the eventual extra connections
// Trim the eventual extra connections
setTimeout(() => this.removeLowPriorityConnections(prefered), WS2PConstants.CONNEXION_TIMEOUT)
setTimeout(() => this.removeLowPriorityConnections(prefered), WS2PConstants.CONNEXION_TIMEOUT)
Loading