Skip to content

Commit

Permalink
refactor(core-p2p): increase network timeouts (#2828)
Browse files Browse the repository at this point in the history
We don't need to be too eager to get the responses from peers in a few
seconds. In some cases we will start 25 "parallel" download jobs, each
one of which downloads 400 blocks which, if full, could account to a few
tens of megabytes per job.
  • Loading branch information
vasild authored and spkjp committed Jul 26, 2019
1 parent 55032f1 commit c882656
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
9 changes: 7 additions & 2 deletions packages/core-p2p/src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ export const defaults = {
*/
minimumNetworkReach: 20,
/**
* The timeout for requests to other peers
* The timeout to verify a peer. [milliseconds]
*/
globalTimeout: 5000,
verifyTimeout: 60000,
/**
* The timeout to download a batch of blocks (400). Notice that we start
* 25 concurrent such downloads, so the network may be saturated. [milliseconds]
*/
getBlocksTimeout: 600000,
/**
* The maximum number of peers we will broadcast data to
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/core-p2p/src/network-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class NetworkMonitor implements P2P.INetworkMonitor {
let max = peers.length;

let unresponsivePeers = 0;
const pingDelay = fast ? 1500 : app.resolveOptions("p2p").globalTimeout;
const pingDelay = fast ? 1500 : app.resolveOptions("p2p").verifyTimeout;

if (peerCount) {
peers = shuffle(peers).slice(0, peerCount);
Expand Down
5 changes: 2 additions & 3 deletions packages/core-p2p/src/peer-communicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,8 @@ export class PeerCommunicator implements P2P.IPeerCommunicator {
{
fromBlockHeight,
blockLimit,
timeoutMsec,
headersOnly,
}: { fromBlockHeight: number; blockLimit?: number; timeoutMsec?: number; headersOnly?: boolean },
}: { fromBlockHeight: number; blockLimit?: number; headersOnly?: boolean },
): Promise<Interfaces.IBlockData[]> {
const peerBlocks = await this.emit(
peer,
Expand All @@ -156,7 +155,7 @@ export class PeerCommunicator implements P2P.IPeerCommunicator {
"Content-Type": "application/json",
},
},
timeoutMsec || 10000,
app.resolveOptions("p2p").getBlocksTimeout,
);

if (!peerBlocks) {
Expand Down
6 changes: 6 additions & 0 deletions packages/core-p2p/src/peer-connector.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { app } from "@arkecosystem/core-container";
import { P2P } from "@arkecosystem/core-interfaces";
import { create, SCClientSocket } from "socketcluster-client";
import { PeerRepository } from "./peer-repository";
Expand All @@ -21,9 +22,14 @@ export class PeerConnector implements P2P.IPeerConnector {
return connection;
}

// https://socketcluster.io/#!/docs/api-socketcluster-client
connection = create({
port: peer.port,
hostname: peer.ip,
ackTimeout: Math.max(
app.resolveOptions("p2p").getBlocksTimeout,
app.resolveOptions("p2p").verifyTimeout
),
});

this.connections.set(peer.ip, connection);
Expand Down
2 changes: 1 addition & 1 deletion packages/core-p2p/src/peer-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class PeerProcessor implements P2P.IPeerProcessor {
try {
this.storage.setPendingPeer(peer);

await this.communicator.ping(newPeer, 3000);
await this.communicator.ping(newPeer, app.resolveOptions("p2p").verifyTimeout);

this.storage.setPeer(newPeer);

Expand Down
7 changes: 7 additions & 0 deletions packages/core-p2p/src/socket-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const startSocketServer = async (service: P2P.IPeerService, config: Recor
// when testing we also need to get socket files from dist folder
const relativeSocketPath = process.env.CORE_ENV === "test" ? "/../../dist/socket-server" : "";

// https://socketcluster.io/#!/docs/api-socketcluster
const server: SocketCluster = new SocketCluster({
...{
appName: "core-p2p",
Expand All @@ -21,6 +22,12 @@ export const startSocketServer = async (service: P2P.IPeerService, config: Recor
workerController: __dirname + `${relativeSocketPath}/worker.js`,
workers: 2,
wsEngine: "ws",
// See https://github.com/SocketCluster/socketcluster/issues/506 about
// details on how pingTimeout works.
pingTimeout: Math.max(
app.resolveOptions("p2p").getBlocksTimeout,
app.resolveOptions("p2p").verifyTimeout,
),
},
...config.server,
});
Expand Down

0 comments on commit c882656

Please sign in to comment.