Skip to content

feat(orchestrator): add capacity limit and memory-pressure eviction to template mmap cache#2958

Open
AdaAibaby wants to merge 2 commits into
e2b-dev:mainfrom
AdaAibaby:add-capacity-limit-and-memory-pressure-eviction
Open

feat(orchestrator): add capacity limit and memory-pressure eviction to template mmap cache#2958
AdaAibaby wants to merge 2 commits into
e2b-dev:mainfrom
AdaAibaby:add-capacity-limit-and-memory-pressure-eviction

Conversation

@AdaAibaby

Copy link
Copy Markdown
Contributor

Changes Summary
model.go

1.Add environment variable TEMPLATE_CACHE_MAX_ENTRIES (integer, default 0 = unlimited)
When a positive value is set, apply ttlcache.WithCapacity.
Cache evicts entries by LRU policy once the entry limit is reached.
2.Add environment variable TEMPLATE_CACHE_MIN_FREE_MEMORY_MB (int64, default 0 = disabled)
When enabled, start a background goroutine to poll host memory via unix.Sysinfo every 5 seconds.
Continuously evict LRU entries one by one until free memory is above the configured threshold.

cache.go
1.In NewCache, conditionally enable ttlcache.WithCapacity according to the new cache config.
2.In Cache.Start, launch the startMemoryPressureEviction goroutine if the memory threshold is non-zero.
Eviction logic: select entries with the earliest ExpiresAt timestamp (TTL resets on each access).
Stop eviction when memory pressure is relieved or the cache is empty.

Backward Compatibility
Both new environment variables use 0 as default, which fully keeps the original runtime behavior.
Existing cloud deployments require no extra configuration or code changes.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

Using unix.Sysinfo's Freeram to check available memory is incorrect because it represents completely unused memory rather than MemAvailable, which will cause premature cache evictions. Additionally, evicting entries in a tight loop without throttling will result in the eviction of all cache entries because system memory statistics are not updated instantaneously after an eviction.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread packages/orchestrator/pkg/sandbox/template/cache.go Outdated
Comment thread packages/orchestrator/pkg/sandbox/template/cache.go

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1f1a5a102c

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/orchestrator/pkg/sandbox/template/cache.go
Comment thread packages/orchestrator/pkg/sandbox/template/cache.go Outdated
…dd unit tests

- Replace unix.Sysinfo Freeram with /proc/meminfo MemAvailable to get
  true available memory (includes reclaimable page cache)
- Evict only one LRU entry per 1s tick instead of looping until threshold
  is met; mmap.Unmap is not instantaneous so the OS stats lag behind
- Add 6 unit tests covering WithCapacity LRU eviction, MemAvailable
  parser correctness/error handling, and pressure-eviction logic

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cab034cabc

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/orchestrator/pkg/sandbox/template/cache.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants