sst / sst

Build full-stack apps on your own infrastructure.
https://sst.dev
MIT License
22.11k stars 1.68k forks source link

Live API times out with large response body #3979

Open shamsup opened 1 month ago

shamsup commented 1 month ago

I have a function that is proxying an API. Some requests have relatively large response sizes, up to 1MB. When running in Live mode (sst dev), it seems like exceeding the IoT Core message size limit(128KB?) causes API Gateway to timeout, giving the ole {"message":"Internal Server Error"} response.

Cloudwatch logs for one of the large payloads:

START RequestId: 299ca077-aa3a-47c0-a191-47bb9adfe7f8 Version: $LATEST
2024/10/11 19:40:57 INFO dialing lambda runtime api
2024/10/11 19:40:58 INFO request method=GET url=/2018-06-01/runtime/invocation/next
2024/10/11 19:40:58 INFO forwarding response
2024/10/11 19:40:58 INFO read error err=EOF
2024/10/11 19:40:58 INFO writing to topic topic=ion/<project>/shamsup/<id>/response/emvwvxkz data=1312
2024/10/11 19:40:58 INFO closed iot writer topic=ion/<project>/shamsup/<id>/response/emvwvxkz count=1
2024/10/11 19:40:58 INFO response sent method=GET
2024/10/11 19:41:01 INFO request method=POST url=/2018-06-01/runtime/invocation/299ca077-aa3a-47c0-a191-47bb9adfe7f8/response
2024/10/11 19:41:01 INFO forwarding response
2024/10/11 19:41:03 INFO read error err="read tcp 127.0.0.1:57680->127.0.0.1:9001: i/o timeout"
2024/10/11 19:41:05 INFO read error err="read tcp 127.0.0.1:57680->127.0.0.1:9001: i/o timeout"
2024/10/11 19:41:07 INFO read error err="read tcp 127.0.0.1:57680->127.0.0.1:9001: i/o timeout"
2024/10/11 19:41:09 INFO read error err="read tcp 127.0.0.1:57680->127.0.0.1:9001: i/o timeout"
2024/10/11 19:41:11 INFO read error err="read tcp 127.0.0.1:57680->127.0.0.1:9001: i/o timeout"
2024/10/11 19:41:13 INFO read error err="read tcp 127.0.0.1:57680->127.0.0.1:9001: i/o timeout"
2024/10/11 19:41:15 INFO read error err="read tcp 127.0.0.1:57680->127.0.0.1:9001: i/o timeout"
2024/10/11 19:41:17 INFO read error err="read tcp 127.0.0.1:57680->127.0.0.1:9001: i/o timeout"
2024/10/11 19:41:19 INFO read error err="read tcp 127.0.0.1:57680->127.0.0.1:9001: i/o timeout"
2024/10/11 19:41:21 INFO read error err="read tcp 127.0.0.1:57680->127.0.0.1:9001: i/o timeout"
2024-10-11T19:41:22.921Z 299ca077-aa3a-47c0-a191-47bb9adfe7f8 Task timed out after 25.03 seconds
END RequestId: 299ca077-aa3a-47c0-a191-47bb9adfe7f8

minimal handler code:

export const handler: APIGatewayProxyHandlerV2 = async (event) => {
  let { headers, rawPath, rawQueryString, isBase64Encoded, body } = event;
  if (isBase64Encoded && body) {
    body = Buffer.from(body, 'base64').toString('utf-8');
  }

  await maybeRefreshToken();

  const url = new URL(`${rawPath}${rawQueryString ? `?${rawQueryString}` : ''}`, HOST);
  console.log(`proxying request`);
  const response = await fetch(url, {
    method: event.requestContext.http.method,
    headers: {
      Authorization: `Bearer ${token}`,
    },
    body,
  });
  console.log(`got response`, response.status);

  const responseHeaders: Record<string, string> = {};
  response.headers.forEach((value, key) => {
    responseHeaders[key] = value;
  });
  let data = await response.text();
  console.log('data received. size:', Buffer.byteLength(data));
  return {
    statusCode: response.status,
    headers: responseHeaders,
    body: data,
  };
};

In my local terminal I can see that the proxied request does succeed:

|  Invoke      apiRouteBbovcaHandler
|  +347ms      proxying request
|  +1.613s     got response 200
|  +2.091s     data received. size: 1069274
<nothing after, where I would expect to see Done>
jayair commented 1 month ago

We have some plans around switching away from IoT. So we'll come back to this.

shamsup commented 1 month ago

Is this something I could work around by using response streaming? I know it doesn't actually stream from APIGW, but would your IoT implementation send chunks back instead?