-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: add env vars for keepAliveTimeout and headersTimeout #15125
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
base: main
Are you sure you want to change the base?
feat: add env vars for keepAliveTimeout and headersTimeout #15125
Conversation
🦋 Changeset detectedLatest commit: 1b2753d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
cc: @Conduitry you might be interested in this one since it pertains to the Node adapter |
Co-authored-by: Tee Ming <chewteeming01@gmail.com>
packages/adapter-node/src/index.js
Outdated
| const httpServer = http.createServer(); | ||
|
|
||
| const keep_alive_timeout_var = env('KEEP_ALIVE_TIMEOUT', ''); | ||
| if (keep_alive_timeout_var) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a better parsing abstraction here. In the env module, declare an int_env
const integer = /^\d+$/;
function parsing_error(name, value, description) {
throw new Error(`Invalid value for environment variable ${name}: ${JSON.stringify(value)} (${description})`)
}
/**
* @param {string} name
* @param {number} [fallback]
* @returns {number} | undefined
*/
function int_env(name, fallback) {
const raw = env(name);
if (!raw) {
return fallback;
}
if (!integer.test(raw)) {
parsing_error('value was a string but could not be parsed as an integer');
}
const parsed = Number.parseInt(raw, 10);
// we don't technically need to check `Number.isNaN` because the integer regex will always parse,
// but just in case there's some new codepath introduced somewhere down the line, it's probably good to stick this in here
if (Number.isNaN(parsed)) {
parsing_error('should be a number');
}
if (parsed < 0) {
parsing_error('should be non-negative');
}
return parsed;
}Then we can use this for both of the variables here. It'll be a lot cleaner, as we can just do const keep_alive_timeout = int_env('KEEP_ALIVE_TIMEOUT');, check if it exists, and assign it if so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you suppose it'd be worth being more specific and calling it non_negative_int_env? I'm fine either way and agree that a helper here will be better for readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just timeout(name, fallback) instead? If we ever need more-specific int handling we can break it out to other functions, but timeouts can't be negative.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a new timeout_env helper in 1b2753d. I also made an attempt at adding some unit tests but hit a snag since the env.js file expects the ENV_PREFIX global to be defined. A vi.stubGlobal plus a dynamic import got things working, though. If that is too hacky, I'm happy to adjust or remove the tests altogether.
Hey there! First off, I want to say thanks for SvelteKit and all of the work done by contributors.
While working on some recent deployments, I noticed that SvelteKit's
adapter-nodedoesn't expose a way to override Node's keepAliveTimeout and headersTimeout values. These often need tweaking when running Node behind a reverse proxy (since the upstream web server's timeouts should be longer than the proxy's). Otherwise, systems can find themselves dealing with intermittent 502 errors.To that end, this PR adds checks for corresponding environment variables:
KEEP_ALIVE_TIMEOUTandHEADERS_TIMEOUT. When present, they will override Node's default settings. Node expects these to be in milliseconds but I chose to have the vars be in seconds to match the otheradapter-nodetimeouts (SHUTDOWN_TIMEOUTandIDLE_TIMEOUT).Additionally, I slightly modified how and when polka's underlying HTTP server gets created. When not provided directly to polka, the server doesn't get created until calling
.listen(). At that point, you can modifypolka.serverbut it is already running. This has a chance of causing a race condition between the server accepting connections and the properties being changed. By manually creating the server ourselves and passing it to polka, this race is avoided. Plus, it solves some of the typing issues related topolka.serverbeing a net.Server and not an http.Server (I've got a PR up to fix that, though).Test Plan
I tested this locally (in
playgrounds/basicand after runningpnpm build) by running the following script (timeouts.js) with a few different env var configurations:Without any vars, we see the defaults:
With both new vars specified, we see that the values have been successfully overridden:
With invalid vars, a meaningful error is thrown:
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits