Skip to content

Commit

Permalink
Only sign and push signed data when the API request is success
Browse files Browse the repository at this point in the history
  • Loading branch information
Siegrift committed Dec 8, 2023
1 parent 60e8800 commit c5a6f8d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
4 changes: 2 additions & 2 deletions packages/airnode-feed/src/api-requests/data-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const callApi = async (
);
};

export const makeTemplateRequests = async (signedApiUpdate: SignedApiUpdate): Promise<TemplateResponse[]> => {
export const makeTemplateRequests = async (signedApiUpdate: SignedApiUpdate): Promise<TemplateResponse[] | null> => {
const {
config: { endpoints, templates, ois: oises, apiCredentials },
} = getState();
Expand Down Expand Up @@ -72,7 +72,7 @@ export const makeTemplateRequests = async (signedApiUpdate: SignedApiUpdate): Pr
operationTemplateId,
errorMessage: goCallApi.error.message,
});
return [];
return null;
}
const apiCallResponse = goCallApi.data;

Expand Down
50 changes: 26 additions & 24 deletions packages/airnode-feed/src/fetch-beacon-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,32 @@ const initiateSignedApiUpdateLoop = async (signedApiUpdate: SignedApiUpdate) =>
logger.debug('Making template requests.');
const templateResponses = await makeTemplateRequests(signedApiUpdate);

logger.debug('Signing template responses.');
const signedResponses = await signTemplateResponses(templateResponses);

logger.debug('Putting signed responses to storage.');
await Promise.all(
signedResponses.map(async ([templateId, signedResponse]) => {
const goPut = await go(() => templateValues[templateId]!.put(signedResponse));
if (!goPut.success) {
// Because there can be multiple triggers for the same template ID it is possible that a race condition
// occurs, where the (newer) response from a different trigger is put first. This throws, because the signed
// data must be inserted increasingly by timestamp.
logger.debug(`Could not put signed response.`, {
templateId,
signedResponse,
errorMessage: goPut.error.message,
});
}
})
);

// We want to send the data to the Signed API "in background" without waiting for the response to avoid blocking the
// fetch interval loop.
logger.debug('Scheduling pushing signed data to the API.');
schedulePushingSignedData(signedApiUpdate);
if (templateResponses) {
logger.debug('Signing template responses.');
const signedResponses = await signTemplateResponses(templateResponses);

logger.debug('Putting signed responses to storage.');
await Promise.all(
signedResponses.map(async ([templateId, signedResponse]) => {
const goPut = await go(() => templateValues[templateId]!.put(signedResponse));
if (!goPut.success) {
// Because there can be multiple triggers for the same template ID it is possible that a race condition
// occurs, where the (newer) response from a different trigger is put first. This throws, because the signed
// data must be inserted increasingly by timestamp.
logger.debug(`Could not put signed response.`, {
templateId,
signedResponse,
errorMessage: goPut.error.message,
});
}
})
);

// We want to send the data to the Signed API "in background" without waiting for the response to avoid blocking the
// fetch interval loop.
logger.debug('Scheduling pushing signed data to the API.');
schedulePushingSignedData(signedApiUpdate);
}

const duration = Date.now() - startTimestamp;
// Take at most 10% of the fetch interval as extra time to avoid all API requests be done at the same time. This
Expand Down

0 comments on commit c5a6f8d

Please sign in to comment.