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

refactor: fastFailMetadataRequest to use Promise.any #603

Open
danielbankhead opened this issue Nov 10, 2023 · 2 comments · May be fixed by #604
Open

refactor: fastFailMetadataRequest to use Promise.any #603

danielbankhead opened this issue Nov 10, 2023 · 2 comments · May be fixed by #604
Assignees
Labels
good first issue This issue is a good place to started contributing to this repository. next major: breaking change this is a change that we should wait to bundle into the next major version priority: p3 Desirable enhancement or fix. May not be included in next release. semver: major Hint for users that this is an API breaking change. type: cleanup An internal cleanup or hygiene concern.

Comments

@danielbankhead
Copy link
Member

We can streamline fastFailMetadataRequest by using Promise.any in Node 15+ rather than Promise.race:

gcp-metadata/src/index.ts

Lines 179 to 229 in 92f258c

async function fastFailMetadataRequest<T>(
options: GaxiosOptions
): Promise<GaxiosResponse> {
const secondaryOptions = {
...options,
url: options.url!.replace(getBaseUrl(), getBaseUrl(SECONDARY_HOST_ADDRESS)),
};
// We race a connection between DNS/IP to metadata server. There are a couple
// reasons for this:
//
// 1. the DNS is slow in some GCP environments; by checking both, we might
// detect the runtime environment signficantly faster.
// 2. we can't just check the IP, which is tarpitted and slow to respond
// on a user's local machine.
//
// Additional logic has been added to make sure that we don't create an
// unhandled rejection in scenarios where a failure happens sometime
// after a success.
//
// Note, however, if a failure happens prior to a success, a rejection should
// occur, this is for folks running locally.
//
let responded = false;
const r1: Promise<GaxiosResponse> = request<T>(options)
.then(res => {
responded = true;
return res;
})
.catch(err => {
if (responded) {
return r2;
} else {
responded = true;
throw err;
}
});
const r2: Promise<GaxiosResponse> = request<T>(secondaryOptions)
.then(res => {
responded = true;
return res;
})
.catch(err => {
if (responded) {
return r1;
} else {
responded = true;
throw err;
}
});
return Promise.race([r1, r2]);
}

@danielbankhead danielbankhead added semver: major Hint for users that this is an API breaking change. type: cleanup An internal cleanup or hygiene concern. good first issue This issue is a good place to started contributing to this repository. priority: p3 Desirable enhancement or fix. May not be included in next release. next major: breaking change this is a change that we should wait to bundle into the next major version labels Nov 10, 2023
@Dhoni77
Copy link

Dhoni77 commented Nov 11, 2023

Hi @danielbankhead I would like to work on this

@danielbankhead
Copy link
Member Author

Thanks @Dhoni77; note that this will require us to Node 16 (next stable), which will not happen for some time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue This issue is a good place to started contributing to this repository. next major: breaking change this is a change that we should wait to bundle into the next major version priority: p3 Desirable enhancement or fix. May not be included in next release. semver: major Hint for users that this is an API breaking change. type: cleanup An internal cleanup or hygiene concern.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants