All files / src/core browser-pool.ts

73.33% Statements 77/105
42.85% Branches 9/21
66.66% Functions 14/21
73% Lines 73/100

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 3341x 1x 1x                               1x 7x       7x               7x 7x 7x 7x 7x                         11x           11x   11x 1x         1x             1x 1x   1x 1x       10x 10x         10x 10x   10x 10x                             12x   10x 10x         10x 10x 10x 10x                                           6x 6x           6x 6x                       7x         7x 7x   7x 6x 1x       7x 1x       1x 1x 1x 1x 1x       1x                     8x 8x   8x 8x     9x 8x   8x 8x             10x           10x 10x           10x 10x   10x                   10x 10x             10x 10x 10x                                                                                                 7x 6x                                   1x                                    
import { chromium, Page, LaunchOptions } from 'playwright'; // Added Browser for clarity
import { EventEmitter } from 'eventemitter3';
import { v4 as uuidv4 } from 'uuid';
import { BrowserInstance, BrowserPoolConfig, Logger } from './types';
 
/**
 * @file Implements a pool for managing Playwright browser instances.
 * This helps in reusing browser instances to improve performance and control resource usage
 * by limiting the maximum number of concurrent browsers.
 */
 
/**
 * Manages a pool of Playwright browser instances.
 * This class handles the creation, acquisition, release, and cleanup of browser instances,
 * ensuring efficient reuse and adherence to configured limits (e.g., max size, instance age).
 * It emits events related to pool operations like 'pool:acquire', 'pool:release', 'pool:cleanup'.
 * @extends EventEmitter
 */
export class BrowserPool extends EventEmitter {
  private pool: BrowserInstance[] = [];
  private config: BrowserPoolConfig;
  private logger: Logger;
  private cleanupTimer?: ReturnType<typeof setTimeout>;
  private isShuttingDown = false;
 
  /**
   * Creates an instance of BrowserPool.
   * @param config The configuration object for the browser pool. See {@link BrowserPoolConfig}.
   * @param logger An instance of a logger conforming to the {@link Logger} interface.
   */
  constructor(config: BrowserPoolConfig, logger: Logger) {
    super();
    this.config = config;
    this.logger = logger;
    this.startCleanupTimer();
    this.logger.info('BrowserPool initialized', { maxSize: config.maxSize, maxAge: config.maxAge });
  }
 
  /**
   * Acquires a browser instance from the pool.
   * If an idle instance is available, it's reused.
   * If the pool is not full, a new instance is created.
   * If the pool is full and no instances are idle, it waits for one to become available.
   * Emits 'pool:acquire' event when an instance is acquired.
   * @returns A Promise that resolves to an available {@link BrowserInstance}.
   * @throws Error if the pool is shutting down or if waiting for an instance times out.
   */
  async acquire(): Promise<BrowserInstance> {
    Iif (this.isShuttingDown) {
      this.logger.warn('Attempted to acquire browser instance while pool is shutting down.');
      throw new Error('Browser pool is shutting down');
    }
 
    // Try to find an available instance
    const availableInstance = this.pool.find(instance => !instance.inUse);
 
    if (availableInstance) {
      this.logger.debug('Reusing browser instance from pool', {
        instanceId: availableInstance.id,
      });
 
      // Check if page is still valid
      Iif (availableInstance.page.isClosed()) {
        this.logger.debug('Page closed, creating new page in existing context', {
          instanceId: availableInstance.id,
        });
        availableInstance.page = await availableInstance.context.newPage();
      }
 
      availableInstance.inUse = true;
      availableInstance.lastUsed = Date.now();
 
      this.emit('pool:acquire', { instanceId: availableInstance.id });
      return availableInstance;
    }
 
    // Create new instance if pool is not full
    if (this.pool.length < this.config.maxSize) {
      this.logger.debug('Creating new browser instance', {
        poolSize: this.pool.length,
        maxSize: this.config.maxSize,
      });
 
      const instance = await this.createInstance();
      this.pool.push(instance);
 
      this.emit('pool:acquire', { instanceId: instance.id });
      return instance;
    }
 
    // Wait for an instance to become available
    this.logger.debug('Pool full, waiting for available instance');
    return this.waitForAvailableInstance();
  }
 
  /**
   * Releases a previously acquired browser instance back to the pool,
   * marking it as available for reuse.
   * Emits 'pool:release' event.
   * @param instance The {@link BrowserInstance} to release.
   */
  release(instance: BrowserInstance): void {
    const poolInstance = this.pool.find(i => i.id === instance.id);
 
    if (poolInstance) {
      Iif (!poolInstance.inUse) {
        this.logger.warn('Attempted to release an instance that was not marked as in-use.', {
          instanceId: instance.id,
        });
      }
      poolInstance.inUse = false;
      poolInstance.lastUsed = Date.now();
      this.logger.debug('Released browser instance to pool', { instanceId: poolInstance.id });
      this.emit('pool:release', { instanceId: poolInstance.id });
    } else E{
      this.logger.warn('Attempted to release an unknown or already destroyed browser instance.', {
        instanceId: instance.id,
      });
    }
  }
 
  /**
   * Retrieves statistics about the current state of the browser pool.
   * @returns An object containing:
   *  - `total`: The total number of browser instances currently managed by the pool (both active and idle).
   *  - `inUse`: The number of browser instances currently acquired and in use.
   *  - `available`: The number of idle browser instances available for immediate acquisition.
   *  - `maxSize`: The maximum number of browser instances the pool is configured to allow.
   */
  getStats(): {
    total: number;
    inUse: number;
    available: number;
    maxSize: number;
  } {
    const inUseCount = this.pool.filter(i => i.inUse).length;
    const stats = {
      total: this.pool.length,
      inUse: inUseCount,
      available: this.pool.length - inUseCount,
      maxSize: this.config.maxSize,
    };
    this.logger.debug('Browser pool stats requested.', stats);
    return stats;
  }
 
  /**
   * Periodically cleans up old and unused browser instances from the pool.
   * An instance is considered old if it has not been used for a duration exceeding
   * `config.maxAge` and is not currently in use.
   * This method is typically called by an internal timer.
   * Emits 'pool:cleanup' event with the number of removed instances.
   * @returns A Promise that resolves when the cleanup operation is complete.
   */
  async cleanupOldInstances(): Promise<void> {
    Iif (this.isShuttingDown) {
      this.logger.debug('Cleanup skipped as pool is shutting down.');
      return;
    }
 
    const now = Date.now();
    const instancesToRemove: BrowserInstance[] = [];
 
    for (const instance of this.pool) {
      if (!instance.inUse && now - instance.lastUsed > this.config.maxAge) {
        instancesToRemove.push(instance);
      }
    }
 
    if (instancesToRemove.length > 0) {
      this.logger.debug('Cleaning up old browser instances', {
        count: instancesToRemove.length,
      });
 
      for (const instance of instancesToRemove) {
        await this.destroyInstance(instance);
        const index = this.pool.indexOf(instance);
        if (index > -1) {
          this.pool.splice(index, 1);
        }
      }
 
      this.emit('pool:cleanup', { removedInstances: instancesToRemove.length });
    }
  }
 
  /**
   * Shuts down the browser pool.
   * This process involves stopping the cleanup timer and closing all active browser instances
   * managed by the pool. No new instances can be acquired after shutdown is initiated.
   * @returns A Promise that resolves when all browser instances have been closed and resources are freed.
   */
  async shutdown(): Promise<void> {
    this.logger.info('Shutting down browser pool...');
    this.isShuttingDown = true;
 
    if (this.cleanupTimer) {
      clearInterval(this.cleanupTimer);
    }
 
    const closePromises = this.pool.map(instance => this.destroyInstance(instance));
    await Promise.all(closePromises);
 
    this.pool = [];
    this.logger.info('Browser pool shutdown complete');
  }
 
  /**
   * Create a new browser instance
   */
  private async createInstance(): Promise<BrowserInstance> {
    const launchOptions: LaunchOptions = {
      headless: this.config.launchOptions.headless,
      args: this.config.launchOptions.args,
      timeout: this.config.launchOptions.timeout,
    };
 
    const browser = await chromium.launch(launchOptions);
    const context = await browser.newContext({
      viewport: { width: 1920, height: 1080 },
      userAgent:
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    });
 
    const page = await context.newPage();
    const id = uuidv4();
 
    const instance: BrowserInstance = {
      id,
      browser,
      context,
      page,
      lastUsed: Date.now(),
      inUse: true,
      createdAt: Date.now(),
    };
 
    this.logger.debug('Created new browser instance', { instanceId: id });
    return instance;
  }
 
  /**
   * Destroy a browser instance
   */
  private async destroyInstance(instance: BrowserInstance): Promise<void> {
    try {
      await instance.browser.close();
      this.logger.debug('Destroyed browser instance', { instanceId: instance.id });
    } catch (error) {
      this.logger.error('Error destroying browser instance', {
        instanceId: instance.id,
        error: error instanceof Error ? error.message : String(error),
      });
    }
  }
 
  /**
   * Wait for an available instance
   */
  private async waitForAvailableInstance(): Promise<BrowserInstance> {
    return new Promise((resolve, reject) => {
      const timeout = setTimeout(() => {
        reject(new Error('Timeout waiting for available browser instance'));
      }, 30000);
 
      const checkInterval = setInterval(() => {
        const availableInstance = this.pool.find(instance => !instance.inUse);
 
        Iif (availableInstance) {
          clearInterval(checkInterval);
          clearTimeout(timeout);
 
          availableInstance.inUse = true;
          availableInstance.lastUsed = Date.now();
 
          // Check if page is still valid
          if (availableInstance.page.isClosed()) {
            availableInstance.context
              .newPage()
              .then((page: Page) => {
                availableInstance.page = page;
                resolve(availableInstance);
              })
              .catch(reject);
          } else {
            resolve(availableInstance);
          }
        }
      }, 200);
    });
  }
 
  /**
   * Start the cleanup timer
   */
  private startCleanupTimer(): void {
    this.cleanupTimer = setInterval(() => {
      this.cleanupOldInstances().catch(error => {
        this.logger.error('Error during scheduled cleanup', {
          error: error instanceof Error ? error.message : String(error),
        });
      });
    }, this.config.cleanupInterval);
  }
}
 
/**
 * Provides a default configuration for the {@link BrowserPool}.
 * This configuration can be used as a base and customized as needed.
 * Key defaults include:
 * - `maxSize`: 5 browser instances.
 * - `maxAge`: 30 minutes for an instance before it's recycled.
 * - `headless`: True (or as per `BROWSER_HEADLESS` env var).
 * - `cleanupInterval`: 5 minutes.
 */
export const defaultBrowserPoolConfig: BrowserPoolConfig = {
  maxSize: 5,
  maxAge: 30 * 60 * 1000, // 30 minutes
  launchOptions: {
    headless: process.env.BROWSER_HEADLESS !== 'false',
    args: [
      '--no-sandbox',
      '--disable-setuid-sandbox',
      '--disable-dev-shm-usage',
      '--disable-accelerated-2d-canvas',
      '--no-first-run',
      '--no-zygote',
      '--disable-gpu',
    ],
    timeout: 30000,
  },
  cleanupInterval: 5 * 60 * 1000, // 5 minutes
};