Simplify browser management with single thread-safe instance#63
Merged
excid3 merged 1 commit intoexcid3:mainfrom Jul 17, 2025
Merged
Simplify browser management with single thread-safe instance#63excid3 merged 1 commit intoexcid3:mainfrom
excid3 merged 1 commit intoexcid3:mainfrom
Conversation
2cf950a to
97f8b28
Compare
Replace multiple browser registry system with single thread-safe browser instance to ensure only one Chrome process per Ruby process. **Breaking Changes:** - Removed `add_browser` method and `browsers` hash - Removed `browser:` parameter from render methods **New Features:** - Added `FerrumPdf.browser = browser_instance` setter - Added `FerrumPdf.with_browser` method for thread-safe browser access - Auto-creation of browser from config when none is set - Thread-safe mutex protection for concurrent access **Benefits:** - Guarantees single Chrome instance per Ruby process - Thread-safe for background jobs and concurrent requests - Simplified codebase with reduced complexity - Users can still provide custom browser instances
97f8b28 to
b5a5648
Compare
excid3
reviewed
Jul 17, 2025
| # This automatically applies HTML preprocessing if `html:` is present | ||
| # | ||
| def load_page(url: nil, html: nil, base_url: nil, authorize: nil, wait_for_idle_options: nil, browser: :default, retries: 1) | ||
| def load_page(url: nil, html: nil, base_url: nil, authorize: nil, wait_for_idle_options: nil, retries: 1) |
Owner
There was a problem hiding this comment.
I wonder if we should keep the browser: option, just in case someone needs to provide a separate browser instance.
That wouldn't need to quit the existing browser like reassigning the default browser would.
excid3
reviewed
Jul 17, 2025
Comment on lines
+43
to
+48
| def with_browser | ||
| browser_mutex.synchronize do | ||
| @@browser ||= Ferrum::Browser.new(config) | ||
| @@browser.restart unless @@browser.client.present? | ||
| yield @@browser | ||
| end |
Owner
There was a problem hiding this comment.
Suggested change
| def with_browser | |
| browser_mutex.synchronize do | |
| @@browser ||= Ferrum::Browser.new(config) | |
| @@browser.restart unless @@browser.client.present? | |
| yield @@browser | |
| end | |
| def with_browser(browser=nil) | |
| if browser.present? | |
| yield browser | |
| else | |
| browser_mutex.synchronize do | |
| @@browser ||= Ferrum::Browser.new(config) | |
| @@browser.restart unless @@browser.client.present? | |
| yield @@browser | |
| end | |
| end |
If we want to support the browser: nil option for render_pdf to let users pass in their own separate browser, we could do something like this.
Owner
|
I think we should probably keep |
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
Breaking Changes
add_browsermethod andbrowsershashbrowser:parameter fromrender_pdf/render_screenshotmethodsNew API
FerrumPdf.browser = browser_instancesetter for custom browsersFerrumPdf.with_browsermethod for thread-safe browser accessFerrumPdf.browser = nilBrowser Configuration Discussion
This PR introduces two ways to configure the browser:
FerrumPdf.configure { |c| c.window_size = [800, 600] }FerrumPdf.browser = Ferrum::Browser.new(window_size: [800, 600])Is it worth maintaining two paths for configuring the browser? Since the simplest use case doesn't have to write a config at all and just uses the library's default config, it might be. It is a good time to consider removing
.configure, though.Benefits
configureblock (for now)Testing
🤖 Generated with Claude Code