diff --git a/.changeset/fair-shrimps-count.md b/.changeset/fair-shrimps-count.md new file mode 100644 index 000000000..6c6675e7c --- /dev/null +++ b/.changeset/fair-shrimps-count.md @@ -0,0 +1,12 @@ +--- +'skuba': patch +--- + +template/\*-rest-api: Set `keepAliveTimeout` to 31 seconds to prevent HTTP 502s + +The default Node.js server keep-alive timeout is set to 5 seconds. However, the Gantry default ALB idle timeout is 30 seconds. This would lead to the occasional issues where the sidecar would throw `proxyStatus=502` errors. AWS recommends setting an application timeout larger than the ALB idle timeout. + +A more detailed explanation can be found in the below links: + +1. +2. diff --git a/template/express-rest-api/src/listen.ts b/template/express-rest-api/src/listen.ts index a4cdfd555..a0bcbcf0e 100644 --- a/template/express-rest-api/src/listen.ts +++ b/template/express-rest-api/src/listen.ts @@ -14,3 +14,10 @@ const listener = app.listen(config.port, () => { rootLogger.debug(`listening on port ${address.port}`); } }); + +// Gantry ALB default idle timeout is 30 seconds +// https://nodejs.org/docs/latest-v18.x/api/http.html#serverkeepalivetimeout +// Node default is 5 seconds +// https://docs.aws.amazon.com/elasticloadbalancing/latest/application/application-load-balancers.html#connection-idle-timeout +// AWS recommends setting an application timeout larger than the load balancer +listener.keepAliveTimeout = 31000; diff --git a/template/koa-rest-api/src/listen.ts b/template/koa-rest-api/src/listen.ts index c1a76ba21..6e31173ad 100644 --- a/template/koa-rest-api/src/listen.ts +++ b/template/koa-rest-api/src/listen.ts @@ -15,3 +15,10 @@ const listener = app.listen(config.port, () => { logger.debug(`listening on port ${address.port}`); } }); + +// Gantry ALB default idle timeout is 30 seconds +// https://nodejs.org/docs/latest-v18.x/api/http.html#serverkeepalivetimeout +// Node default is 5 seconds +// https://docs.aws.amazon.com/elasticloadbalancing/latest/application/application-load-balancers.html#connection-idle-timeout +// AWS recommends setting an application timeout larger than the load balancer +listener.keepAliveTimeout = 31000;