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

fix(ec2-metadata-service): set timeout for requests #6072

Merged
merged 3 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions packages/ec2-metadata-service/src/MetadataService.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,17 @@ describe("MetadataService E2E Tests", () => {
expect(lines).toContain("services/");
}
});

it("should timeout as expected when a request exceeds the specified duration", async () => {
if (!metadataServiceAvailable) {
return;
}
metadataService = new MetadataService({ httpOptions: { timeout: 0.1 } }); // 0.1ms timeout for testing
try {
await metadataService.request("/latest/meta-data/", {});
} catch (error) {
expect(error).toBeDefined();
expect(error.message).toMatch(/TimeoutError/);
}
});
});
14 changes: 10 additions & 4 deletions packages/ec2-metadata-service/src/MetadataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ export class MetadataService {
}

async request(path: string, options: { method?: string; headers?: Record<string, string> }): Promise<string> {
const { endpoint, ec2MetadataV1Disabled } = await this.config;
const handler = new NodeHttpHandler();
const { endpoint, ec2MetadataV1Disabled, httpOptions } = await this.config;
const handler = new NodeHttpHandler({
requestTimeout: httpOptions?.timeout,
siddsriv marked this conversation as resolved.
Show resolved Hide resolved
connectionTimeout: httpOptions?.timeout,
});
const endpointUrl = new URL(endpoint!);
const headers = options.headers || {};
/**
Expand Down Expand Up @@ -82,8 +85,11 @@ export class MetadataService {
/**
* Refer: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-metadata-v2-how-it-works.html
*/
const { endpoint } = await this.config;
const handler = new NodeHttpHandler();
const { endpoint, httpOptions } = await this.config;
const handler = new NodeHttpHandler({
requestTimeout: httpOptions?.timeout,
connectionTimeout: httpOptions?.timeout,
});
const endpointUrl = new URL(endpoint!);
const tokenRequest = new HttpRequest({
method: "PUT",
Expand Down
Loading