generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 3
feat: lazy loading #13
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
0ccfb91
feat: multi base
maxday 211a404
feat: fix tess
maxday bd3d9b1
fix: discard yarn.lock
maxday 6a75ee4
fix: tests
maxday 68243d0
fix: test
maxday 19329f5
feat: use await
maxday bc884e7
fix: multiconcurrency
maxday 23c57d7
fix: readme
maxday d9f9ebc
fix: cleanup
maxday a3d10b2
fix: cleanup
maxday e3f640e
fix: export type
maxday b05a318
fix: adressing first batch of PR comments
maxday 84d9235
fix: test refactoring
maxday 8d79c7f
fix: changelog
maxday e77cebb
docs: update changelog
trivikr 88241c4
fix: PR comments dynamicImport
maxday 3c762a8
fix: PR comments, run prettier
maxday c45d6d7
fix: PR comments, symbol.for
maxday 39cffee
fix: PR comment, NO_GLOBAL_AWS_LAMBDA
maxday 151642c
fix: PR comment, getRequestId not undefined
maxday af75806
fix: PR comments, remove useless getInstance
maxday 0595d2c
fix: PR comments, remove useless ctor
maxday a8d1531
fix: PR comments getInstance -> getInstanceAsync
maxday a1e9c59
fix: PR comments, PROTECTED_KEYS are now static
maxday e080cff
fix: PR comments, import type and add benchmark
maxday 7cffc76
fix: PR comment, race
maxday 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| "@aws/lambda-invoke-store": minor | ||
| --- | ||
|
|
||
| - `InvokeStore` is now accessible via `InvokeStore.getInstance()` instead of direct instantiation | ||
|
|
||
| - Lazy loading of `node:async_hooks` to improve startup performance | ||
| - Dynamic implementation selection based on Lambda environment: | ||
| - Single-context implementation for standard Lambda executions | ||
| - Multi-context implementation (using AsyncLocalStorage) | ||
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
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,122 @@ | ||
| import { describe, it, expect, afterEach, beforeEach, vi } from "vitest"; | ||
| import { InvokeStore, InvokeStoreBase } from "./invoke-store.js"; | ||
|
|
||
| describe("InvokeStore", async () => { | ||
|
|
||
| let invokeStore: InvokeStoreBase; | ||
|
|
||
| beforeEach(async () => { | ||
| vi.stubEnv("AWS_LAMBDA_MAX_CONCURRENCY", "2"); | ||
| vi.useFakeTimers(); | ||
| invokeStore = await InvokeStore.getInstance(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| describe("run", () => { | ||
|
|
||
| it("should handle nested runs with different IDs", async () => { | ||
| // GIVEN | ||
| const traces: string[] = []; | ||
|
|
||
| // WHEN | ||
| await invokeStore.run( | ||
| { | ||
| [invokeStore.PROTECTED_KEYS.REQUEST_ID]: "outer", | ||
| }, | ||
| async () => { | ||
| traces.push(`outer-${invokeStore.getRequestId()}`); | ||
| await invokeStore.run( | ||
| { | ||
| [invokeStore.PROTECTED_KEYS.REQUEST_ID]: "inner", | ||
| }, | ||
| async () => { | ||
| traces.push(`inner-${invokeStore.getRequestId()}`); | ||
| }, | ||
| ); | ||
| traces.push(`outer-again-${invokeStore.getRequestId()}`); | ||
| }, | ||
| ); | ||
|
|
||
| // THEN | ||
| expect(traces).toEqual([ | ||
| "outer-outer", | ||
| "inner-inner", | ||
| "outer-again-outer", | ||
| ]); | ||
| }); | ||
|
|
||
| it("should maintain isolation between concurrent executions", async () => { | ||
| // GIVEN | ||
| const traces: string[] = []; | ||
|
|
||
| // WHEN - Simulate concurrent invocations | ||
| const isolateTasks = Promise.all([ | ||
| invokeStore.run( | ||
| { | ||
| [invokeStore.PROTECTED_KEYS.REQUEST_ID]: "request-1", | ||
| [invokeStore.PROTECTED_KEYS.X_RAY_TRACE_ID]: "trace-1", | ||
| }, | ||
| async () => { | ||
| traces.push(`start-1-${invokeStore.getRequestId()}`); | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
| traces.push(`end-1-${invokeStore.getRequestId()}`); | ||
| }, | ||
| ), | ||
| invokeStore.run( | ||
| { | ||
| [invokeStore.PROTECTED_KEYS.REQUEST_ID]: "request-2", | ||
| [invokeStore.PROTECTED_KEYS.X_RAY_TRACE_ID]: "trace-2", | ||
| }, | ||
| async () => { | ||
| traces.push(`start-2-${invokeStore.getRequestId()}`); | ||
| await new Promise((resolve) => setTimeout(resolve, 5)); | ||
| traces.push(`end-2-${invokeStore.getRequestId()}`); | ||
| }, | ||
| ), | ||
| ]); | ||
| vi.runAllTimers(); | ||
| await isolateTasks; | ||
|
|
||
| // THEN | ||
| expect(traces).toEqual([ | ||
| "start-1-request-1", | ||
| "start-2-request-2", | ||
| "end-2-request-2", | ||
| "end-1-request-1", | ||
| ]); | ||
| }); | ||
|
|
||
| it("should maintain isolation across async operations", async () => { | ||
| // GIVEN | ||
| const traces: string[] = []; | ||
|
|
||
| // WHEN | ||
| await invokeStore.run( | ||
| { | ||
| [invokeStore.PROTECTED_KEYS.REQUEST_ID]: "request-1", | ||
| }, | ||
| async () => { | ||
| traces.push(`before-${invokeStore.getRequestId()}`); | ||
| const task = new Promise((resolve) => { | ||
| setTimeout(resolve, 1); | ||
| }).then(() => { | ||
| traces.push(`inside-${invokeStore.getRequestId()}`); | ||
| }); | ||
| vi.runAllTimers(); | ||
| await task; | ||
| traces.push(`after-${invokeStore.getRequestId()}`); | ||
| }, | ||
| ); | ||
|
|
||
| // THEN | ||
| expect(traces).toEqual([ | ||
| "before-request-1", | ||
| "inside-request-1", | ||
| "after-request-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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { | ||
trivikr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| describe, | ||
| it, | ||
| expect, | ||
| beforeAll, | ||
| afterAll, | ||
| beforeEach, | ||
| vi, | ||
| } from "vitest"; | ||
| import { InvokeStoreBase, InvokeStore as OriginalImport } from "./invoke-store.js"; | ||
|
|
||
| describe("InvokeStore Global Singleton", () => { | ||
| const originalGlobalAwsLambda = globalThis.awslambda; | ||
| const originalEnv = process.env; | ||
| let invokeStore: InvokeStoreBase; | ||
|
|
||
| beforeAll(() => { | ||
| globalThis.awslambda = originalGlobalAwsLambda; | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| delete (globalThis as any).awslambda; | ||
| process.env = originalEnv; | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| vi.stubEnv("AWS_LAMBDA_MAX_CONCURRENCY", "2"); | ||
| process.env = { ...originalEnv }; | ||
| invokeStore = await OriginalImport.getInstance(); | ||
| }); | ||
|
|
||
| it("should maintain singleton behavior with dynamic imports", async () => { | ||
| // GIVEN | ||
| const testRequestId = "dynamic-import-test"; | ||
| const testTenantId = "dynamic-import-tenant-id-test"; | ||
| const testKey = "dynamic-key"; | ||
| const testValue = "dynamic-value"; | ||
|
|
||
| // WHEN - Set up context with original import | ||
| await invokeStore.run( | ||
| { | ||
| [invokeStore.PROTECTED_KEYS.REQUEST_ID]: testRequestId, | ||
| [invokeStore.PROTECTED_KEYS.TENANT_ID]: testTenantId, | ||
| }, | ||
| async () => { | ||
| invokeStore.set(testKey, testValue); | ||
|
|
||
| // Dynamically import the module again | ||
| const dynamicModule = await import("./invoke-store.js"); | ||
| const DynamicImport = await dynamicModule.InvokeStore.getInstance(); | ||
kuhe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // THEN - Dynamically imported instance should see the same context | ||
| expect(DynamicImport).toBe(invokeStore); // Same instance | ||
| expect(DynamicImport.getRequestId()).toBe(testRequestId); | ||
| expect(DynamicImport.getTenantId()).toBe(testTenantId); | ||
| expect(DynamicImport.get(testKey)).toBe(testValue); | ||
|
|
||
| // WHEN - Set a new value using dynamic import | ||
| const newKey = "new-dynamic-key"; | ||
| const newValue = "new-dynamic-value"; | ||
| DynamicImport.set(newKey, newValue); | ||
|
|
||
| // THEN - Original import should see the new value | ||
| expect(invokeStore.get(newKey)).toBe(newValue); | ||
| } | ||
| ); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.