generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 59
feat(toolkit-lib): network detector #926
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
Open
kaizencc
wants to merge
34
commits into
main
Choose a base branch
from
conroy/ping
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
7ee3f4d
feat: add network detector that uses notices endpoint
kaizencc 7718108
feat(toolkit-lib): network detector
kaizencc 91d3441
chore: refactor network detector to ping once an hour and write to disk
kaizencc 51ffbf6
Merge branch 'main' into conroy/ping
kaizencc d7dcdc6
update funnle test
kaizencc f69b420
mock network detector in notices
kaizencc f7cd018
chore: self mutation
invalid-email-address d0d4e93
merge
kaizencc ec0768f
chore: self mutation
invalid-email-address 60f2c12
udpate tests
kaizencc c342cc2
skip network check property
kaizencc 671b1ee
update network-detector
kaizencc 995765b
actually skip cache
kaizencc 5365dfb
one line
kaizencc d037ec8
Merge branch 'main' into conroy/ping
kaizencc 2cbbc6b
delete connection cache
kaizencc 22f49d4
add logs
kaizencc 4e65441
eslint
kaizencc c05df0e
logs
kaizencc 0d975e9
await
kaizencc e647834
type
kaizencc 34683ea
omg
kaizencc 12e07e2
reverse
kaizencc 0fc2d90
chore: self mutation
invalid-email-address ae56f62
merge
kaizencc 0818fb2
omgggg
kaizencc 7f2d4ea
update call
kaizencc c33ec6c
hail mary
kaizencc b2822d2
add back timeout
kaizencc 0dd91d8
refactor back to what i want to merge
kaizencc ae20ea5
add back head request
kaizencc 2adf47e
fix test
kaizencc d1355f1
add reasonable timeout
kaizencc 1dc3a75
fix tests
kaizencc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/@aws-cdk/toolkit-lib/lib/util/network-detector.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import type { Agent } from 'https'; | ||
| import { request } from 'https'; | ||
|
|
||
| /** | ||
| * Detects internet connectivity by making a lightweight request to the notices endpoint | ||
| */ | ||
| export class NetworkDetector { | ||
| /** | ||
| * Check if internet connectivity is available | ||
| */ | ||
| public static async hasConnectivity(agent?: Agent): Promise<boolean> { | ||
| const now = Date.now(); | ||
|
|
||
| // Return cached result if still valid | ||
| if (this.cachedResult !== undefined && now < this.cacheExpiry) { | ||
| return this.cachedResult; | ||
| } | ||
|
|
||
| try { | ||
| const connected = await this.ping(agent); | ||
| this.cachedResult = connected; | ||
| this.cacheExpiry = now + this.CACHE_DURATION_MS; | ||
| return connected; | ||
| } catch { | ||
| this.cachedResult = false; | ||
| this.cacheExpiry = now + this.CACHE_DURATION_MS; | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private static readonly CACHE_DURATION_MS = 30_000; // 30 seconds | ||
| private static readonly TIMEOUT_MS = 500; | ||
|
|
||
| private static cachedResult: boolean | undefined; | ||
| private static cacheExpiry: number = 0; | ||
|
|
||
| private static ping(agent?: Agent): Promise<boolean> { | ||
| return new Promise((resolve) => { | ||
| const req = request({ | ||
| hostname: 'cli.cdk.dev-tools.aws.dev', | ||
| path: '/notices.json', | ||
| method: 'HEAD', | ||
| agent, | ||
| timeout: this.TIMEOUT_MS, | ||
| }, (res) => { | ||
| resolve(res.statusCode !== undefined && res.statusCode < 500); | ||
| }); | ||
|
|
||
| req.on('error', () => resolve(false)); | ||
| req.on('timeout', () => { | ||
| req.destroy(); | ||
| resolve(false); | ||
| }); | ||
|
|
||
| req.end(); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
packages/@aws-cdk/toolkit-lib/test/util/network-detector.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import * as https from 'https'; | ||
| import { NetworkDetector } from '../../lib/util/network-detector'; | ||
|
|
||
| // Mock the https module | ||
| jest.mock('https'); | ||
| const mockHttps = https as jest.Mocked<typeof https>; | ||
|
|
||
| describe('NetworkDetector', () => { | ||
| let mockRequest: jest.Mock; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| mockRequest = jest.fn(); | ||
| mockHttps.request.mockImplementation(mockRequest); | ||
|
|
||
| // Clear static cache between tests | ||
| (NetworkDetector as any).cachedResult = undefined; | ||
| (NetworkDetector as any).cacheExpiry = 0; | ||
| }); | ||
|
|
||
| test('returns true when server responds with success status', async () => { | ||
| const mockReq = { | ||
| on: jest.fn(), | ||
| end: jest.fn(), | ||
| destroy: jest.fn(), | ||
| }; | ||
|
|
||
| mockRequest.mockImplementation((_options, callback) => { | ||
| callback({ statusCode: 200 }); | ||
| return mockReq; | ||
| }); | ||
|
|
||
| const result = await NetworkDetector.hasConnectivity(); | ||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| test('returns false when server responds with server error', async () => { | ||
| const mockReq = { | ||
| on: jest.fn(), | ||
| end: jest.fn(), | ||
| destroy: jest.fn(), | ||
| }; | ||
|
|
||
| mockRequest.mockImplementation((_options, callback) => { | ||
| callback({ statusCode: 500 }); | ||
| return mockReq; | ||
| }); | ||
|
|
||
| const result = await NetworkDetector.hasConnectivity(); | ||
| expect(result).toBe(false); | ||
| }); | ||
|
|
||
| test('returns false on network error', async () => { | ||
| const mockReq = { | ||
| on: jest.fn((event, handler) => { | ||
| if (event === 'error') { | ||
| setTimeout(() => handler(new Error('Network error')), 0); | ||
| } | ||
| }), | ||
| end: jest.fn(), | ||
| destroy: jest.fn(), | ||
| }; | ||
|
|
||
| mockRequest.mockReturnValue(mockReq); | ||
|
|
||
| const result = await NetworkDetector.hasConnectivity(); | ||
| expect(result).toBe(false); | ||
| }); | ||
|
|
||
| test('returns false on timeout', async () => { | ||
| const mockReq = { | ||
| on: jest.fn((event, handler) => { | ||
| if (event === 'timeout') { | ||
| setTimeout(() => handler(), 0); | ||
| } | ||
| }), | ||
| end: jest.fn(), | ||
| destroy: jest.fn(), | ||
| }; | ||
|
|
||
| mockRequest.mockReturnValue(mockReq); | ||
|
|
||
| const result = await NetworkDetector.hasConnectivity(); | ||
| expect(result).toBe(false); | ||
| }); | ||
|
|
||
| test('caches result for subsequent calls', async () => { | ||
| const mockReq = { | ||
| on: jest.fn(), | ||
| end: jest.fn(), | ||
| destroy: jest.fn(), | ||
| }; | ||
|
|
||
| mockRequest.mockImplementation((_options, callback) => { | ||
| callback({ statusCode: 200 }); | ||
| return mockReq; | ||
| }); | ||
|
|
||
| await NetworkDetector.hasConnectivity(); | ||
| await NetworkDetector.hasConnectivity(); | ||
|
|
||
| expect(mockRequest).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,18 +3,30 @@ import { createTestEvent } from './util'; | |
| import { IoHelper } from '../../../../lib/api-private'; | ||
| import { CliIoHost } from '../../../../lib/cli/io-host'; | ||
| import { EndpointTelemetrySink } from '../../../../lib/cli/telemetry/sink/endpoint-sink'; | ||
| import { NetworkDetector } from '@aws-cdk/toolkit-lib/lib/util/network-detector'; | ||
|
|
||
| // Mock the https module | ||
| jest.mock('https', () => ({ | ||
| request: jest.fn(), | ||
| })); | ||
|
|
||
| // Mock NetworkDetector | ||
| jest.mock('@aws-cdk/toolkit-lib', () => ({ | ||
| ...jest.requireActual('@aws-cdk/toolkit-lib'), | ||
| NetworkDetector: { | ||
| hasConnectivity: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| describe('EndpointTelemetrySink', () => { | ||
| let ioHost: CliIoHost; | ||
|
|
||
| beforeEach(() => { | ||
| jest.resetAllMocks(); | ||
|
|
||
| // Mock NetworkDetector to return true by default for existing tests | ||
| (NetworkDetector.hasConnectivity as jest.Mock).mockResolvedValue(true); | ||
|
|
||
| ioHost = CliIoHost.instance(); | ||
| }); | ||
|
|
||
|
|
@@ -312,4 +324,20 @@ describe('EndpointTelemetrySink', () => { | |
| expect.stringContaining('Telemetry Error: POST example.com/telemetry:'), | ||
| ); | ||
| }); | ||
|
|
||
| test('skips request when no connectivity detected', async () => { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the true result: we do not ping the telemetry endpoint in environments without internet access |
||
| // GIVEN | ||
| (NetworkDetector.hasConnectivity as jest.Mock).mockResolvedValue(false); | ||
|
|
||
| const testEvent = createTestEvent('INVOKE', { foo: 'bar' }); | ||
| const client = new EndpointTelemetrySink({ endpoint: 'https://example.com/telemetry', ioHost }); | ||
|
|
||
| // WHEN | ||
| await client.emit(testEvent); | ||
| await client.flush(); | ||
|
|
||
| // THEN | ||
| expect(NetworkDetector.hasConnectivity).toHaveBeenCalledWith(undefined); | ||
| expect(https.request).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is throwing the right thing here? Is that error caught elsewhere? Asking because Notices should just silently fail. A comment might help.
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.
this is the right thing to do here. we are throwing errors in
web-data-sourceon failures and expecting to swallow them elsewhere (which we do)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.
[non-blocking] Since this pattern will be very common (get the result, check whether it's true and throw an error if not), we could also have a method that takes a callback and does this for you:
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.
this is a good thought and i have considered this. im not entirely against it, but i feel like my more (naive) approach works more intuitively even if it reuses the same pattern. we can always refactor in the future if it turns out that
ifConnectedis a cleaner API