Make maximum connections configurable (#731)

This commit is contained in:
Hanno Cornelius 2021-10-12 14:48:48 +02:00 committed by GitHub
parent 83f71ae905
commit 165e235158
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 12 deletions

View File

@ -13,6 +13,7 @@ This release contains the following:
- GossipSub [prune backoff period](https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.1.md#prune-backoff-and-peer-exchange) is now the recommended 1 minute
- Bridge now uses content topic format according to [23/WAKU2-TOPICS](https://rfc.vac.dev/spec/23/)
- Better internal differentiation between local and remote peer info
- Maximum number of libp2p connections is now configurable
#### General refactoring

View File

@ -834,4 +834,44 @@ procSuite "WakuNode":
responseCount == 2
await node1.stop()
await node2.stop()
await node2.stop()
asyncTest "Maximum connections can be configured":
let
maxConnections = 2
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"),
Port(60010), maxConnections = maxConnections)
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"),
Port(60012))
nodeKey3 = crypto.PrivateKey.random(Secp256k1, rng[])[]
node3 = WakuNode.new(nodeKey3, ValidIpAddress.init("0.0.0.0"),
Port(60013))
check:
# Sanity check, to verify config was applied
node1.switch.connManager.inSema.size == maxConnections
# Node with connection limit set to 1
await node1.start()
node1.mountRelay()
# Remote node 1
await node2.start()
node2.mountRelay()
# Remote node 2
await node3.start()
node3.mountRelay()
discard await node1.peerManager.dialPeer(node2.peerInfo.toRemotePeerInfo(), WakuRelayCodec)
await sleepAsync(3.seconds)
discard await node1.peerManager.dialPeer(node3.peerInfo.toRemotePeerInfo(), WakuRelayCodec)
check:
# Verify that only the first connection succeeded
node1.switch.isConnected(node2.peerInfo.peerId)
node1.switch.isConnected(node3.peerInfo.peerId) == false
await allFutures([node1.stop(), node2.stop(), node3.stop()])

View File

@ -41,6 +41,11 @@ type
desc: "Specify method to use for determining public address. " &
"Must be one of: any, none, upnp, pmp, extip:<IP>."
defaultValue: "any" }: string
maxConnections* {.
desc: "Maximum allowed number of libp2p connections."
defaultValue: 50
name: "max-connections" }: uint16
## Persistence config

View File

@ -128,7 +128,8 @@ template tcpEndPoint(address, port): auto =
proc new*(T: type WakuNode, nodeKey: crypto.PrivateKey,
bindIp: ValidIpAddress, bindPort: Port,
extIp = none[ValidIpAddress](), extPort = none[Port](),
peerStorage: PeerStorage = nil): T
peerStorage: PeerStorage = nil,
maxConnections = builders.MaxConnections): T
{.raises: [Defect, LPError].} =
## Creates a Waku Node.
##
@ -153,15 +154,13 @@ proc new*(T: type WakuNode, nodeKey: crypto.PrivateKey,
for multiaddr in announcedAddresses:
peerInfo.addrs.add(multiaddr) # Announced addresses in index > 0
var switch = newStandardSwitch(some(nodekey), hostAddress,
transportFlags = {ServerFlags.ReuseAddr}, rng = rng)
# TODO Untested - verify behavior after switch interface change
# More like this:
# let pubsub = GossipSub.init(
# switch = switch,
# msgIdProvider = msgIdProvider,
# triggerSelf = true, sign = false,
# verifySignature = false).PubSub
var switch = newStandardSwitch(
some(nodekey),
hostAddress,
transportFlags = {ServerFlags.ReuseAddr},
rng = rng,
maxConnections = maxConnections)
let wakuNode = WakuNode(
peerManager: PeerManager.new(switch, peerStorage),
switch: switch,
@ -841,7 +840,8 @@ when isMainModule:
node = WakuNode.new(conf.nodekey,
conf.listenAddress, Port(uint16(conf.tcpPort) + conf.portsShift),
extIp, extPort,
pStorage)
pStorage,
conf.maxConnections.int)
ok(node)