mirror of
https://github.com/waku-org/js-waku.git
synced 2025-02-23 09:38:24 +00:00
reform connection manager configurations
This commit is contained in:
parent
7efbd26609
commit
a6dd49974c
@ -10,7 +10,6 @@ import {
|
|||||||
IConnectionStateEvents,
|
IConnectionStateEvents,
|
||||||
IPeersByDiscoveryEvents,
|
IPeersByDiscoveryEvents,
|
||||||
IRelay,
|
IRelay,
|
||||||
KeepAliveOptions,
|
|
||||||
PeersByDiscoveryResult,
|
PeersByDiscoveryResult,
|
||||||
PubsubTopic,
|
PubsubTopic,
|
||||||
ShardInfo
|
ShardInfo
|
||||||
@ -23,13 +22,15 @@ import { KeepAliveManager } from "./keep_alive_manager.js";
|
|||||||
|
|
||||||
const log = new Logger("connection-manager");
|
const log = new Logger("connection-manager");
|
||||||
|
|
||||||
export const DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED = 1;
|
const DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED = 1;
|
||||||
export const DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER = 3;
|
const DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER = 3;
|
||||||
export const DEFAULT_MAX_PARALLEL_DIALS = 3;
|
const DEFAULT_MAX_PARALLEL_DIALS = 3;
|
||||||
|
|
||||||
|
const DEFAULT_PING_KEEP_ALIVE_SEC = 5 * 60;
|
||||||
|
const DEFAULT_RELAY_KEEP_ALIVE_SEC = 5 * 60;
|
||||||
|
|
||||||
type ConnectionManagerConstructorOptions = {
|
type ConnectionManagerConstructorOptions = {
|
||||||
libp2p: Libp2p;
|
libp2p: Libp2p;
|
||||||
keepAliveOptions: KeepAliveOptions;
|
|
||||||
pubsubTopics: PubsubTopic[];
|
pubsubTopics: PubsubTopic[];
|
||||||
relay?: IRelay;
|
relay?: IRelay;
|
||||||
config?: Partial<ConnectionManagerOptions>;
|
config?: Partial<ConnectionManagerOptions>;
|
||||||
@ -151,13 +152,18 @@ export class ConnectionManager
|
|||||||
maxDialAttemptsForPeer: DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER,
|
maxDialAttemptsForPeer: DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER,
|
||||||
maxBootstrapPeersAllowed: DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED,
|
maxBootstrapPeersAllowed: DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED,
|
||||||
maxParallelDials: DEFAULT_MAX_PARALLEL_DIALS,
|
maxParallelDials: DEFAULT_MAX_PARALLEL_DIALS,
|
||||||
|
pingKeepAlive: DEFAULT_PING_KEEP_ALIVE_SEC,
|
||||||
|
relayKeepAlive: DEFAULT_RELAY_KEEP_ALIVE_SEC,
|
||||||
...options.config
|
...options.config
|
||||||
};
|
};
|
||||||
|
|
||||||
this.keepAliveManager = new KeepAliveManager({
|
this.keepAliveManager = new KeepAliveManager({
|
||||||
relay: options.relay,
|
relay: options.relay,
|
||||||
libp2p: options.libp2p,
|
libp2p: options.libp2p,
|
||||||
options: options.keepAliveOptions
|
options: {
|
||||||
|
pingKeepAlive: this.options.pingKeepAlive,
|
||||||
|
relayKeepAlive: this.options.relayKeepAlive
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
this.startEventListeners()
|
this.startEventListeners()
|
||||||
|
@ -1,14 +1,18 @@
|
|||||||
import type { PeerId } from "@libp2p/interface";
|
import type { PeerId } from "@libp2p/interface";
|
||||||
import type { IRelay, Libp2p, PeerIdStr } from "@waku/interfaces";
|
import type { IRelay, Libp2p, PeerIdStr } from "@waku/interfaces";
|
||||||
import type { KeepAliveOptions } from "@waku/interfaces";
|
|
||||||
import { Logger, pubsubTopicToSingleShardInfo } from "@waku/utils";
|
import { Logger, pubsubTopicToSingleShardInfo } from "@waku/utils";
|
||||||
import { utf8ToBytes } from "@waku/utils/bytes";
|
import { utf8ToBytes } from "@waku/utils/bytes";
|
||||||
|
|
||||||
import { createEncoder } from "./message/version_0.js";
|
import { createEncoder } from "./message/version_0.js";
|
||||||
|
|
||||||
export const RelayPingContentTopic = "/relay-ping/1/ping/null";
|
const RelayPingContentTopic = "/relay-ping/1/ping/null";
|
||||||
const log = new Logger("keep-alive");
|
const log = new Logger("keep-alive");
|
||||||
|
|
||||||
|
type KeepAliveOptions = {
|
||||||
|
pingKeepAlive: number;
|
||||||
|
relayKeepAlive: number;
|
||||||
|
};
|
||||||
|
|
||||||
type CreateKeepAliveManagerOptions = {
|
type CreateKeepAliveManagerOptions = {
|
||||||
options: KeepAliveOptions;
|
options: KeepAliveOptions;
|
||||||
libp2p: Libp2p;
|
libp2p: Libp2p;
|
||||||
|
@ -8,22 +8,44 @@ export enum Tags {
|
|||||||
LOCAL = "local-peer-cache"
|
LOCAL = "local-peer-cache"
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ConnectionManagerOptions {
|
export type ConnectionManagerOptions = {
|
||||||
/**
|
/**
|
||||||
* Number of attempts before a peer is considered non-dialable
|
* Number of attempts before a peer is considered non-dialable.
|
||||||
* This is used to not spam a peer with dial attempts when it is not dialable
|
* This is used to not spam a peer with dial attempts when it is not dialable.
|
||||||
|
*
|
||||||
|
* @default 3
|
||||||
*/
|
*/
|
||||||
maxDialAttemptsForPeer: number;
|
maxDialAttemptsForPeer: number;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Max number of bootstrap peers allowed to be connected to, initially
|
* Max number of bootstrap peers allowed to be connected to initially.
|
||||||
* This is used to increase intention of dialing non-bootstrap peers, found using other discovery mechanisms (like Peer Exchange)
|
* This is used to increase intention of dialing non-bootstrap peers, found using other discovery mechanisms (like Peer Exchange).
|
||||||
|
*
|
||||||
|
* @default 1
|
||||||
*/
|
*/
|
||||||
maxBootstrapPeersAllowed: number;
|
maxBootstrapPeersAllowed: number;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Max number of parallel dials allowed
|
* Max number of parallel dials allowed.
|
||||||
|
*
|
||||||
|
* @default 3
|
||||||
*/
|
*/
|
||||||
maxParallelDials: number;
|
maxParallelDials: number;
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* Keep alive libp2p pings interval in seconds.
|
||||||
|
*
|
||||||
|
* @default 300 seconds
|
||||||
|
*/
|
||||||
|
pingKeepAlive: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gossip sub specific keep alive interval in seconds.
|
||||||
|
*
|
||||||
|
* @default 300 seconds
|
||||||
|
*/
|
||||||
|
relayKeepAlive: number;
|
||||||
|
};
|
||||||
|
|
||||||
export enum EPeersByDiscoveryEvents {
|
export enum EPeersByDiscoveryEvents {
|
||||||
PEER_DISCOVERY_BOOTSTRAP = "peer:discovery:bootstrap",
|
PEER_DISCOVERY_BOOTSTRAP = "peer:discovery:bootstrap",
|
||||||
|
@ -12,7 +12,6 @@ export * from "./sender.js";
|
|||||||
export * from "./receiver.js";
|
export * from "./receiver.js";
|
||||||
export * from "./misc.js";
|
export * from "./misc.js";
|
||||||
export * from "./libp2p.js";
|
export * from "./libp2p.js";
|
||||||
export * from "./keep_alive_manager.js";
|
|
||||||
export * from "./dns_discovery.js";
|
export * from "./dns_discovery.js";
|
||||||
export * from "./metadata.js";
|
export * from "./metadata.js";
|
||||||
export * from "./constants.js";
|
export * from "./constants.js";
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
export interface KeepAliveOptions {
|
|
||||||
pingKeepAlive: number;
|
|
||||||
relayKeepAlive: number;
|
|
||||||
}
|
|
@ -2,6 +2,7 @@ import type { Libp2p } from "@libp2p/interface";
|
|||||||
import type { PeerId } from "@libp2p/interface";
|
import type { PeerId } from "@libp2p/interface";
|
||||||
import type { Peer } from "@libp2p/interface";
|
import type { Peer } from "@libp2p/interface";
|
||||||
|
|
||||||
|
import type { ConnectionManagerOptions } from "./connection_manager.js";
|
||||||
import type { CreateLibp2pOptions } from "./libp2p.js";
|
import type { CreateLibp2pOptions } from "./libp2p.js";
|
||||||
import type { IDecodedMessage } from "./message.js";
|
import type { IDecodedMessage } from "./message.js";
|
||||||
import { ThisAndThat, ThisOrThat } from "./misc.js";
|
import { ThisAndThat, ThisOrThat } from "./misc.js";
|
||||||
@ -58,6 +59,7 @@ export type ProtocolCreateOptions = {
|
|||||||
* See [Waku v2 Topic Usage Recommendations](https://github.com/vacp2p/rfc-index/blob/main/waku/informational/23/topics.md#content-topics) for details.
|
* See [Waku v2 Topic Usage Recommendations](https://github.com/vacp2p/rfc-index/blob/main/waku/informational/23/topics.md#content-topics) for details.
|
||||||
* You cannot add or remove content topics after initialization of the node.
|
* You cannot add or remove content topics after initialization of the node.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration for determining the network in use.
|
* Configuration for determining the network in use.
|
||||||
* Network configuration refers to the shards and clusters used in the network.
|
* Network configuration refers to the shards and clusters used in the network.
|
||||||
@ -76,6 +78,7 @@ export type ProtocolCreateOptions = {
|
|||||||
* @default { clusterId: 1, shards: [0, 1, 2, 3, 4, 5, 6, 7] }
|
* @default { clusterId: 1, shards: [0, 1, 2, 3, 4, 5, 6, 7] }
|
||||||
*/
|
*/
|
||||||
networkConfig?: NetworkConfig;
|
networkConfig?: NetworkConfig;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* You can pass options to the `Libp2p` instance used by {@link @waku/sdk!WakuNode} using the `libp2p` property.
|
* You can pass options to the `Libp2p` instance used by {@link @waku/sdk!WakuNode} using the `libp2p` property.
|
||||||
* This property is the same type as the one passed to [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
|
* This property is the same type as the one passed to [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
|
||||||
@ -84,28 +87,38 @@ export type ProtocolCreateOptions = {
|
|||||||
* Notes that some values are overridden by {@link @waku/sdk!WakuNode} to ensure it implements the Waku protocol.
|
* Notes that some values are overridden by {@link @waku/sdk!WakuNode} to ensure it implements the Waku protocol.
|
||||||
*/
|
*/
|
||||||
libp2p?: Partial<CreateLibp2pOptions>;
|
libp2p?: Partial<CreateLibp2pOptions>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Number of peers to connect to, for the usage of the protocol.
|
* Number of peers to connect to, for the usage of the protocol.
|
||||||
* This is used by:
|
* This is used by:
|
||||||
* - Light Push to send messages,
|
* - Light Push to send messages,
|
||||||
* - Filter to retrieve messages.
|
* - Filter to retrieve messages.
|
||||||
* Defaults to 2.
|
*
|
||||||
|
* @default 2.
|
||||||
*/
|
*/
|
||||||
numPeersToUse?: number;
|
numPeersToUse?: number;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Byte array used as key for the noise protocol used for connection encryption
|
* Byte array used as key for the noise protocol used for connection encryption
|
||||||
* by [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
|
* by [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
|
||||||
* This is only used for test purposes to not run out of entropy during CI runs.
|
* This is only used for test purposes to not run out of entropy during CI runs.
|
||||||
*/
|
*/
|
||||||
staticNoiseKey?: Uint8Array;
|
staticNoiseKey?: Uint8Array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use recommended bootstrap method to discovery and connect to new nodes.
|
* Use recommended bootstrap method to discovery and connect to new nodes.
|
||||||
*/
|
*/
|
||||||
defaultBootstrap?: boolean;
|
defaultBootstrap?: boolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List of peers to use to bootstrap the node. Ignored if defaultBootstrap is set to true.
|
* List of peers to use to bootstrap the node. Ignored if defaultBootstrap is set to true.
|
||||||
*/
|
*/
|
||||||
bootstrapPeers?: string[];
|
bootstrapPeers?: string[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration for connection manager. If not specified - default values are applied.
|
||||||
|
*/
|
||||||
|
connectionManager?: Partial<ConnectionManagerOptions>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Callback<T extends IDecodedMessage> = (
|
export type Callback<T extends IDecodedMessage> = (
|
||||||
|
@ -23,28 +23,12 @@ import { ReliabilityMonitorManager } from "../reliability_monitor/index.js";
|
|||||||
|
|
||||||
import { waitForRemotePeer } from "./wait_for_remote_peer.js";
|
import { waitForRemotePeer } from "./wait_for_remote_peer.js";
|
||||||
|
|
||||||
export const DefaultPingKeepAliveValueSecs = 5 * 60;
|
|
||||||
export const DefaultRelayKeepAliveValueSecs = 5 * 60;
|
|
||||||
export const DefaultUserAgent = "js-waku";
|
export const DefaultUserAgent = "js-waku";
|
||||||
export const DefaultPingMaxInboundStreams = 10;
|
export const DefaultPingMaxInboundStreams = 10;
|
||||||
|
|
||||||
const log = new Logger("waku");
|
const log = new Logger("waku");
|
||||||
|
|
||||||
export interface WakuOptions {
|
export interface WakuOptions {
|
||||||
/**
|
|
||||||
* Set keep alive frequency in seconds: Waku will send a `/ipfs/ping/1.0.0`
|
|
||||||
* request to each peer after the set number of seconds. Set to 0 to disable.
|
|
||||||
*
|
|
||||||
* @default {@link @waku/core.DefaultPingKeepAliveValueSecs}
|
|
||||||
*/
|
|
||||||
pingKeepAlive?: number;
|
|
||||||
/**
|
|
||||||
* Set keep alive frequency in seconds: Waku will send a ping message over
|
|
||||||
* relay to each peer after the set number of seconds. Set to 0 to disable.
|
|
||||||
*
|
|
||||||
* @default {@link @waku/core.DefaultRelayKeepAliveValueSecs}
|
|
||||||
*/
|
|
||||||
relayKeepAlive?: number;
|
|
||||||
/**
|
/**
|
||||||
* Set the user agent string to be used in identification of the node.
|
* Set the user agent string to be used in identification of the node.
|
||||||
* @default {@link @waku/core.DefaultUserAgent}
|
* @default {@link @waku/core.DefaultUserAgent}
|
||||||
@ -87,19 +71,13 @@ export class WakuNode implements IWaku {
|
|||||||
...protocolsEnabled
|
...protocolsEnabled
|
||||||
};
|
};
|
||||||
|
|
||||||
const pingKeepAlive =
|
|
||||||
options.pingKeepAlive || DefaultPingKeepAliveValueSecs;
|
|
||||||
const relayKeepAlive = this.relay
|
|
||||||
? options.relayKeepAlive || DefaultRelayKeepAliveValueSecs
|
|
||||||
: 0;
|
|
||||||
|
|
||||||
const peerId = this.libp2p.peerId.toString();
|
const peerId = this.libp2p.peerId.toString();
|
||||||
|
|
||||||
this.connectionManager = new ConnectionManager({
|
this.connectionManager = new ConnectionManager({
|
||||||
libp2p,
|
libp2p,
|
||||||
keepAliveOptions: { pingKeepAlive, relayKeepAlive },
|
relay: this.relay,
|
||||||
pubsubTopics: this.pubsubTopics,
|
pubsubTopics: this.pubsubTopics,
|
||||||
relay: this.relay
|
config: options?.connectionManager
|
||||||
});
|
});
|
||||||
|
|
||||||
this.health = getHealthManager();
|
this.health = getHealthManager();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user