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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
const puppeteer = require('puppeteer');
const EventEmitter = require('events');

class BrowserInstance {
  constructor(browser, options = {}) {
    this.browser = browser;
    this.id = this.generateId();
    this.createdAt = Date.now();
    this.lastUsed = Date.now();
    this.isIdle = true;
    this.usageCount = 0;
    this.maxIdleTime = options.maxIdleTime || 300000; // 5 minutes
    this.maxLifetime = options.maxLifetime || 1800000; // 30 minutes
    this.maxUsageCount = options.maxUsageCount || 100;
    this.activeTabs = new Set();
  }

  generateId() {
    return `browser_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
  }

  async newPage() {
    const page = await this.browser.newPage();
    const pageId = `page_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;

    this.activeTabs.add(pageId);
    this.usageCount++;
    this.lastUsed = Date.now();
    this.isIdle = false;

    // Wrap page.close to clean up from our tracking
    const originalClose = page.close.bind(page);
    page.close = async () => {
      this.activeTabs.delete(pageId);
      if (this.activeTabs.size === 0) {
        this.isIdle = true;
      }
      return originalClose();
    };

    return page;
  }

  isExpired() {
    const now = Date.now();
    const idleTime = now - this.lastUsed;
    const lifetime = now - this.createdAt;

    return (
      (this.isIdle && idleTime > this.maxIdleTime) ||
      lifetime > this.maxLifetime ||
      this.usageCount > this.maxUsageCount ||
      this.browser.isConnected() === false
    );
  }

  async close() {
    try {
      // Close all active pages first
      for (const page of await this.browser.pages()) {
        try {
          await page.close();
        } catch (error) {
          console.warn(`Error closing page: ${error.message}`);
        }
      }

      await this.browser.close();
      console.log(`Browser ${this.id} closed successfully`);
    } catch (error) {
      console.error(`Error closing browser ${this.id}:`, error.message);
    }
  }

  getStatus() {
    const now = Date.now();
    return {
      id: this.id,
      isIdle: this.isIdle,
      usageCount: this.usageCount,
      activeTabs: this.activeTabs.size,
      idleTime: now - this.lastUsed,
      lifetime: now - this.createdAt,
      isExpired: this.isExpired(),
      isConnected: this.browser.isConnected()
    };
  }
}

class BrowserPool extends EventEmitter {
  constructor(options = {}) {
    super();

    this.maxSize = options.maxSize || 3;
    this.minSize = options.minSize || 1;
    this.launchOptions = options.launchOptions || {
      headless: 'new',
      executablePath: process.env.PUPPETEER_EXECUTABLE_PATH || '/usr/bin/chromium',
      args: [
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--disable-dev-shm-usage',
        '--disable-gpu',
        '--disable-web-security',
        '--single-process'
      ]
    };

    this.instanceOptions = {
      maxIdleTime: options.maxIdleTime || 300000,
      maxLifetime: options.maxLifetime || 1800000,
      maxUsageCount: options.maxUsageCount || 100
    };

    this.instances = new Map();
    this.queue = [];
    this.isShuttingDown = false;

    // Stats
    this.stats = {
      created: 0,
      destroyed: 0,
      acquired: 0,
      released: 0,
      errors: 0,
      currentSize: 0,
      queueSize: 0
    };

    // Start maintenance
    this.startMaintenance();
  }

  /**
   * Get a browser instance from the pool
   */
  async acquire(timeout = 30000) {
    if (this.isShuttingDown) {
      throw new Error('Browser pool is shutting down');
    }

    return new Promise((resolve, reject) => {
      const timeoutId = setTimeout(() => {
        reject(new Error(`Browser acquisition timeout after ${timeout}ms`));
      }, timeout);

      const handleRequest = async () => {
        try {
          clearTimeout(timeoutId);
          const instance = await this.getOrCreateInstance();
          this.stats.acquired++;
          resolve(instance);
        } catch (error) {
          clearTimeout(timeoutId);
          this.stats.errors++;
          reject(error);
        }
      };

      if (this.hasAvailableInstance()) {
        handleRequest();
      } else if (this.instances.size < this.maxSize) {
        handleRequest();
      } else {
        // Add to queue
        this.queue.push(handleRequest);
        this.stats.queueSize = this.queue.length;
        this.emit('queue', { size: this.queue.length, maxSize: this.maxSize });
      }
    });
  }

  /**
   * Release a browser instance back to the pool
   */
  async release(instance) {
    if (!instance || !this.instances.has(instance.id)) {
      return;
    }

    instance.isIdle = true;
    instance.lastUsed = Date.now();
    this.stats.released++;

    this.emit('release', { instanceId: instance.id, stats: instance.getStatus() });

    // Process queue if there are waiting requests
    if (this.queue.length > 0) {
      const handleRequest = this.queue.shift();
      this.stats.queueSize = this.queue.length;
      setImmediate(handleRequest);
    }
  }

  /**
   * Check if there's an available instance
   */
  hasAvailableInstance() {
    for (const instance of this.instances.values()) {
      if (instance.isIdle && !instance.isExpired()) {
        return true;
      }
    }
    return false;
  }

  /**
   * Get an existing instance or create a new one
   */
  async getOrCreateInstance() {
    // Try to find an available instance
    for (const instance of this.instances.values()) {
      if (instance.isIdle && !instance.isExpired()) {
        return instance;
      }
    }

    // Create new instance if under limit
    if (this.instances.size < this.maxSize) {
      return await this.createInstance();
    }

    throw new Error('No available browser instances and pool is at maximum capacity');
  }

  /**
   * Create a new browser instance
   */
  async createInstance() {
    try {
      console.log('Creating new browser instance...');
      const browser = await puppeteer.launch(this.launchOptions);
      const instance = new BrowserInstance(browser, this.instanceOptions);

      this.instances.set(instance.id, instance);
      this.stats.created++;
      this.stats.currentSize = this.instances.size;

      this.emit('create', { instanceId: instance.id, poolSize: this.instances.size });

      // Set up browser disconnect handler
      browser.on('disconnected', () => {
        this.handleBrowserDisconnect(instance.id);
      });

      console.log(`Browser instance ${instance.id} created. Pool size: ${this.instances.size}`);
      return instance;

    } catch (error) {
      this.stats.errors++;
      console.error('Failed to create browser instance:', error);
      throw error;
    }
  }

  /**
   * Handle browser disconnect
   */
  handleBrowserDisconnect(instanceId) {
    const instance = this.instances.get(instanceId);
    if (instance) {
      console.warn(`Browser ${instanceId} disconnected unexpectedly`);
      this.instances.delete(instanceId);
      this.stats.destroyed++;
      this.stats.currentSize = this.instances.size;
      this.emit('disconnect', { instanceId, poolSize: this.instances.size });
    }
  }

  /**
   * Remove expired instances
   */
  async cleanupExpiredInstances() {
    const expiredInstances = [];

    for (const [id, instance] of this.instances.entries()) {
      if (instance.isExpired()) {
        expiredInstances.push(id);
      }
    }

    for (const id of expiredInstances) {
      await this.destroyInstance(id, 'expired');
    }

    if (expiredInstances.length > 0) {
      console.log(`Cleaned up ${expiredInstances.length} expired browser instances`);
    }
  }

  /**
   * Destroy a specific instance
   */
  async destroyInstance(instanceId, reason = 'manual') {
    const instance = this.instances.get(instanceId);
    if (!instance) {
      return;
    }

    try {
      await instance.close();
      this.instances.delete(instanceId);
      this.stats.destroyed++;
      this.stats.currentSize = this.instances.size;

      this.emit('destroy', {
        instanceId,
        reason,
        poolSize: this.instances.size,
        stats: instance.getStatus()
      });

      console.log(`Browser instance ${instanceId} destroyed (reason: ${reason})`);

    } catch (error) {
      console.error(`Error destroying browser instance ${instanceId}:`, error);
    }
  }

  /**
   * Ensure minimum pool size
   */
  async ensureMinimumSize() {
    const currentSize = this.instances.size;
    const needed = this.minSize - currentSize;

    if (needed > 0) {
      console.log(`Creating ${needed} browser instances to maintain minimum pool size`);

      const createPromises = [];
      for (let i = 0; i < needed; i++) {
        createPromises.push(this.createInstance().catch(error => {
          console.error('Failed to create browser instance for minimum pool size:', error);
        }));
      }

      await Promise.allSettled(createPromises);
    }
  }

  /**
   * Get pool status
   */
  getStatus() {
    const instances = Array.from(this.instances.values()).map(instance => instance.getStatus());

    return {
      poolSize: this.instances.size,
      maxSize: this.maxSize,
      minSize: this.minSize,
      queueSize: this.queue.length,
      isShuttingDown: this.isShuttingDown,
      stats: { ...this.stats },
      instances,
      healthy: instances.filter(i => i.isConnected && !i.isExpired).length,
      idle: instances.filter(i => i.isIdle).length,
      active: instances.filter(i => !i.isIdle).length,
      expired: instances.filter(i => i.isExpired).length
    };
  }

  /**
   * Start maintenance routine
   */
  startMaintenance() {
    this.maintenanceInterval = setInterval(async () => {
      try {
        await this.cleanupExpiredInstances();
        await this.ensureMinimumSize();

        // Log status periodically
        const status = this.getStatus();
        if (status.poolSize > 0) {
          console.log(`Browser pool status: ${status.poolSize}/${status.maxSize} instances, ${status.active} active, ${status.queueSize} queued`);
        }

      } catch (error) {
        console.error('Browser pool maintenance error:', error);
      }
    }, 30000); // Run every 30 seconds
  }

  /**
   * Stop maintenance routine
   */
  stopMaintenance() {
    if (this.maintenanceInterval) {
      clearInterval(this.maintenanceInterval);
      this.maintenanceInterval = null;
    }
  }

  /**
   * Shutdown the browser pool
   */
  async shutdown(timeout = 30000) {
    console.log('Shutting down browser pool...');
    this.isShuttingDown = true;
    this.stopMaintenance();

    // Reject all queued requests
    while (this.queue.length > 0) {
      const handleRequest = this.queue.shift();
      setImmediate(() => {
        handleRequest(new Error('Browser pool is shutting down'));
      });
    }

    // Close all instances
    const shutdownPromises = Array.from(this.instances.values()).map(async (instance) => {
      try {
        await instance.close();
      } catch (error) {
        console.error(`Error closing browser ${instance.id} during shutdown:`, error);
      }
    });

    // Wait for all instances to close or timeout
    const timeoutPromise = new Promise((resolve) => {
      setTimeout(resolve, timeout);
    });

    await Promise.race([
      Promise.allSettled(shutdownPromises),
      timeoutPromise
    ]);

    this.instances.clear();
    this.stats.currentSize = 0;

    console.log('Browser pool shutdown complete');
    this.emit('shutdown', { totalDestroyed: shutdownPromises.length });
  }

  /**
   * Health check
   */
  async healthCheck() {
    const status = this.getStatus();
    const healthyRatio = status.poolSize > 0 ? status.healthy / status.poolSize : 1;

    return {
      status: healthyRatio >= 0.5 ? 'healthy' : 'unhealthy',
      poolSize: status.poolSize,
      healthy: status.healthy,
      expired: status.expired,
      queueSize: status.queueSize,
      healthyRatio,
      details: status
    };
  }
}

module.exports = BrowserPool;