Skip to content

Commit

Permalink
fix(core-p2p): reset maxPayload on ws message
Browse files Browse the repository at this point in the history
  • Loading branch information
air1one committed Dec 11, 2020
1 parent 021d18a commit f1bf195
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/core-p2p/src/constants.ts
@@ -1,5 +1,6 @@
export const constants = {
MAX_DOWNLOAD_BLOCKS: 400, // maximum number of blocks we can download at once
DEFAULT_MAX_PAYLOAD: 20 * 1024 * 1024, // default maxPayload value on the WS socket
DEFAULT_MAX_PAYLOAD: 20 * 1024 * 1024, // default maxPayload value on the server WS socket
DEFAULT_MAX_PAYLOAD_CLIENT: 100 * 1024, // default maxPayload value on the client WS socket
KILOBYTE: 1024,
};
2 changes: 1 addition & 1 deletion packages/core-p2p/src/peer-communicator.ts
Expand Up @@ -242,7 +242,7 @@ export class PeerCommunicator implements P2P.IPeerCommunicator {

const timeBeforeSocketCall: number = new Date().getTime();

maxPayload = maxPayload || 100 * constants.KILOBYTE; // 100KB by default, enough for most requests
maxPayload = maxPayload || constants.DEFAULT_MAX_PAYLOAD_CLIENT;
const connection: SCClientSocket = this.connector.connect(peer, maxPayload);
response = await socketEmit(
peer.ip,
Expand Down
8 changes: 8 additions & 0 deletions packages/core-p2p/src/peer-connector.ts
@@ -1,6 +1,7 @@
import { app } from "@arkecosystem/core-container";
import { P2P } from "@arkecosystem/core-interfaces";
import { create, SCClientSocket } from "socketcluster-client";
import { constants } from "./constants";
import { PeerRepository } from "./peer-repository";
import { codec } from "./utils/sc-codec";

Expand Down Expand Up @@ -89,6 +90,13 @@ export class PeerConnector implements P2P.IPeerConnector {
socket.on("ping", () => this.terminate(peer));
socket.on("pong", () => this.terminate(peer));
socket.on("message", data => {
if (data.length > 1000) {
// reset max payload to default after receiving a message
// (the 1k length condition helps discarding SC messages and also some of our own
// that would not need a max payload reset)
socket._receiver._maxPayload = constants.DEFAULT_MAX_PAYLOAD_CLIENT;
}

// this is to establish some rate limit on socket messages
// 30 messages per second is enough for socketcluster's + our own messages
const timeNow: number = new Date().getTime();
Expand Down

0 comments on commit f1bf195

Please sign in to comment.