Repository navigation
fix: process memory usage method high cpu utilization - #4455
Conversation
Summary by CodeRabbit
WalkthroughA configurable Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Middleware
participant Process
Client->>Middleware: Incoming request
alt If refresh period elapsed
Middleware->>Process: process.memoryUsage()
Process-->>Middleware: heapUsed
Middleware->>Middleware: Cache heapUsed and timestamp
else If within refresh period
Middleware->>Middleware: Use cached heapUsed
end
Middleware-->>Client: Allow or block request based on heapUsed
Possibly related PRs
Suggested reviewers
Suggestions for improvement:
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/middlewares/memoryFencing.ts (1)
14-14: Add JSDoc documentation for the new parameter.The
memoryUsageRefreshPeriodparameter lacks documentation, which breaks consistency with the existing interface documentation pattern.interface MemoryFenceOptions { + /** + * The interval in milliseconds between memory usage checks. + * Defaults to 100ms to reduce CPU overhead from frequent memory sampling. + */ memoryUsageRefreshPeriod?: number; // default 100ms
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/index.ts(1 hunks)src/middlewares/__tests__/memoryFencing.test.ts(1 hunks)src/middlewares/memoryFencing.ts(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (10)
- GitHub Check: Check PR Title
- GitHub Check: Build User Transformer Docker Image - PR / Build Transformer Docker Image AMD64
- GitHub Check: Build User Transformer Docker Image - PR / Build Transformer Docker Image ARM64
- GitHub Check: Build Transformer Docker Image - PR / Build Transformer Docker Image AMD64
- GitHub Check: Build Transformer Docker Image - PR / Build Transformer Docker Image ARM64
- GitHub Check: Code Coverage
- GitHub Check: test_and_publish
- GitHub Check: UT Tests
- GitHub Check: Analyze (javascript-typescript)
- GitHub Check: Analyze (go)
🔇 Additional comments (3)
src/middlewares/memoryFencing.ts (1)
39-46: Well-implemented caching mechanism.The caching logic correctly stores memory usage state outside the request handler, creating proper closure-based caching that persists across requests. The time-based refresh logic ensures memory usage is only sampled after the configured period, effectively reducing CPU overhead.
src/index.ts (1)
39-43: Clean configuration integration.The environment variable parsing follows the established pattern and correctly defaults to 100ms, matching the middleware's default behavior. The configuration is properly integrated into the existing middleware setup.
src/middlewares/__tests__/memoryFencing.test.ts (1)
149-176: Comprehensive test coverage for the new caching functionality.The test effectively validates the periodic memory checking behavior by:
- Mocking time control to simulate passage of time
- Verifying memory usage is cached and not called on every request
- Testing the exact refresh threshold boundary (99ms vs 100ms)
- Ensuring proper initialization behavior
This provides excellent coverage for the new optimization feature.
|
Allure Test reports for this run are available at:
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #4455 +/- ##
===========================================
- Coverage 91.22% 91.21% -0.01%
===========================================
Files 639 639
Lines 33588 33615 +27
Branches 7931 7935 +4
===========================================
+ Hits 30640 30662 +22
- Misses 2681 2686 +5
Partials 267 267 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
chore: process.memoryUsage utilizing cpu



Description
Updating memory usage every 100ms to avoid increased cpu utilization