pub struct BrowserPool { /* private fields */ }Expand description
Thread-safe pool of reusable BrowserInstances.
Maintains a warm set of idle browsers ready for immediate acquisition
(<100ms), and lazily launches new instances when demand spikes.
§Example
use stygian_browser::{BrowserConfig, BrowserPool};
let pool = BrowserPool::new(BrowserConfig::default()).await?;
let handle = pool.acquire().await?;
handle.release().await;Implementations§
Source§impl BrowserPool
impl BrowserPool
Sourcepub async fn new(config: BrowserConfig) -> Result<Arc<Self>>
pub async fn new(config: BrowserConfig) -> Result<Arc<Self>>
Create a new pool and pre-warm config.pool.min_size browser instances.
Warmup failures are logged but not fatal — the pool will start smaller and grow lazily.
§Example
use stygian_browser::{BrowserPool, BrowserConfig};
let pool = BrowserPool::new(BrowserConfig::default()).await?;Sourcepub async fn acquire(self: &Arc<Self>) -> Result<BrowserHandle>
pub async fn acquire(self: &Arc<Self>) -> Result<BrowserHandle>
Acquire a browser handle from the pool.
- If a healthy idle browser is available it is returned immediately.
- If
active < max_sizea new browser is launched. - Otherwise waits up to
pool.acquire_timeout.
§Errors
Returns BrowserError::PoolExhausted if no browser becomes available
within pool.acquire_timeout.
§Example
use stygian_browser::{BrowserPool, BrowserConfig};
let pool = BrowserPool::new(BrowserConfig::default()).await?;
let handle = pool.acquire().await?;
handle.release().await;Sourcepub async fn acquire_for(
self: &Arc<Self>,
context_id: &str,
) -> Result<BrowserHandle>
pub async fn acquire_for( self: &Arc<Self>, context_id: &str, ) -> Result<BrowserHandle>
Acquire a browser scoped to context_id.
Browsers obtained this way are isolated: they will only be reused by
future calls to acquire_for with the same context_id.
The global max_size still applies across all contexts.
§Errors
Returns BrowserError::PoolExhausted if no browser becomes available
within pool.acquire_timeout.
§Example
use stygian_browser::{BrowserPool, BrowserConfig};
let pool = BrowserPool::new(BrowserConfig::default()).await?;
let a = pool.acquire_for("bot-a").await?;
let b = pool.acquire_for("bot-b").await?;
a.release().await;
b.release().await;Sourcepub async fn release_context(&self, context_id: &str) -> usize
pub async fn release_context(&self, context_id: &str) -> usize
Shut down and remove all idle browsers belonging to context_id.
Active handles for that context are unaffected — they will be disposed normally when released. Call this when a bot or tenant is deprovisioned.
Returns the number of browsers shut down.
§Example
use stygian_browser::{BrowserPool, BrowserConfig};
let pool = BrowserPool::new(BrowserConfig::default()).await?;
let released = pool.release_context("bot-a").await;
println!("Shut down {released} browsers for bot-a");Sourcepub async fn context_ids(&self) -> Vec<String>
pub async fn context_ids(&self) -> Vec<String>
List all active context IDs that have idle browsers in the pool.
§Example
use stygian_browser::{BrowserPool, BrowserConfig};
let pool = BrowserPool::new(BrowserConfig::default()).await?;
let ids = pool.context_ids().await;
println!("Active contexts: {ids:?}");