Add SandboxSet abstraction for goroutine-free sandbox pooling#404
Open
AmiBuch wants to merge 5 commits intoopen-lambda:mainfrom
Open
Add SandboxSet abstraction for goroutine-free sandbox pooling#404AmiBuch wants to merge 5 commits intoopen-lambda:mainfrom
AmiBuch wants to merge 5 commits intoopen-lambda:mainfrom
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Overview
This PR implements Step 1 of the plan to eliminate unnecessary goroutines from the Lambda layer (issue #217).
Currently, each Lambda Function maintains a pool of Lambda Instances where each instance runs in its own goroutine waiting on channels. This creates significant memory overhead and unnecessary complexity.
SandboxSet provides a simple, thread-safe pool of sandboxes using mutex-based synchronization instead of goroutines and channels.
Changes
New Files
go/worker/sandbox/sandboxSet.go- Core SandboxSet implementation (920 LOC)go/worker/sandbox/sandboxSetAPI.go- Enhanced API with options pattern, metrics, and events (449 LOC)go/worker/sandbox/mock_test.go- MockSandbox for testing without real containers (247 LOC)go/worker/sandbox/sandboxSet_unit_test.go- 22 comprehensive unit tests (645 LOC)go/worker/sandbox/sandboxSet_test.go- Integration tests with real Docker containers (450 LOC)Architecture
Current Flow (with Lambda Instance goroutines):
New Flow (with SandboxSet - Steps 2&3):
Core API
Key Features
Design Decisions
Testing
22 unit tests using MockSandbox run in 0.172 seconds. Tests cover basic operations, reuse, pool growth, capacity, health checks, concurrent access, warm/shrink, options pattern, metrics, events, and error handling.
9 integration tests with real Docker containers run in 16.6 seconds. Tests verify container lifecycle, sandbox reuse, dynamic pool growth, capacity enforcement, destroy/remove operations, pause/unpause health checks, failed health check detection, concurrent access (12 goroutines with 12 real containers), and error handling.
Next Steps
Step 2: Replace ImportCacheNode with SandboxSet to eliminate complex reference counting logic.
Step 3: Eliminate LambdaInstance goroutines by having Lambda Function call SandboxSet directly.
Performance Impact
Estimated memory savings: 8KB per Lambda Instance reduced to approximately 500 bytes per SandboxSet pool (94% reduction in per-pool overhead).
Concurrency improvements: RWMutex enables concurrent reads, eliminates channel blocking and goroutine scheduling overhead, replaces message passing with direct function calls.
Backward Compatibility
This PR is fully backward compatible. Existing sandbox code is unchanged, new SandboxSet is opt-in and not yet integrated, and all existing tests pass.