Skip to content
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

error handling for read ETIMEDOUT error #2846

Open
wants to merge 13 commits into
base: v6/develop
Choose a base branch
from
49 changes: 27 additions & 22 deletions src/controllers/rpc/publish-rpc-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,35 @@ class PublishController extends BaseController {
const command = { sequence: [], delay: 0, transactional: false, data: {} };
let dataSource;
const [handleInitCommand, handleRequestCommand] = this.getCommandSequence(protocol);
switch (messageType) {
case NETWORK_MESSAGE_TYPES.REQUESTS.PROTOCOL_INIT:
dataSource = message.data;
command.name = handleInitCommand;
command.period = 5000;
command.retries = 3;
try {
switch (messageType) {
case NETWORK_MESSAGE_TYPES.REQUESTS.PROTOCOL_INIT:
dataSource = message.data;
command.name = handleInitCommand;
command.period = 5000;
command.retries = 3;

break;
case NETWORK_MESSAGE_TYPES.REQUESTS.PROTOCOL_REQUEST:
// eslint-disable-next-line no-case-declarations
dataSource = await this.operationIdService.getCachedOperationIdData(operationId);
await this.operationIdService.cacheOperationIdData(operationId, {
assertionId: dataSource.assertionId,
assertion: message.data.assertion,
});
command.name = handleRequestCommand;
command.data.keyword = message.data.keyword;
command.data.agreementId = dataSource.agreementId;
command.data.agreementData = dataSource.agreementData;
break;
default:
throw Error('unknown message type');
break;
case NETWORK_MESSAGE_TYPES.REQUESTS.PROTOCOL_REQUEST:
// eslint-disable-next-line no-case-declarations
dataSource = await this.operationIdService.getCachedOperationIdData(
operationId,
);
await this.operationIdService.cacheOperationIdData(operationId, {
assertionId: dataSource.assertionId,
assertion: message.data.assertion,
});
command.name = handleRequestCommand;
command.data.keyword = message.data.keyword;
command.data.agreementId = dataSource.agreementId;
command.data.agreementData = dataSource.agreementData;
break;
default:
throw Error('unknown message type');
}
} catch (error) {
this.logger.error(error.message);
milosstanisavljevic marked this conversation as resolved.
Show resolved Hide resolved
}

command.data = {
...command.data,
remotePeerId,
Expand Down
16 changes: 10 additions & 6 deletions src/modules/network/implementation/libp2p-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,12 +632,16 @@ class Libp2pService {
}

removeCachedSession(operationId, keywordUuid, peerIdString) {
if (this.sessions[peerIdString]?.[operationId]?.[keywordUuid]?.stream) {
this.sessions[peerIdString][operationId][keywordUuid].stream.close();
delete this.sessions[peerIdString][operationId];
this.logger.trace(
`Removed session for remotePeerId: ${peerIdString}, operationId: ${operationId}.`,
);
try {
zeroxbt marked this conversation as resolved.
Show resolved Hide resolved
if (this.sessions[peerIdString]?.[operationId]?.[keywordUuid]?.stream) {
this.sessions[peerIdString][operationId][keywordUuid].stream.close();
delete this.sessions[peerIdString][operationId];
this.logger.trace(
`Removed session for remotePeerId: ${peerIdString}, operationId: ${operationId}.`,
);
}
} catch (error) {
this.logger.error(error.message);
}
}
}
Expand Down
35 changes: 21 additions & 14 deletions src/service/operation-id-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,31 @@ class OperationIdService {
}

async cacheOperationIdData(operationId, data) {
this.logger.debug(`Caching data for operation id: ${operationId} in file`);
const operationIdCachePath = this.fileService.getOperationIdCachePath();

await this.fileService.writeContentsToFile(
operationIdCachePath,
operationId,
JSON.stringify(data),
);

this.memoryCachedHandlersData[operationId] = { data, timestamp: Date.now() };
try {
milosstanisavljevic marked this conversation as resolved.
Show resolved Hide resolved
this.logger.debug(`Caching data for operation id: ${operationId} in file`);
const operationIdCachePath = this.fileService.getOperationIdCachePath();

await this.fileService.writeContentsToFile(
operationIdCachePath,
operationId,
JSON.stringify(data),
);

this.memoryCachedHandlersData[operationId] = { data, timestamp: Date.now() };
} catch (error) {
this.logger.error(error);
}
}

async getCachedOperationIdData(operationId) {
if (this.memoryCachedHandlersData[operationId]) {
this.logger.debug(`Reading operation id: ${operationId} cached data from memory`);
return this.memoryCachedHandlersData[operationId].data;
try {
zeroxbt marked this conversation as resolved.
Show resolved Hide resolved
if (this.memoryCachedHandlersData[operationId]) {
this.logger.debug(`Reading operation id: ${operationId} cached data from memory`);
return this.memoryCachedHandlersData[operationId].data;
}
} catch (error) {
this.logger.error(error);
}

this.logger.debug(`Reading operation id: ${operationId} cached data from file`);
const documentPath = this.fileService.getOperationIdDocumentPath(operationId);
let data;
Expand Down