Class ConnectionPool<T>

Connection pool for managing persistent WebSocket connections.

Reuses connections efficiently and automatically refreshes them after max duration. Prevents creating too many connections in a single conversation.

Type Parameters

  • T

Constructors

Methods

  • Close all connections, draining any pending connection closures.

    Returns Promise<void>

  • Get an available connection or create a new one if needed.

    Parameters

    • Optional timeout: number

      Connection timeout in milliseconds

    Returns Promise<T>

    An active connection object

  • Clear all existing connections.

    Marks all current connections to be closed during the next drain cycle.

    Returns void

  • Initiate prewarming of the connection pool without blocking.

    This method starts a background task that creates a new connection if none exist. The task automatically cleans itself up when the connection pool is closed.

    Returns void

  • Mark a connection as available for reuse.

    If connection has been removed, it will not be added to the pool.

    Parameters

    • conn: T

      The connection to make available

    Returns void

  • Remove a specific connection from the pool.

    Marks the connection to be closed during the next drain cycle.

    Parameters

    • conn: T

      The connection to remove

    Returns void

  • Get a connection from the pool and automatically return it when done. Handles abort signals and ensures proper cleanup.

    Type Parameters

    • R

    Parameters

    • fn: ((conn) => Promise<R>)

      Function to execute with the connection

        • (conn): Promise<R>
        • Parameters

          • conn: T

          Returns Promise<R>

    • Optional options: {
          signal?: AbortSignal;
          timeout?: number;
      }

      Options including timeout and abort signal

      • Optional signal?: AbortSignal
      • Optional timeout?: number

    Returns Promise<R>

    The result of the function