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

feat: significantly increase timeout if GCF environment detected #300

Merged
merged 4 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async function metadataAccessor<T>(
retryConfig: {noResponseRetries},
params: options.params,
responseType: 'text',
timeout: 3000,
timeout: requestTimeout(),
});
// NOTE: node.js converts all incoming headers to lower case.
if (res.headers[HEADER_NAME.toLowerCase()] !== HEADER_VALUE) {
Expand Down Expand Up @@ -221,3 +221,8 @@ export async function isAvailable() {
export function resetIsAvailableCache() {
cachedIsAvailableResponse = undefined;
}

export function requestTimeout(): number {
// Increase timeout if we think we're in a Cloud Run or GCF environment:
bcoe marked this conversation as resolved.
Show resolved Hide resolved
return process.env.K_SERVICE || process.env.FUNCTION_NAME ? 500000 : 3000;
zamnuts marked this conversation as resolved.
Show resolved Hide resolved
}
10 changes: 10 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,13 @@ it('resets cache when resetIsAvailableCache() is called', async () => {
primary.done();
assert.strictEqual(isGCE, false);
});

it('returns request timeout of 3000ms, when not GCF', () => {
assert.strictEqual(gcp.requestTimeout(), 3000);
});

it('returns request timeout of 500000ms, when in GCF environment', () => {
process.env.FUNCTION_NAME = 'my-function';
bcoe marked this conversation as resolved.
Show resolved Hide resolved
stephenplusplus marked this conversation as resolved.
Show resolved Hide resolved
assert.strictEqual(gcp.requestTimeout(), 500000);
delete process.env.FUNCTION_NAME;
});