Skip to content

RLQS: Improve timer lifetime management#44236

Open
yanavlasov wants to merge 3 commits intoenvoyproxy:mainfrom
yanavlasov:rlqs-fix
Open

RLQS: Improve timer lifetime management#44236
yanavlasov wants to merge 3 commits intoenvoyproxy:mainfrom
yanavlasov:rlqs-fix

Conversation

@yanavlasov
Copy link
Copy Markdown
Contributor

  1. Refactored GlobalRateLimitClientImpl to inherit from std::enable_shared_from_this.
  2. Updated all asynchronous callbacks in GlobalRateLimitClientImpl to capture a weak_ptr to 'this' and use the lock-and-check pattern.
  3. Changed TlsStore to hold a std::shared_ptr and implement a custom deleter for std::shared_ptr to ensure main-thread cleanup, even when dropped by worker threads.
  4. Updated LocalRateLimitClientImpl to hold a std::shared_ptr, anchoring the lifetime of the global client and its resources for the duration of the filter.
  5. Updated unit tests to align with the new lifetime management model.

Risk Level: medium
Testing: unit tests
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a

Signed-off-by: Yan Avlasov <yavlasov@google.com>
@yanavlasov yanavlasov requested a review from tyxia as a code owner April 2, 2026 14:22
@yanavlasov
Copy link
Copy Markdown
Contributor Author

@bsurber for review

Copy link
Copy Markdown
Contributor

@bsurber bsurber left a comment

Choose a reason for hiding this comment

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

LGTM once you have the last bit of coverage

adisuissa
adisuissa previously approved these changes Apr 3, 2026
Copy link
Copy Markdown
Contributor

@adisuissa adisuissa 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!
Assigning @tyxia as code-owner and maintainer review.
/assign @tyxia

@adisuissa
Copy link
Copy Markdown
Contributor

Seems the coverage is broken.
/wait

@tyxia
Copy link
Copy Markdown
Member

tyxia commented Apr 17, 2026

/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 refactors the lifetime management of the rate limit quota filter's global client and associated resources by transitioning to shared_ptr and weak_ptr patterns. By introducing a TlsStore to anchor these resources, the changes ensure that the global client and thread-local bucket caches remain valid throughout the filter's lifecycle. Additionally, GlobalRateLimitClientImpl now utilizes std::enable_shared_from_this to safely handle asynchronous tasks on the main dispatcher. Review feedback suggests capturing the dispatcher by reference or retrieving it from the store object within the TlsStore deleter to enhance code clarity and safety.

Comment on lines +36 to +42
auto deleter = [main_dispatcher = &dispatcher](TlsStore* store) {
if (main_dispatcher->isThreadSafe()) {
delete store;
} else {
main_dispatcher->post([store]() { delete store; });
}
};
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

Capturing the dispatcher by pointer &dispatcher is safe here because the main thread dispatcher in Envoy is a long-lived object that outlives the filter factories and filters. However, for clarity and to avoid any potential issues with pointer validity if the context were different, it is recommended to capture the dispatcher by reference or retrieve it from the store object inside the deleter (since TlsStore holds a reference to it). This aligns with the repository guideline that in complex code, it is acceptable to keep cheap safety checks for added safety.

References
  1. In complex code, it is acceptable to keep cheap safety checks, even if they appear redundant, for added safety.

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.

optional change

tyxia
tyxia previously approved these changes Apr 20, 2026
Copy link
Copy Markdown
Member

@tyxia tyxia left a comment

Choose a reason for hiding this comment

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

Nice change! Thanks @yanavlasov ! Please fix the test coverage failure

✗ source/extensions/filters/http/rate_limit_quota: 96.5% (threshold: 96.6%)

Signed-off-by: Yan Avlasov <yavlasov@google.com>
@yanavlasov yanavlasov dismissed stale reviews from tyxia and adisuissa via 685a61b April 23, 2026 15:14
@tyxia
Copy link
Copy Markdown
Member

tyxia commented Apr 27, 2026

/retest

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.

4 participants