feat(docker): optimize concurrency performance and memory management#1689
Open
mzyfree wants to merge 3 commits intounclecode:mainfrom
Open
feat(docker): optimize concurrency performance and memory management#1689mzyfree wants to merge 3 commits intounclecode:mainfrom
mzyfree wants to merge 3 commits intounclecode:mainfrom
Conversation
This commit consolidates several optimizations for crawl4ai in high-concurrency environments: 1. Browser Pool Optimization: - Implemented a tiered browser pool (Hot, Cold, Retired). - Added a browser retirement mechanism based on usage count (MAX_USAGE_COUNT) and memory pressure (MEMORY_RETIRE_THRESHOLD). - Added reference counting (active_requests) to ensure browser instances are not closed while in use. - Enhanced the pool janitor with adaptive cleanup intervals based on system memory. 2. Resource Loading Optimization: - Integrated optional CSS and Ad blocking to reduce memory footprint and improve QPS. - Decoupled resource filtering from text_mode to allow granular control. 3. Stability and Scalability: - Added mandatory release_crawler calls in API/Server handlers to prevent resource leaks. - Introduced environment variables to toggle these new features (defaulting to False for safe community adoption). - Added optional 5-minute pool audit logs for better observability. Co-authored-by: dylan.min <dylan.min@example.com>
…eanup docs - Refactor BrowserManager to dynamically block resources based on avoid_css and text_mode - Align text_mode behavior with community standards (no forced CSS blocking) - Add Top 20 curated ad and tracker patterns for performance - Restore and translate permanent browser logs in crawler_pool.py - Clean up models.py schema annotations and server.py docstrings - Add unit and functional tests for filtering flags
01fb7ee to
47bc688
Compare
Author
|
@unclecode @ntohidi please review this MR |
|
@mzyfree +1, better supported in high concurrence envs is needed |
|
Look forward to it as well! The current performance is very POOR. The QPS is <1 for 2 CPU + 4GB RAM, tried to fetch 3 URLs in one request |
|
@ntohidi @aravindkarnam Pls help... |
|
Any update pls? |
|
Anyone is looking at this issue pls? |
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.
Summary
This PR introduces a comprehensive optimization suite for
crawl4aiin high-concurrency Docker environments. It focuses on improving QPS (Queries Per Second) and ensuring long-term memory stability by re-engineering the browser pooling mechanism and introducing optional resource filtering.Key Design Principle: All new features are opt-in. By default, the system behaves exactly as before, ensuring zero impact on existing community users.
Core Enhancements:
active_requeststracking to prevent browsers from being closed while still processing requests, fixing common "Target closed" errors under load.New Configuration Options
These new features can be enabled via
BrowserConfigor Environment Variables:Engine Layer (
BrowserConfig)avoid_ads(bool, default:False): Enable intercepting and blocking ad/tracker network requests.avoid_css(bool, default:False): Enable blocking CSS resource loading to save CPU/Memory.Docker Layer (Environment Variables)
CRAWL4AI_BROWSER_RETIREMENT_ENABLED(default:false): Enable the usage/memory-based retirement mechanism.CRAWL4AI_PERMANENT_BROWSER_DISABLED(default:false): If true, disables the always-on permanent browser instance.CRAWL4AI_POOL_AUDIT_ENABLED(default:false): Enable detailed pool status logging every 5 minutes.CRAWL4AI_BROWSER_MAX_USAGE(default:100): Max requests per instance before retirement.CRAWL4AI_MEMORY_RETIRE_THRESHOLD(default:75): System memory % to trigger aggressive retirement.List of files changed and why
crawl4ai/async_configs.py: Added new parameters toBrowserConfig.crawl4ai/browser_manager.py: Implemented the network interception logic for resource filtering.deploy/docker/crawler_pool.py: Implemented the tiered pool, retirement, and audit logic.deploy/docker/api.py&deploy/docker/server.py: Updated withtry...finallyfor accurate reference counting.How Has This Been Tested?
False) values.Checklist:
Stress test performance
QPS increased by 40%
Resource with no OOM