From 5d6457168578e1dd9c286705ba74f8485e1ac192 Mon Sep 17 00:00:00 2001 From: Ishan Date: Fri, 13 Sep 2019 18:10:33 +0200 Subject: [PATCH 1/2] Add update status job for peer info in peer pool --- elements/lisk-p2p/src/p2p.ts | 4 ++++ elements/lisk-p2p/src/p2p_types.ts | 1 + elements/lisk-p2p/src/peer_pool.ts | 22 ++++++++++++++++++++++ elements/lisk-p2p/test/unit/peer_pool.ts | 2 ++ 4 files changed, 29 insertions(+) diff --git a/elements/lisk-p2p/src/p2p.ts b/elements/lisk-p2p/src/p2p.ts index a6f1d80bf5a..822acb00c6c 100644 --- a/elements/lisk-p2p/src/p2p.ts +++ b/elements/lisk-p2p/src/p2p.ts @@ -135,6 +135,7 @@ const BASE_10_RADIX = 10; export const DEFAULT_MAX_OUTBOUND_CONNECTIONS = 20; export const DEFAULT_MAX_INBOUND_CONNECTIONS = 100; export const DEFAULT_OUTBOUND_SHUFFLE_INTERVAL = 300000; +export const DEFAULT_OUTBOUND_UPDATE_STATUS_INTERVAL = 2000; export const DEFAULT_PEER_PROTECTION_FOR_NETGROUP = 0.034; export const DEFAULT_PEER_PROTECTION_FOR_LATENCY = 0.068; export const DEFAULT_PEER_PROTECTION_FOR_USEFULNESS = 0.068; @@ -448,6 +449,9 @@ export class P2P extends EventEmitter { outboundShuffleInterval: config.outboundShuffleInterval ? config.outboundShuffleInterval : DEFAULT_OUTBOUND_SHUFFLE_INTERVAL, + outboundUpdateStatusInterval: config.outboundUpdateStatusInterval + ? config.outboundUpdateStatusInterval + : DEFAULT_OUTBOUND_UPDATE_STATUS_INTERVAL, netgroupProtectionRatio: typeof config.netgroupProtectionRatio === 'number' ? config.netgroupProtectionRatio diff --git a/elements/lisk-p2p/src/p2p_types.ts b/elements/lisk-p2p/src/p2p_types.ts index 508d3560099..484039b2ef4 100644 --- a/elements/lisk-p2p/src/p2p_types.ts +++ b/elements/lisk-p2p/src/p2p_types.ts @@ -98,6 +98,7 @@ export interface P2PConfig { readonly peerBanTime?: number; readonly sendPeerLimit?: number; readonly outboundShuffleInterval?: number; + readonly outboundUpdateStatusInterval?: number; readonly latencyProtectionRatio?: number; readonly productivityProtectionRatio?: number; readonly longevityProtectionRatio?: number; diff --git a/elements/lisk-p2p/src/peer_pool.ts b/elements/lisk-p2p/src/peer_pool.ts index a0bdaf8d7f3..c2edf222112 100644 --- a/elements/lisk-p2p/src/peer_pool.ts +++ b/elements/lisk-p2p/src/peer_pool.ts @@ -99,6 +99,7 @@ interface PeerPoolConfig { readonly sendPeerLimit: number; readonly peerBanTime: number; readonly maxOutboundConnections: number; + readonly outboundUpdateStatusInterval?: number; readonly maxInboundConnections: number; readonly maxPeerDiscoveryResponseLength: number; readonly outboundShuffleInterval: number; @@ -191,6 +192,7 @@ export class PeerPool extends EventEmitter { private readonly _peerSelectForConnection: P2PPeerSelectionForConnectionFunction; private readonly _sendPeerLimit: number; private readonly _outboundShuffleIntervalId: NodeJS.Timer | undefined; + private readonly _outboundUpdateStatusId: number; private readonly _peerConfig: PeerConfig; private readonly _peerLists: PeerLists; @@ -217,6 +219,13 @@ export class PeerPool extends EventEmitter { this._maxOutboundConnections = peerPoolConfig.maxOutboundConnections; this._maxInboundConnections = peerPoolConfig.maxInboundConnections; this._sendPeerLimit = peerPoolConfig.sendPeerLimit; + this._outboundUpdateStatusId = setInterval(() => { + // tslint:disable-next-line: no-floating-promises + (async () => { + await this._updateOutboundConnections(); + })().catch(error => error); + }, peerPoolConfig.outboundUpdateStatusInterval); + this._outboundShuffleIntervalId = setInterval(() => { this._evictPeer(OutboundPeer); }, peerPoolConfig.outboundShuffleInterval); @@ -513,6 +522,7 @@ export class PeerPool extends EventEmitter { // Clear periodic eviction of outbound peers for shuffling if (this._outboundShuffleIntervalId) { clearInterval(this._outboundShuffleIntervalId); + clearInterval(this._outboundUpdateStatusId); } this._peerMap.forEach((peer: Peer) => { @@ -604,6 +614,18 @@ export class PeerPool extends EventEmitter { })(); } + private async _updateOutboundConnections(): Promise { + try { + await Promise.all( + this.getConnectedPeers(OutboundPeer).map(async peer => + peer.fetchStatus().catch(err => err), + ), + ); + } catch (err) { + return; + } + } + private _selectPeersForEviction(): Peer[] { const peers = [...this.getPeers(InboundPeer)].filter(peer => this._peerLists.whitelisted.every( diff --git a/elements/lisk-p2p/test/unit/peer_pool.ts b/elements/lisk-p2p/test/unit/peer_pool.ts index ea6d7ded89e..03e5b18e8ef 100644 --- a/elements/lisk-p2p/test/unit/peer_pool.ts +++ b/elements/lisk-p2p/test/unit/peer_pool.ts @@ -33,6 +33,7 @@ import { DEFAULT_PEER_PROTECTION_FOR_LATENCY, DEFAULT_PEER_PROTECTION_FOR_USEFULNESS, DEFAULT_PEER_PROTECTION_FOR_LONGEVITY, + DEFAULT_OUTBOUND_UPDATE_STATUS_INTERVAL, DEFAULT_RANDOM_SECRET, } from '../../src/p2p'; @@ -50,6 +51,7 @@ describe('peerPool', () => { maxOutboundConnections: DEFAULT_MAX_OUTBOUND_CONNECTIONS, maxInboundConnections: DEFAULT_MAX_INBOUND_CONNECTIONS, outboundShuffleInterval: DEFAULT_OUTBOUND_SHUFFLE_INTERVAL, + outboundUpdateStatusInterval: DEFAULT_OUTBOUND_UPDATE_STATUS_INTERVAL, netgroupProtectionRatio: DEFAULT_PEER_PROTECTION_FOR_NETGROUP, latencyProtectionRatio: DEFAULT_PEER_PROTECTION_FOR_LATENCY, productivityProtectionRatio: DEFAULT_PEER_PROTECTION_FOR_USEFULNESS, From d42a4382617c23522a701d7fa17ce2b183d3db05 Mon Sep 17 00:00:00 2001 From: Shusetsu Toda Date: Mon, 16 Sep 2019 09:35:06 +0200 Subject: [PATCH 2/2] :bug: Fix outdated max value for connection --- sdk/src/samples/config_devnet.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/src/samples/config_devnet.json b/sdk/src/samples/config_devnet.json index 70cb46b1d89..b53bbf3d680 100644 --- a/sdk/src/samples/config_devnet.json +++ b/sdk/src/samples/config_devnet.json @@ -38,7 +38,7 @@ "user": "lisk", "password": "password", "min": 10, - "max": 95, + "max": 25, "poolIdleTimeout": 30000, "reapIntervalMillis": 1000, "logEvents": ["error"]