Connection Pool

class nextorm.pool.ConnectionPool[source]

Bases: object

Thread-safe synchronous connection pool.

Parameters

factory:

Zero-argument callable that creates and returns a new connection.

min_size:

Number of connections to pre-create at construction time (0 is valid).

max_size:

Maximum total number of connections. Once reached, acquire() blocks until a connection is released or timeout expires.

timeout:

Seconds to wait for a free connection before raising PoolTimeoutError.

__init__(factory, *, min_size=1, max_size=5, timeout=30.0)[source]
Parameters:
  • factory (Callable[[], Any])

  • min_size (int)

  • max_size (int)

  • timeout (float)

Return type:

None

acquire()[source]

Check out a connection from the pool.

Returns immediately when an idle connection is available. Creates a new connection when the pool is below max_size. Blocks up to timeout seconds when the pool is at max_size.

Raises

PoolTimeoutError

If no connection becomes available within the timeout.

Return type:

Any

release(conn)[source]

Return conn to the pool for reuse.

Parameters:

conn (Any)

Return type:

None

close_all()[source]

Close all idle connections in the pool.

Connections that are currently checked out are not affected; they will be closed when release() is called and the pool detects that it is shutting down (currently the pool does not track checked-out connections, so callers must ensure all connections are released before calling this method if a clean shutdown is required).

Return type:

None

property pool_size: int

Total number of connections created (idle + checked out).

property idle_count: int

Number of connections currently sitting idle in the pool.

class nextorm.pool.AsyncConnectionPool[source]

Bases: object

Async connection pool (uses asyncio synchronisation primitives).

Parameters

factory:

Async callable that creates and returns a new async connection.

min_size:

Number of connections to pre-create during start().

max_size:

Maximum total number of connections.

timeout:

Seconds to wait for a free connection before raising PoolTimeoutError.

__init__(factory, *, min_size=1, max_size=5, timeout=30.0)[source]
Parameters:
  • factory (Callable[[], Any])

  • min_size (int)

  • max_size (int)

  • timeout (float)

Return type:

None

async start()[source]

Pre-create min_size connections. Must be called inside an event loop.

Return type:

None

async acquire()[source]

Check out an async connection from the pool.

Raises

PoolTimeoutError

If no connection becomes available within the timeout.

Return type:

Any

async release(conn)[source]

Return conn to the pool for reuse.

Parameters:

conn (Any)

Return type:

None

async close_all()[source]

Close all idle connections.

Return type:

None

property pool_size: int

Total number of connections created.

property idle_count: int

Number of idle connections.

exception nextorm.pool.PoolTimeoutError[source]

Bases: Exception

Raised when all pool connections are in use and the wait times out.