or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

data-structures.mdindex.mdpool-lifecycle.mdpool-management.mdpool-monitoring.mdresource-operations.md
tile.json

pool-management.mddocs/

Pool Management

Core functionality for creating and managing resource pools with comprehensive configuration options for connection pooling, resource reuse, and pool behavior customization.

Capabilities

createPool Function

Creates a new resource pool with the specified factory and configuration options.

/**
 * Creates a new resource pool
 * @param factory - Factory object for creating/destroying resources
 * @param options - Pool configuration options
 * @returns Pool instance
 */
function createPool(factory, options);

Usage Examples:

const genericPool = require('generic-pool');

// Database connection pool
const dbFactory = {
  create: () => {
    return new Promise((resolve, reject) => {
      const connection = new DatabaseConnection();
      connection.connect((err) => {
        if (err) reject(err);
        else resolve(connection);
      });
    });
  },
  destroy: (connection) => {
    return new Promise((resolve) => {
      connection.close(() => resolve());
    });
  },
  validate: (connection) => {
    return Promise.resolve(connection.isConnected());
  }
};

const pool = genericPool.createPool(dbFactory, {
  max: 10,
  min: 2,
  acquireTimeoutMillis: 3000,
  testOnBorrow: true
});

Factory Interface

Required interface for resource creation, destruction, and optional validation.

/**
 * Factory interface for managing pool resources
 */
interface Factory {
  /**
   * Creates a new resource - must return a Promise
   * @returns Promise that resolves to the created resource
   */
  create(): Promise<any>;

  /**
   * Destroys a resource - must return a Promise
   * @param resource - The resource to destroy
   * @returns Promise that resolves when destruction is complete
   */
  destroy(resource: any): Promise<void>;

  /**
   * Optional: Validates if a resource is still usable
   * @param resource - The resource to validate
   * @returns Promise that resolves to true if valid, false otherwise
   */
  validate?(resource: any): Promise<boolean>;
}

Usage Examples:

// Simple factory
const simpleFactory = {
  create: async () => {
    return new SomeResource();
  },
  destroy: async (resource) => {
    await resource.cleanup();
  }
};

// Factory with validation
const validatingFactory = {
  create: async () => {
    const client = new HttpClient();
    await client.connect();
    return client;
  },
  destroy: async (client) => {
    await client.disconnect();
  },
  validate: async (client) => {
    return client.isConnected() && !client.hasErrors();
  }
};

Pool Configuration Options

Comprehensive configuration interface for customizing pool behavior.

/**
 * Pool configuration options
 */
interface Options {
  /** Maximum number of resources to create (default: 1) */
  max?: number;
  
  /** Minimum number of resources to keep in pool (default: 0) */
  min?: number;
  
  /** Maximum number of queued acquire requests (default: unlimited) */
  maxWaitingClients?: number;
  
  /** Validate resources before lending (default: false) */
  testOnBorrow?: boolean;
  
  /** Validate resources when returned (default: false) */
  testOnReturn?: boolean;
  
  /** Timeout for acquire operations in ms (default: unlimited) */
  acquireTimeoutMillis?: number;
  
  /** Timeout for destroy operations in ms (default: unlimited) */
  destroyTimeoutMillis?: number;
  
  /** FIFO vs LIFO behavior for idle resources (default: true) */
  fifo?: boolean;
  
  /** Number of priority levels for queuing (default: 1) */
  priorityRange?: number;
  
  /** Start pool automatically on creation (default: true) */
  autostart?: boolean;
  
  /** Interval for eviction checks in ms (default: 0 - disabled) */
  evictionRunIntervalMillis?: number;
  
  /** Number of resources to check per eviction run (default: 3) */
  numTestsPerEvictionRun?: number;
  
  /** Soft idle timeout maintaining minimum pool size (default: -1) */
  softIdleTimeoutMillis?: number;
  
  /** Hard idle timeout for eviction (default: 30000) */
  idleTimeoutMillis?: number;
  
  /** Promise implementation to use (default: global Promise) */
  Promise?: typeof Promise;
}

Usage Examples:

// High-capacity database pool
const dbPool = genericPool.createPool(dbFactory, {
  max: 50,
  min: 5,
  maxWaitingClients: 100,
  acquireTimeoutMillis: 5000,
  testOnBorrow: true,
  evictionRunIntervalMillis: 60000,
  idleTimeoutMillis: 300000
});

// Priority-enabled API client pool
const apiPool = genericPool.createPool(apiFactory, {
  max: 20,
  priorityRange: 3, // 0=high, 1=medium, 2=low priority
  acquireTimeoutMillis: 10000
});

// Custom Promise implementation
const customPool = genericPool.createPool(factory, {
  max: 10,
  Promise: require('bluebird')
});

Pool Constructor

Direct Pool class constructor for advanced use cases.

/**
 * Pool class constructor (advanced usage)
 * @param Evictor - Evictor class for resource eviction
 * @param Deque - Deque class for queue management
 * @param PriorityQueue - PriorityQueue class for priority queuing
 * @param factory - Resource factory
 * @param options - Pool configuration
 */
class Pool extends EventEmitter {
  constructor(Evictor, Deque, PriorityQueue, factory, options);
}

Usage Examples:

const { Pool, DefaultEvictor, Deque, PriorityQueue } = require('generic-pool');

// Create pool with custom components
const customPool = new Pool(
  DefaultEvictor,
  Deque, 
  PriorityQueue,
  factory,
  options
);

Default Exports

All available exports from the generic-pool module.

/**
 * Module exports
 */
module.exports = {
  Pool: Pool,
  Deque: Deque,
  PriorityQueue: PriorityQueue,
  DefaultEvictor: DefaultEvictor,
  createPool: function(factory, config) {
    return new Pool(DefaultEvictor, Deque, PriorityQueue, factory, config);
  }
};