Skip to content

fix: network message timeout #3106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: v6/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@
"rolling-rate-limiter": "^0.2.13",
"semver": "^7.5.2",
"sequelize": "^6.29.0",
"timeout-abort-controller": "^3.0.0",
"toobusy-js": "^0.5.1",
"uint8arrays": "^3.1.0",
"umzug": "^3.2.1",
Expand Down
163 changes: 73 additions & 90 deletions src/modules/network/implementation/libp2p-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { InMemoryRateLimiter } from 'rolling-rate-limiter';
import toobusy from 'toobusy-js';
import { mkdir, writeFile, readFile, stat } from 'fs/promises';
import ip from 'ip';
import { TimeoutController } from 'timeout-abort-controller';
import {
NETWORK_API_RATE_LIMIT,
NETWORK_API_SPAM_DETECTION,
Expand Down Expand Up @@ -340,116 +339,100 @@ class Libp2pService {
.map((addr) => addr.toString().split('/'))
.filter((splittedAddr) => !ip.isPrivate(splittedAddr[2]))[0]?.[2];

this.logger.trace(
`Dialing remotePeerId: ${peerIdString} with public ip: ${publicIp}: protocol: ${protocol}, messageType: ${messageType} , operationId: ${operationId}`,
);
let dialResult;
let dialStart;
let dialEnd;
try {
dialStart = Date.now();
dialResult = await this.node.dialProtocol(peerIdObject, protocol);
dialEnd = Date.now();
} catch (error) {
dialEnd = Date.now();
nackMessage.data.errorMessage = `Unable to dial peer: ${peerIdString}. protocol: ${protocol}, messageType: ${messageType} , operationId: ${operationId}, dial execution time: ${
dialEnd - dialStart
} ms. Error: ${error.message}`;
const timeoutSignal = AbortSignal.timeout(timeout);
const abortError = new Error('Message timed out');

return nackMessage;
}
this.logger.trace(
`Created stream for peer: ${peerIdString}. protocol: ${protocol}, messageType: ${messageType} , operationId: ${operationId}, dial execution time: ${
dialEnd - dialStart
} ms.`,
);

const { stream } = dialResult;
const abortPromise = new Promise((_, reject) => {
timeoutSignal.onabort = () => reject(abortError);
});
let stream;
let response;
let errorMessage;
let operationStart;
let operationEnd;
const onAbort = () => {
if (stream) stream.abort(abortError);
response = null;
};

this.updateSessionStream(operationId, keywordUuid, peerIdString, stream);
try {
timeoutSignal.addEventListener('abort', onAbort, { once: true });
this.logger.trace(
`Dialing remotePeerId: ${peerIdString} with public ip: ${publicIp}: protocol: ${protocol}, messageType: ${messageType} , operationId: ${operationId}`,
);

const streamMessage = this.createStreamMessage(
message,
operationId,
keywordUuid,
messageType,
);
errorMessage = `dial`;
operationStart = Date.now();
const dialPromise = this.node.dialProtocol(peerIdObject, protocol, {
signal: timeoutSignal,
});
const dialResult = await Promise.race([dialPromise, abortPromise]);
operationEnd = Date.now();
if (timeoutSignal.aborted) {
throw abortError;
}

this.logger.trace(
`Sending message to ${peerIdString}. protocol: ${protocol}, messageType: ${messageType}, operationId: ${operationId}`,
);
this.logger.trace(
`Created stream for peer: ${peerIdString}. protocol: ${protocol}, messageType: ${messageType} , operationId: ${operationId}, dial execution time: ${
operationEnd - operationStart
} ms.`,
);

let sendMessageStart;
let sendMessageEnd;
try {
sendMessageStart = Date.now();
await this._sendMessageToStream(stream, streamMessage);
sendMessageEnd = Date.now();
} catch (error) {
sendMessageEnd = Date.now();
nackMessage.data.errorMessage = `Unable to send message to peer: ${peerIdString}. protocol: ${protocol}, messageType: ${messageType}, operationId: ${operationId}, execution time: ${
sendMessageEnd - sendMessageStart
} ms. Error: ${error.message}`;
stream = dialResult.stream;

return nackMessage;
}
this.updateSessionStream(operationId, keywordUuid, peerIdString, stream);

let readResponseStart;
let readResponseEnd;
let response;
const timeoutController = new TimeoutController(timeout);
try {
readResponseStart = Date.now();
const streamMessage = this.createStreamMessage(
message,
operationId,
keywordUuid,
messageType,
);

timeoutController.signal.addEventListener(
'abort',
async () => {
stream.abort();
response = null;
},
{ once: true },
this.logger.trace(
`Sending message to ${peerIdString}. protocol: ${protocol}, messageType: ${messageType}, operationId: ${operationId}`,
);

errorMessage = `send message`;
operationStart = Date.now();
await this._sendMessageToStream(stream, streamMessage);
operationEnd = Date.now();
if (timeoutSignal.aborted) {
throw abortError;
}

errorMessage = `read response`;
operationStart = Date.now();
response = await this._readMessageFromStream(
stream,
this.isResponseValid.bind(this),
peerIdString,
);

if (timeoutController.signal.aborted) {
throw Error('Message timed out!');
operationEnd = Date.now();
if (timeoutSignal.aborted) {
throw abortError;
}

timeoutController.signal.removeEventListener('abort');
timeoutController.clear();
this.logger.trace(
`Receiving response from ${peerIdString}. protocol: ${protocol}, messageType: ${
response.message?.header?.messageType
}, operationId: ${operationId}, execution time: ${
operationEnd - operationStart
} ms.`,
);

readResponseEnd = Date.now();
if (!response.valid) {
nackMessage.data.errorMessage = 'Invalid response';
return nackMessage;
}
} catch (error) {
timeoutController.signal.removeEventListener('abort');
timeoutController.clear();

readResponseEnd = Date.now();
nackMessage.data.errorMessage = `Unable to read response from peer ${peerIdString}. protocol: ${protocol}, messageType: ${messageType} , operationId: ${operationId}, execution time: ${
readResponseEnd - readResponseStart
} ms. Error: ${error.message}`;

return nackMessage;
}

this.logger.trace(
`Receiving response from ${peerIdString}. protocol: ${protocol}, messageType: ${
response.message?.header?.messageType
}, operationId: ${operationId}, execution time: ${
readResponseEnd - readResponseStart
} ms.`,
);

if (!response.valid) {
nackMessage.data.errorMessage = 'Invalid response';

nackMessage.data.errorMessage = `Unable to ${errorMessage} from peer ${peerIdString}. protocol: ${protocol}, messageType: ${messageType} , operationId: ${operationId}. Execution time: ${
(operationEnd ?? Date.now()) - operationStart
} ms. Error: ${error.message.slice(0, 145)}`;
return nackMessage;
} finally {
timeoutSignal.removeEventListener('abort', onAbort);
}

return response.message;
}

Expand Down
Loading