Skip to content

Commit

Permalink
error handling for read ETIMEDOUT error
Browse files Browse the repository at this point in the history
  • Loading branch information
Milos Stanisavljevic committed Dec 25, 2023
1 parent 0b0f104 commit e3d7aa8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 42 deletions.
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);
}

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 {
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 {
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 {
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

0 comments on commit e3d7aa8

Please sign in to comment.