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

  • 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

  • Drop every current connection so the next checkout connects fresh, for example after the connection settings changed.

    Idle connections close on the next drain. A checked-out connection is still serving a request, so it is retired instead: it finishes and then closes on return.

    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

  • Release every current connection without interrupting in-flight work, and abort a pending prewarm.

    Idle connections close now. A checked-out connection finishes its request and then closes on return instead of rejoining the pool. Use this when the pool's owner is done with it, for example an agent that handed off. The pool stays usable: the next get() or prewarm() connects fresh rather than reviving a released connection.

    Returns Promise<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