Skip to content

Commit

Permalink
feat(rosenet-node, rosenet-relay): limit relays count for nodes
Browse files Browse the repository at this point in the history
Connecting to a lot of relays can significantly affect pubsub network
traffic. For 40 nodes, 3 relay per node seems a good balance between
traffic, delivery and performance. This number may change in the future,
though.

Also prevent relays from broadcasting their addresses over discovery
network. If not disable, it totally negates the point of limiting relays
count to 3.
  • Loading branch information
mkermani144 committed Aug 18, 2024
1 parent d7f66e7 commit c1e3008
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
12 changes: 9 additions & 3 deletions packages/rosenet-node/lib/createRoseNetNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import addressService from './address/address-service';
import streamService from './stream/stream-service';

import { decode, encode } from './utils/codec';
import sample from './utils/sample';

import RoseNetNodeError from './errors/RoseNetNodeError';

Expand Down Expand Up @@ -61,6 +62,11 @@ const createRoseNetNode = async ({
const announceMultiaddr = await addressService.getAnnounceMultiaddr(port);
logger.info(`${announceMultiaddr} set as announce multiaddr`);

const sampledRelayMultiaddrs = sample(
config.relayMultiaddrs,
RELAYS_COUNT_TO_CONNECT,
);

const node = await createLibp2p({
peerId,
transports: [
Expand All @@ -72,13 +78,13 @@ const createRoseNetNode = async ({
addresses: {
listen: [
`/ip4/0.0.0.0/tcp/${port}`,
...config.relayMultiaddrs.map(
...sampledRelayMultiaddrs.map(
(multiaddr) => `${multiaddr}/p2p-circuit`,
),
],
announce: [
announceMultiaddr,
...config.relayMultiaddrs.map(
...sampledRelayMultiaddrs.map(
(multiaddr) => `${multiaddr}/p2p-circuit`,
),
],
Expand All @@ -98,7 +104,7 @@ const createRoseNetNode = async ({
streamMuxers: [yamux()],
peerDiscovery: [
bootstrap({
list: config.relayMultiaddrs,
list: sampledRelayMultiaddrs,
}),
pubsubPeerDiscovery(),
],
Expand Down
13 changes: 13 additions & 0 deletions packages/rosenet-node/lib/utils/sample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Sample `size` elements from `array` randomly
* @param array
* @param size
*/
const sample = <T>(array: T[], size: number): T[] =>
array
.map((value) => ({ value, sort: Math.random() }))
.sort((a, b) => a.sort - b.sort)
.map(({ value }) => value)
.slice(0, size);

export default sample;
2 changes: 1 addition & 1 deletion packages/rosenet-relay/lib/createRoseNetRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const createRoseNetRelay = async ({
}),
identify: identify(),
},
peerDiscovery: [pubsubPeerDiscovery()],
peerDiscovery: [pubsubPeerDiscovery({ listenOnly: true })],
nodeInfo: {
name: 'rosenet-relay',
version: packageJson.version,
Expand Down

0 comments on commit c1e3008

Please sign in to comment.