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

Back Merge #2293

Merged
merged 9 commits into from
Jun 19, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import {
OutboundDocumentQueryReq,
OutboundDocumentRetrievalReq,
} from "@metriport/ihe-gateway-sdk";
import { sleep } from "@metriport/shared";
import { makeLambdaClient } from "../../aws/lambda";
import { Config } from "../../../util/config";
import { processAsyncError } from "../../../util/error/shared";
import { IHEGatewayV2 } from "./ihe-gateway-v2";
import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";

dayjs.extend(duration);

const SLEEP_IN_BETWEEN_DOCUMENT_RETRIEVAL_REQUESTS = dayjs.duration({ seconds: 1 });
const MAX_GATEWAYS_BEFORE_CHUNK = 1000;
const MAX_DOCUMENT_QUERY_REQUESTS_PER_INVOCATION = 20;
const MAX_DOCUMENT_RETRIEVAL_REQUESTS_PER_INVOCATION = 20;
Expand Down Expand Up @@ -108,9 +114,13 @@ export class IHEGatewayV2Async extends IHEGatewayV2 {
MAX_DOCUMENT_RETRIEVAL_REQUESTS_PER_INVOCATION
);

for (const chunk of requestChunks) {
for (const [i, chunk] of requestChunks.entries()) {
const params = { patientId, cxId, requestId, drRequestsGatewayV2: chunk };

if (i > 0) {
await sleep(SLEEP_IN_BETWEEN_DOCUMENT_RETRIEVAL_REQUESTS.asMilliseconds());
}

// intentionally not waiting
lambdaClient
.invoke({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function handleHttpErrorResponse({
gateway,
patientId: outboundRequest?.patientId,
patientMatch: null,
iheGatewayV2: true,
operationOutcome,
};
}
Expand Down Expand Up @@ -74,6 +75,7 @@ export function handlePatientErrorResponse({
gateway,
patientId: outboundRequest.patientId,
patientMatch: null,
iheGatewayV2: true,
operationOutcome,
};
return response;
Expand Down Expand Up @@ -108,6 +110,7 @@ export function handleSchemaErrorResponse({
gateway,
patientId: outboundRequest.patientId,
patientMatch: null,
iheGatewayV2: true,
operationOutcome,
};
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@ import https from "https";
import { constants } from "crypto";
import axios from "axios";
import * as AWS from "aws-sdk";
import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";
import { SamlCertsAndKeys } from "./security/types";
import { Config } from "../../../../util/config";
import { out } from "../../../../util/log";
import { MetriportError } from "../../../../util/error/metriport-error";
import { createMtomContentTypeAndPayload } from "../outbound/xca/mtom/builder";
import { executeWithNetworkRetries } from "@metriport/shared";

import {
parseMtomResponse,
getBoundaryFromMtomResponse,
MtomAttachments,
convertSoapResponseToMtomResponse,
} from "../outbound/xca/mtom/parser";

dayjs.extend(duration);

const { log } = out("Saml Client");
const timeout = 120000;
const httpTimeout = dayjs.duration({ seconds: 120 });
const initialDelay = dayjs.duration({ seconds: 3 });
const maxPayloadSize = Infinity;
let rejectUnauthorized = true;
let trustedStore: string | undefined = undefined;
async function getTrustedKeyStore(): Promise<string> {
Expand Down Expand Up @@ -84,7 +89,7 @@ export async function sendSignedXml({
const response = await executeWithNetworkRetries(
async () => {
return axios.post(url, signedXml, {
timeout: 120000,
timeout: httpTimeout.asMilliseconds(),
headers: {
"Content-Type": "application/soap+xml;charset=UTF-8",
Accept: "application/soap+xml",
Expand All @@ -94,7 +99,7 @@ export async function sendSignedXml({
});
},
{
initialDelay: 3000,
initialDelay: initialDelay.asMilliseconds(),
maxAttempts: 3,
//TODO: This introduces retry on timeout without needing to specify the http Code: https://github.com/metriport/metriport/pull/2285. Remove once PR is merged
httpCodesToRetry: ["ECONNREFUSED", "ECONNRESET", "ETIMEDOUT"],
Expand Down Expand Up @@ -129,21 +134,29 @@ export async function sendSignedXmlMtom({
const response = await executeWithNetworkRetries(
async () => {
return axios.post(url, payload, {
timeout: timeout,
timeout: httpTimeout.asMilliseconds(),
headers: {
"Accept-Encoding": "gzip, deflate",
"Content-Type": contentType,
"Cache-Control": "no-cache",
},
httpsAgent: agent,
responseType: "arraybuffer",
maxBodyLength: maxPayloadSize,
maxContentLength: maxPayloadSize,
});
},
{
initialDelay: 3000,
maxAttempts: 3,
initialDelay: initialDelay.asMilliseconds(),
maxAttempts: 4,
//TODO: This introduces retry on timeout without needing to specify the http Code: https://github.com/metriport/metriport/pull/2285. Remove once PR is merged
httpCodesToRetry: ["ECONNREFUSED", "ECONNRESET", "ETIMEDOUT"],
httpCodesToRetry: [
"ECONNREFUSED",
"ECONNRESET",
"ETIMEDOUT",
"ECONNABORTED",
"ERR_BAD_RESPONSE",
],
}
);

Expand Down
5 changes: 4 additions & 1 deletion packages/shared/src/net/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { AxiosError } from "axios";
// https://nodejs.org/docs/latest-v18.x/api/errors.html#common-system-errors
export type NodeNetworkError = "ECONNREFUSED" | "ECONNRESET";

export type AxiosNetworkError = typeof AxiosError.ETIMEDOUT | typeof AxiosError.ECONNABORTED;
export type AxiosNetworkError =
| typeof AxiosError.ETIMEDOUT
| typeof AxiosError.ECONNABORTED
| typeof AxiosError.ERR_BAD_RESPONSE;

export type NetworkError = NodeNetworkError | AxiosNetworkError;