mirror of
https://github.com/waku-org/js-waku.git
synced 2025-02-22 09:08:19 +00:00
reform connection manager configurations
This commit is contained in:
parent
7efbd26609
commit
a6dd49974c
@ -10,7 +10,6 @@ import {
|
||||
IConnectionStateEvents,
|
||||
IPeersByDiscoveryEvents,
|
||||
IRelay,
|
||||
KeepAliveOptions,
|
||||
PeersByDiscoveryResult,
|
||||
PubsubTopic,
|
||||
ShardInfo
|
||||
@ -23,13 +22,15 @@ import { KeepAliveManager } from "./keep_alive_manager.js";
|
||||
|
||||
const log = new Logger("connection-manager");
|
||||
|
||||
export const DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED = 1;
|
||||
export const DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER = 3;
|
||||
export const DEFAULT_MAX_PARALLEL_DIALS = 3;
|
||||
const DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED = 1;
|
||||
const DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER = 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 = {
|
||||
libp2p: Libp2p;
|
||||
keepAliveOptions: KeepAliveOptions;
|
||||
pubsubTopics: PubsubTopic[];
|
||||
relay?: IRelay;
|
||||
config?: Partial<ConnectionManagerOptions>;
|
||||
@ -151,13 +152,18 @@ export class ConnectionManager
|
||||
maxDialAttemptsForPeer: DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER,
|
||||
maxBootstrapPeersAllowed: DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED,
|
||||
maxParallelDials: DEFAULT_MAX_PARALLEL_DIALS,
|
||||
pingKeepAlive: DEFAULT_PING_KEEP_ALIVE_SEC,
|
||||
relayKeepAlive: DEFAULT_RELAY_KEEP_ALIVE_SEC,
|
||||
...options.config
|
||||
};
|
||||
|
||||
this.keepAliveManager = new KeepAliveManager({
|
||||
relay: options.relay,
|
||||
libp2p: options.libp2p,
|
||||
options: options.keepAliveOptions
|
||||
options: {
|
||||
pingKeepAlive: this.options.pingKeepAlive,
|
||||
relayKeepAlive: this.options.relayKeepAlive
|
||||
}
|
||||
});
|
||||
|
||||
this.startEventListeners()
|
||||
|
@ -1,14 +1,18 @@
|
||||
import type { PeerId } from "@libp2p/interface";
|
||||
import type { IRelay, Libp2p, PeerIdStr } from "@waku/interfaces";
|
||||
import type { KeepAliveOptions } from "@waku/interfaces";
|
||||
import { Logger, pubsubTopicToSingleShardInfo } from "@waku/utils";
|
||||
import { utf8ToBytes } from "@waku/utils/bytes";
|
||||
|
||||
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");
|
||||
|
||||
type KeepAliveOptions = {
|
||||
pingKeepAlive: number;
|
||||
relayKeepAlive: number;
|
||||
};
|
||||
|
||||
type CreateKeepAliveManagerOptions = {
|
||||
options: KeepAliveOptions;
|
||||
libp2p: Libp2p;
|
||||
|
@ -8,22 +8,44 @@ export enum Tags {
|
||||
LOCAL = "local-peer-cache"
|
||||
}
|
||||
|
||||
export interface ConnectionManagerOptions {
|
||||
export type ConnectionManagerOptions = {
|
||||
/**
|
||||
* 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
|
||||
* 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.
|
||||
*
|
||||
* @default 3
|
||||
*/
|
||||
maxDialAttemptsForPeer: number;
|
||||
|
||||
/**
|
||||
* 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)
|
||||
* 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).
|
||||
*
|
||||
* @default 1
|
||||
*/
|
||||
maxBootstrapPeersAllowed: number;
|
||||
|
||||
/**
|
||||
* Max number of parallel dials allowed
|
||||
* Max number of parallel dials allowed.
|
||||
*
|
||||
* @default 3
|
||||
*/
|
||||
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 {
|
||||
PEER_DISCOVERY_BOOTSTRAP = "peer:discovery:bootstrap",
|
||||
|
@ -12,7 +12,6 @@ export * from "./sender.js";
|
||||
export * from "./receiver.js";
|
||||
export * from "./misc.js";
|
||||
export * from "./libp2p.js";
|
||||
export * from "./keep_alive_manager.js";
|
||||
export * from "./dns_discovery.js";
|
||||
export * from "./metadata.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 { Peer } from "@libp2p/interface";
|
||||
|
||||
import type { ConnectionManagerOptions } from "./connection_manager.js";
|
||||
import type { CreateLibp2pOptions } from "./libp2p.js";
|
||||
import type { IDecodedMessage } from "./message.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.
|
||||
* You cannot add or remove content topics after initialization of the node.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Configuration for determining the network in use.
|
||||
* 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] }
|
||||
*/
|
||||
networkConfig?: NetworkConfig;
|
||||
|
||||
/**
|
||||
* 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)
|
||||
@ -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.
|
||||
*/
|
||||
libp2p?: Partial<CreateLibp2pOptions>;
|
||||
|
||||
/**
|
||||
* Number of peers to connect to, for the usage of the protocol.
|
||||
* This is used by:
|
||||
* - Light Push to send messages,
|
||||
* - Filter to retrieve messages.
|
||||
* Defaults to 2.
|
||||
*
|
||||
* @default 2.
|
||||
*/
|
||||
numPeersToUse?: number;
|
||||
|
||||
/**
|
||||
* 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)
|
||||
* This is only used for test purposes to not run out of entropy during CI runs.
|
||||
*/
|
||||
staticNoiseKey?: Uint8Array;
|
||||
|
||||
/**
|
||||
* Use recommended bootstrap method to discovery and connect to new nodes.
|
||||
*/
|
||||
defaultBootstrap?: boolean;
|
||||
|
||||
/**
|
||||
* List of peers to use to bootstrap the node. Ignored if defaultBootstrap is set to true.
|
||||
*/
|
||||
bootstrapPeers?: string[];
|
||||
|
||||
/**
|
||||
* Configuration for connection manager. If not specified - default values are applied.
|
||||
*/
|
||||
connectionManager?: Partial<ConnectionManagerOptions>;
|
||||
};
|
||||
|
||||
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";
|
||||
|
||||
export const DefaultPingKeepAliveValueSecs = 5 * 60;
|
||||
export const DefaultRelayKeepAliveValueSecs = 5 * 60;
|
||||
export const DefaultUserAgent = "js-waku";
|
||||
export const DefaultPingMaxInboundStreams = 10;
|
||||
|
||||
const log = new Logger("waku");
|
||||
|
||||
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.
|
||||
* @default {@link @waku/core.DefaultUserAgent}
|
||||
@ -87,19 +71,13 @@ export class WakuNode implements IWaku {
|
||||
...protocolsEnabled
|
||||
};
|
||||
|
||||
const pingKeepAlive =
|
||||
options.pingKeepAlive || DefaultPingKeepAliveValueSecs;
|
||||
const relayKeepAlive = this.relay
|
||||
? options.relayKeepAlive || DefaultRelayKeepAliveValueSecs
|
||||
: 0;
|
||||
|
||||
const peerId = this.libp2p.peerId.toString();
|
||||
|
||||
this.connectionManager = new ConnectionManager({
|
||||
libp2p,
|
||||
keepAliveOptions: { pingKeepAlive, relayKeepAlive },
|
||||
relay: this.relay,
|
||||
pubsubTopics: this.pubsubTopics,
|
||||
relay: this.relay
|
||||
config: options?.connectionManager
|
||||
});
|
||||
|
||||
this.health = getHealthManager();
|
||||
|
Loading…
x
Reference in New Issue
Block a user