Skip to content

Commit 912c5bf

Browse files
committed
transform into timeout
1 parent 6af28c8 commit 912c5bf

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

src/proxy.ts

+13-15
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ function convertHeaders(
3333
const waitForEndpoint = async (
3434
endpoint: URL,
3535
deadline: number
36-
): Promise<{ deadline: number }> => {
36+
): Promise<{ timeout: number }> => {
3737
const start = Date.now();
38+
const timeout = deadline - start;
3839

3940
// Stop recursing if the deadline has passed
40-
if (deadline < start) {
41-
return { deadline: 0 };
41+
if (timeout < 0) {
42+
return { timeout: 0 };
4243
}
4344

4445
const hostname = endpoint.hostname;
@@ -55,13 +56,13 @@ const waitForEndpoint = async (
5556
);
5657
};
5758

58-
socket.setTimeout(Date.now() - deadline);
59+
socket.setTimeout(deadline - start);
5960
socket.once("error", onError);
6061
socket.once("timeout", onError);
6162

6263
socket.connect(port, hostname, () => {
6364
socket.end();
64-
resolve({ deadline: deadline - (Date.now() - start) });
65+
resolve({ timeout: deadline - Date.now() });
6566
});
6667
});
6768
};
@@ -70,7 +71,7 @@ export const endpointProxy = async ({
7071
requestId,
7172
endpoint,
7273
event,
73-
initialDeadline,
74+
deadline,
7475
}: EndpointRequest): Promise<EndpointResponse> => {
7576
const {
7677
requestContext,
@@ -82,15 +83,12 @@ export const endpointProxy = async ({
8283
} = event;
8384
const method = requestContext.http.method;
8485

85-
log("Waiting for endpoint to start", { endpoint, initialDeadline });
86-
const { deadline } = await waitForEndpoint(endpoint, initialDeadline);
87-
log("Endpoint started", { endpoint, deadline });
86+
log("Waiting for endpoint to start", { endpoint, deadline });
87+
const { timeout } = await waitForEndpoint(endpoint, deadline);
8888

89-
if (!deadline) {
89+
if (!timeout) {
9090
throw new Error(
91-
`${endpoint.toString()} took longer than ${Math.floor(
92-
initialDeadline / 1000
93-
)} second(s) to start.`
91+
`${endpoint.toString()} took longer than ${deadline} milliseconds to start.`
9492
);
9593
}
9694

@@ -102,14 +100,14 @@ export const endpointProxy = async ({
102100
const decodedBody =
103101
isBase64Encoded && rawBody ? Buffer.from(rawBody, "base64") : rawBody;
104102

105-
log("Proxying request", { url, method, rawHeaders, decodedBody, deadline });
103+
log("Proxying request", { url, method, rawHeaders, decodedBody, timeout });
106104

107105
const response = await axios.request({
108106
method: method.toLowerCase(),
109107
url: url.toString(),
110108
headers: rawHeaders,
111109
data: decodedBody,
112-
timeout: Date.now() - deadline,
110+
timeout,
113111
responseType: "arraybuffer",
114112
});
115113

src/routing.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ export const routeEvents = async (
2727
throw new Error("No request ID found in response headers");
2828
}
2929

30-
const initialDeadline = Number.parseInt(
31-
headers["lambda-runtime-deadline-ms"]
32-
);
30+
const deadline = Number.parseInt(headers["lambda-runtime-deadline-ms"]);
3331

3432
let payload: any | undefined = undefined;
3533

@@ -54,7 +52,7 @@ export const routeEvents = async (
5452
requestId,
5553
endpoint,
5654
event: JSON.parse(data),
57-
initialDeadline,
55+
deadline,
5856
};
5957

6058
payload = (await endpointProxy(request)).payload;

src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export type EndpointRequest = {
22
requestId: string;
33
endpoint: URL;
44
event: any;
5-
initialDeadline: number;
5+
deadline: number;
66
};
77

88
export type EndpointResponse = {

0 commit comments

Comments
 (0)