pryscraper/browser_pool.py
cryptorugmunch bb77eb5f35 chore(license): re-license to dual MIT (core) + BSL 1.1 (stealth)
Re-license Pry from full Proprietary to a dual-license model:

- Core engine, extraction, templates (80+), MCP server, x402 payment rail,
  CLI, SDK, browser extension, WordPress plugin, Shopify app, and
  llm_providers: MIT (see LICENSE)
- Anti-detection / stealth subset (15 files): BSL 1.1 with Change Date
  2029-01-01 (see LICENSE-BSL-STEALTH)

BSL files (anti-detection moat):
  ultimate_scraper.py, stealth_engine.py, stealth_scripts/*.js (6),
  camoufox_integration.py, tls_fingerprint.py, cookie_warmer.py,
  behavioral_biometrics.py, adaptive.py, browser_pool.py, network.py,
  captcha_solver.py, shadow_dom.py, lazy_load.py, signup_automator.py,
  auth_connector.py

This enables community contributions to the core engine (templates,
integrations, MCP tools) while protecting the anti-detection techniques
that constitute the actual competitive moat. BSL Additional Use Grant
permits free non-production use; production deployment requires a
commercial license from enterprise@rugmunch.io.

Changes:
- Replace proprietary LICENSE with MIT LICENSE + new LICENSE-BSL-STEALTH
- Add SPDX-License-Identifier headers to 300+ source files
- Add docs/adr/0002-dual-licensing.md (ADR documenting the decision)
- Update README.md: new License section with BSL Additional Use Grant
- Update LICENSING_PRICING_STRATEGY.md: Section 3 (PryScraper) for dual license
- Update AGENTS.md: license line in header + new rule 8 (PRs touching BSL rejected)
- Update pyproject.toml: license = "MIT AND BSL-1.1" + classifiers + license-files
- Update DECISIONS.md index with ADR-0002
- Update STATUS.md (2026-07-03) and PLAN.md sprint goals

Refs: ADR-0002
2026-07-02 19:49:21 +02:00

102 lines
3.2 KiB
Python

"""Pry — shared Playwright browser pool.
Reuses browser instances across requests instead of launching per scrape."""
# SPDX-License-Identifier: BSL-1.1
# Copyright (c) 2026 Rug Munch Media LLC
#
# Part of Pry — Stealth / Anti-Detection Module
# Licensed under Business Source License 1.1 — see LICENSE-BSL-STEALTH.
# Change Date: 2029-01-01 (converts to MIT).
import asyncio
import logging
from typing import Any
logger = logging.getLogger(__name__)
class BrowserPool:
"""Warm pool of Playwright browser instances.
Maximum `max_browsers` chromium instances. Each can create multiple
contexts/pages. Browsers are kept warm and reused across requests.
"""
def __init__(self, max_browsers: int = 2, headless: bool = True):
self.max_browsers = max_browsers
self.headless = headless
self._playwright: Any = None
self._browsers: list[Any] = []
self._lock = asyncio.Lock()
self._initialized = False
async def start(self) -> None:
"""Launch the Playwright manager and pre-warm browsers."""
from playwright.async_api import async_playwright
self._playwright = await async_playwright().start()
for _ in range(self.max_browsers):
browser = await self._launch_browser()
self._browsers.append(browser)
self._initialized = True
logger.info("browser_pool_started", extra={"browsers": self.max_browsers})
async def _launch_browser(self) -> Any:
"""Launch a single Chromium instance."""
assert self._playwright is not None
return await self._playwright.chromium.launch(
headless=self.headless,
args=[
"--no-sandbox",
"--disable-setuid-sandbox",
"--disable-dev-shm-usage",
"--disable-gpu",
],
)
async def get_browser(self) -> Any:
"""Get a browser from the pool (round-robin)."""
async with self._lock:
if not self._initialized:
await self.start()
if not self._browsers:
browser = await self._launch_browser()
self._browsers.append(browser)
# Simple round-robin: return first, rotate
browser = self._browsers.pop(0)
self._browsers.append(browser)
return browser
async def close(self) -> None:
"""Close all browsers and stop Playwright."""
for b in self._browsers:
try:
await b.close()
except Exception:
logger.exception("browser_close_failed")
self._browsers.clear()
if self._playwright:
await self._playwright.stop()
self._initialized = False
logger.info("browser_pool_stopped")
# Module-level singleton
_pool: BrowserPool | None = None
async def get_browser_pool() -> BrowserPool:
"""Get or create the global browser pool."""
global _pool
if _pool is None:
_pool = BrowserPool()
await _pool.start()
return _pool
async def close_browser_pool() -> None:
"""Close the global browser pool."""
global _pool
if _pool:
await _pool.close()
_pool = None