Skip to content

dynamic_modules: remote source: add caching for fetched modules#43974

Merged
wbpcode merged 4 commits intoenvoyproxy:mainfrom
kanurag94:dym-local-cache
Mar 18, 2026
Merged

dynamic_modules: remote source: add caching for fetched modules#43974
wbpcode merged 4 commits intoenvoyproxy:mainfrom
kanurag94:dym-local-cache

Conversation

@kanurag94
Copy link
Copy Markdown
Contributor

@kanurag94 kanurag94 commented Mar 16, 2026

Commit Message: dynamic_modules: remote source: add caching for fetched modules

Motivation
Follow up from #43818 and #43333
Next PR: nack_on_cache_miss support (NACK on cache miss + background fetch)

Additional Description:

newDynamicModuleFromBytes already writes fetched modules to a deterministic path based on SHA256. This PR adds a check before the async fetch: if the file already exists on disk, load it directly and skip the HTTP fetch entirely.

It is a simple approach that forms base for the next PR.

Risk Level: Low
Testing: Added unit tests
Docs Changes: NA
Release Notes: Added
Platform Specific Features: NA
[Optional Runtime guard:] Not needed
[Optional Fixes #Issue]: NA
[Optional Fixes commit #PR or SHA]: NA
[Optional Deprecated:]: NA
[Optional API Considerations:] NA

Signed-off-by: Anurag Aggarwal <kanurag94@gmail.com>
@repokitteh-read-only
Copy link
Copy Markdown

As a reminder, PRs marked as draft will not be automatically assigned reviewers,
or be handled by maintainer-oncall triage.

Please mark your PR as ready when you want it to be reviewed!

🐱

Caused by: #43974 was opened by kanurag94.

see: more, trace.

Signed-off-by: Anurag Aggarwal <kanurag94@gmail.com>
@kanurag94 kanurag94 marked this pull request as ready for review March 17, 2026 05:32
@kanurag94
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

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

Choose a reason for hiding this comment

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

Code Review

This pull request introduces an in-memory cache for remotely fetched dynamic modules, which is a great performance improvement. The implementation is straightforward, adding the caching logic at the factory level to avoid redundant HTTP fetches for modules with the same SHA256. The cache invalidation logic for missing or unloadable files is also handled correctly. The added unit tests are comprehensive and cover both cache hits and cache invalidation scenarios.

I have one suggestion regarding code maintainability, related to how the path for the cached module is determined. Overall, this is a solid contribution.

Comment on lines +192 to +194
auto cached_path = std::filesystem::temp_directory_path() /
fmt::format("envoy_dynamic_module_{}.so", fetch_sha256);
remote_module_cache_[fetch_sha256] = cached_path.string();
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.

medium

This code reconstructs the path to the temporary file where the dynamic module was saved by newDynamicModuleFromBytes. This path generation logic appears to be duplicated from what newDynamicModuleFromBytes does internally, which creates a fragile dependency on its implementation details. If the file naming convention in newDynamicModuleFromBytes were to change, this caching logic would break.

A more robust approach would be for newDynamicModuleFromBytes to return the path of the created file along with the module pointer, or for the DynamicModule object to store the path from which it was loaded. This would eliminate the need to reconstruct the path here.

While changing newDynamicModuleFromBytes might be out of scope for this PR, it would be beneficial to centralize the path generation logic into a shared helper function to reduce duplication and improve long-term maintainability.

Signed-off-by: Anurag Aggarwal <kanurag94@gmail.com>
@kanurag94
Copy link
Copy Markdown
Contributor Author

/retest

Copy link
Copy Markdown
Member

@wbpcode wbpcode left a comment

Choose a reason for hiding this comment

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

Thanks for this great contribution. And tow comments are added. :)

Comment on lines +93 to +94
auto cache_it = remote_module_cache_.find(sha256);
if (cache_it != remote_module_cache_.end()) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we need the remote_module_cache_? I think we may could only need to check the std::filesystem::exists(cached_path) and if the file is there, then the cache hit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you so much for the review.
This makes sense. I was somehow trying to keep nack_on_cache_miss in my mind and not introduce a global cache key. But I think that's not avoidable in this PR.

Comment on lines +99 to +103
if (dynamic_module.ok()) {
// Cache hit — skip async fetch entirely.
return buildFilterFactoryCallback(std::move(dynamic_module.value()), proto_config,
module_config, context, scope);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think we need the fallback because the hash of the file is used for caching. The re-fecthing won't resolve the problem if the file/content is invalid?

Signed-off-by: Anurag Aggarwal <kanurag94@gmail.com>
@paul-r-gall
Copy link
Copy Markdown
Contributor

/assign @wbpcode

Copy link
Copy Markdown
Member

@wbpcode wbpcode left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks.

@wbpcode wbpcode merged commit 4be2167 into envoyproxy:main Mar 18, 2026
29 checks passed
fishcakez pushed a commit to fishcakez/envoy that referenced this pull request Mar 25, 2026
…yproxy#43974)

**Commit Message**: dynamic_modules: remote source: add caching for
fetched modules

**Motivation**
Follow up from envoyproxy#43818 and envoyproxy#43333
Next PR: `nack_on_cache_miss` support (NACK on cache miss + background
fetch)

**Additional Description**: 

`newDynamicModuleFromBytes` already writes fetched modules to a
deterministic path based on SHA256. This PR adds a check before the
async fetch: if the file already exists on disk, load it directly and
skip the HTTP fetch entirely.

It is a simple approach that forms base for the next PR.

**Risk Level**: Low
**Testing**: Added unit tests
**Docs Changes**: NA
**Release Notes**: Added
Platform Specific Features: NA
[Optional Runtime guard:] Not needed
[Optional Fixes #Issue]: NA
[Optional Fixes commit #PR or SHA]: NA
[Optional Deprecated:]: NA
[Optional [API
Considerations](https://github.com/envoyproxy/envoy/blob/main/api/review_checklist.md):]
NA

---------

Signed-off-by: Anurag Aggarwal <kanurag94@gmail.com>
TAOXUY pushed a commit to TAOXUY/envoy that referenced this pull request Apr 1, 2026
…yproxy#43974)

**Commit Message**: dynamic_modules: remote source: add caching for
fetched modules

**Motivation**
Follow up from envoyproxy#43818 and envoyproxy#43333
Next PR: `nack_on_cache_miss` support (NACK on cache miss + background
fetch)

**Additional Description**:

`newDynamicModuleFromBytes` already writes fetched modules to a
deterministic path based on SHA256. This PR adds a check before the
async fetch: if the file already exists on disk, load it directly and
skip the HTTP fetch entirely.

It is a simple approach that forms base for the next PR.

**Risk Level**: Low
**Testing**: Added unit tests
**Docs Changes**: NA
**Release Notes**: Added
Platform Specific Features: NA
[Optional Runtime guard:] Not needed
[Optional Fixes #Issue]: NA
[Optional Fixes commit #PR or SHA]: NA
[Optional Deprecated:]: NA
[Optional [API
Considerations](https://github.com/envoyproxy/envoy/blob/main/api/review_checklist.md):]
NA

---------

Signed-off-by: Anurag Aggarwal <kanurag94@gmail.com>
Signed-off-by: Xuyang Tao <taoxuy@google.com>
nshipilov pushed a commit to nshipilov/envoy that referenced this pull request Apr 13, 2026
…yproxy#43974)

**Commit Message**: dynamic_modules: remote source: add caching for
fetched modules

**Motivation**
Follow up from envoyproxy#43818 and envoyproxy#43333
Next PR: `nack_on_cache_miss` support (NACK on cache miss + background
fetch)

**Additional Description**:

`newDynamicModuleFromBytes` already writes fetched modules to a
deterministic path based on SHA256. This PR adds a check before the
async fetch: if the file already exists on disk, load it directly and
skip the HTTP fetch entirely.

It is a simple approach that forms base for the next PR.

**Risk Level**: Low
**Testing**: Added unit tests
**Docs Changes**: NA
**Release Notes**: Added
Platform Specific Features: NA
[Optional Runtime guard:] Not needed
[Optional Fixes #Issue]: NA
[Optional Fixes commit #PR or SHA]: NA
[Optional Deprecated:]: NA
[Optional [API
Considerations](https://github.com/envoyproxy/envoy/blob/main/api/review_checklist.md):]
NA

---------

Signed-off-by: Anurag Aggarwal <kanurag94@gmail.com>
Signed-off-by: Nick Shipilov <nick.shipilov.n@gmail.com>
kpramesh2212 pushed a commit to kpramesh2212/envoy that referenced this pull request Apr 14, 2026
…yproxy#43974)

**Commit Message**: dynamic_modules: remote source: add caching for
fetched modules

**Motivation**
Follow up from envoyproxy#43818 and envoyproxy#43333
Next PR: `nack_on_cache_miss` support (NACK on cache miss + background
fetch)

**Additional Description**: 

`newDynamicModuleFromBytes` already writes fetched modules to a
deterministic path based on SHA256. This PR adds a check before the
async fetch: if the file already exists on disk, load it directly and
skip the HTTP fetch entirely.

It is a simple approach that forms base for the next PR.

**Risk Level**: Low
**Testing**: Added unit tests
**Docs Changes**: NA
**Release Notes**: Added
Platform Specific Features: NA
[Optional Runtime guard:] Not needed
[Optional Fixes #Issue]: NA
[Optional Fixes commit #PR or SHA]: NA
[Optional Deprecated:]: NA
[Optional [API
Considerations](https://github.com/envoyproxy/envoy/blob/main/api/review_checklist.md):]
NA

---------

Signed-off-by: Anurag Aggarwal <kanurag94@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants