apifrom.performance.connection_pool

Connection pooling utilities for APIFromAnything.

This module provides tools for efficient connection management in high-load scenarios, including connection pooling, monitoring, and optimization.

Overview

Classes

Classes

BaseMiddleware(**options):bases: abc.ABC

Base middleware class for APIFromAnything.

This abstract class defines the interface for middleware components. Middleware components can process requests and responses.

apifrom.performance.connection_pool.options

Options for the middleware.

Type:

dict

Initialize a new BaseMiddleware instance.

param **options:

Options for the middleware.

class apifrom.performance.connection_pool.ConnectionPool(factory, settings=None, validate_func=None, close_func=None, metrics=None)[source]
Parameters:

A generic connection pool for managing connections to external resources.

This class provides a pool of connections to external resources, such as databases, APIs, or services, and manages their lifecycle.

Initialize a connection pool.

param factory:

A factory function that creates new connections

param settings:

The connection pool settings

param validate_func:

A function that validates connections

param close_func:

A function that closes connections

param metrics:

A metrics instance for collecting pool metrics

apifrom.performance.connection_pool._close_connection(connection)
:async:

Close a connection.

param connection:

The connection to close

apifrom.performance.connection_pool._create_connection()
:async:

Create a new connection.

returns:

A new connection

apifrom.performance.connection_pool._validate_connection(connection)
:async:

Validate a connection.

param connection:

The connection to validate

returns:

True if the connection is valid, False otherwise

apifrom.performance.connection_pool.acquire()
:async:

Acquire a connection from the pool.

returns:

A connection from the pool

raises TimeoutError:

If no connection could be acquired within the timeout

raises RuntimeError:

If the pool is closed

apifrom.performance.connection_pool.close()
:async:

Close the connection pool.

This method closes all connections in the pool.

apifrom.performance.connection_pool.connection()
:async:

Get a connection from the pool as a context manager.

This method acquires a connection from the pool, yields it, and ensures it is released back to the pool when done.

Yields:

A connection from the pool

apifrom.performance.connection_pool.get_metrics()

Get the connection pool metrics.

returns:

The connection pool metrics

apifrom.performance.connection_pool.initialize()
:async:

Initialize the connection pool.

This method creates the minimum number of connections.

apifrom.performance.connection_pool.release(connection)
:async:

Release a connection back to the pool.

param connection:

The connection to release

class apifrom.performance.connection_pool.ConnectionPoolMetrics[source]

Collects and analyzes connection pool metrics.

This class collects connection pool metrics such as pool size, connection acquisition times, and utilization, and provides tools for analyzing and visualizing this data.

Initialize connection pool metrics.

apifrom.performance.connection_pool.print_summary()

Print a summary of the metrics data to the console.

apifrom.performance.connection_pool.record_connection_acquire(connection_id, acquisition_time)

Record a connection acquisition.

param connection_id:

The connection ID

param acquisition_time:

The time it took to acquire the connection in seconds

apifrom.performance.connection_pool.record_connection_error()

Record a connection error.

apifrom.performance.connection_pool.record_connection_release(connection_id, release_time)

Record a connection release.

param connection_id:

The connection ID

param release_time:

The time it took to release the connection in seconds

apifrom.performance.connection_pool.record_connection_request()

Record a connection request.

apifrom.performance.connection_pool.record_connection_timeout()

Record a connection timeout.

apifrom.performance.connection_pool.reset()

Reset all metrics data.

apifrom.performance.connection_pool.save(file_path)

Save the metrics data to a file.

param file_path:

The path to save the data to

apifrom.performance.connection_pool.to_dict()

Convert the metrics data to a dictionary.

returns:

A dictionary representation of the metrics data

apifrom.performance.connection_pool.to_json(pretty=True)

Convert the metrics data to a JSON string.

param pretty:

Whether to format the JSON with indentation

returns:

A JSON string representation of the metrics data

ConnectionPoolMiddleware(database_url = None, redis_url = None, settings = None, max_connections = 20, connection_timeout = 5.0, pool_recycle = 300, pool_pre_ping = True, **kwargs):bases: apifrom.middleware.base.BaseMiddleware

Middleware for providing connection pooling for API requests.

This middleware initializes connection pools for databases and other external services, and provides them to API requests through the request state.

Initialize the connection pool middleware.

param database_url:

The database connection URL

param redis_url:

The Redis connection URL

param settings:

Connection pool settings

param max_connections:

Maximum number of connections in the pool

param connection_timeout:

Connection timeout in seconds

param pool_recycle:

Connection recycle time in seconds

param pool_pre_ping:

Whether to ping connections before using them

param **kwargs:

Additional options

apifrom.performance.connection_pool._initialize_database_pool()
:async:

Initialize the database connection pool.

apifrom.performance.connection_pool._initialize_redis_pool()
:async:

Initialize the Redis connection pool.

apifrom.performance.connection_pool.dispatch(request, call_next)
:async:

Dispatch a request, providing connection pools.

param request:

The request to process

param call_next:

The next middleware or route handler

returns:

The response

apifrom.performance.connection_pool.get_metrics()

Get connection pool metrics.

returns:

A dictionary of metrics

apifrom.performance.connection_pool.initialize()
:async:

Initialize the connection pools.

apifrom.performance.connection_pool.print_metrics()

Print connection pool metrics.

apifrom.performance.connection_pool.process_request(request)
:async:

Process a request (required by BaseMiddleware).

param request:

The request to process

returns:

The processed request

apifrom.performance.connection_pool.process_response(response)
:async:

Process a response (required by BaseMiddleware).

param response:

The response to process

returns:

The processed response

apifrom.performance.connection_pool.shutdown()
:async:

Shutdown all connection pools.

class apifrom.performance.connection_pool.ConnectionPoolSettings(min_size=1, max_size=10, max_idle=5, max_lifetime=3600, acquire_timeout=10.0, idle_timeout=300.0, retry_limit=3, retry_delay=1.0, validate_on_acquire=True)[source]
Parameters:
  • min_size (int)

  • max_size (int)

  • max_idle (int)

  • max_lifetime (int)

  • acquire_timeout (float)

  • idle_timeout (float)

  • retry_limit (int)

  • retry_delay (float)

  • validate_on_acquire (bool)

Settings for a connection pool.

This class defines the settings for a connection pool, such as pool size, timeout, and retry parameters.

Initialize connection pool settings.

param min_size:

The minimum pool size

param max_size:

The maximum pool size

param max_idle:

The maximum number of idle connections

param max_lifetime:

The maximum connection lifetime in seconds

param acquire_timeout:

The connection acquisition timeout in seconds

param idle_timeout:

The idle connection timeout in seconds

param retry_limit:

The maximum number of connection retries

param retry_delay:

The delay between connection retries in seconds

param validate_on_acquire:

Whether to validate connections on acquisition

apifrom.performance.connection_pool.create_aggressive()
:classmethod:

Create aggressive connection pool settings.

These settings are optimized for high-throughput, low-latency scenarios.

returns:

ConnectionPoolSettings instance

apifrom.performance.connection_pool.create_balanced()
:classmethod:

Create balanced connection pool settings.

These settings provide a balance between performance and resource usage.

returns:

ConnectionPoolSettings instance

apifrom.performance.connection_pool.create_conservative()
:classmethod:

Create conservative connection pool settings.

These settings are optimized for reliability and resource efficiency.

returns:

ConnectionPoolSettings instance

class apifrom.performance.connection_pool.PoolManager[source]

Manages multiple connection pools.

This class manages multiple connection pools for different resources, such as databases, APIs, or services.

Initialize a pool manager.

apifrom.performance.connection_pool.close_all()
:async:

Close all connection pools.

apifrom.performance.connection_pool.close_pool(name)
:async:

Close a connection pool.

param name:

The name of the pool

apifrom.performance.connection_pool.create_pool(name, factory, settings=None, validate_func=None, close_func=None)
:async:

Create a new connection pool.

param name:

The name of the pool

param factory:

A factory function that creates new connections

param settings:

The connection pool settings

param validate_func:

A function that validates connections

param close_func:

A function that closes connections

returns:

The created connection pool

apifrom.performance.connection_pool.get_metrics()

Get metrics for all connection pools.

returns:

A dictionary mapping pool names to metrics

apifrom.performance.connection_pool.get_pool(name)
:async:

Get a connection pool by name.

param name:

The name of the pool

returns:

The connection pool, or None if not found

apifrom.performance.connection_pool.print_summary()

Print a summary of all connection pools.

class apifrom.performance.connection_pool.Request(request=None, path_params=None, method=None, path=None, query_params=None, headers=None, body=None, client_ip=None)[source]
Parameters:
  • request (Optional[starlette.requests.Request])

  • path_params (Optional[dict[Any, Any]])

  • method (Optional[str])

  • path (Optional[str])

  • query_params (Optional[dict[Any, Any]])

  • headers (Optional[dict[Any, Any]])

  • body (Optional[Union[str, bytes]])

  • client_ip (Optional[str])

Request class for APIFromAnything.

This class wraps a Starlette request and provides methods for accessing request data in a convenient way.

apifrom.performance.connection_pool._request

The underlying Starlette request.

apifrom.performance.connection_pool.path_params

Path parameters extracted from the URL.

apifrom.performance.connection_pool.query_params

Query parameters extracted from the URL.

apifrom.performance.connection_pool.headers

HTTP headers.

apifrom.performance.connection_pool.method

HTTP method.

apifrom.performance.connection_pool.path

Request path.

apifrom.performance.connection_pool._body

Cached request body.

Initialize a new Request instance.

param request:

The underlying Starlette request.

param path_params:

Path parameters extracted from the URL.

param method:

The HTTP method.

param path:

The request path.

param query_params:

Query parameters.

param headers:

HTTP headers.

param body:

Request body.

param client_ip:

Client IP address.

class apifrom.performance.connection_pool.Response(content=None, status_code=200, headers=None, content_type='application/json')[source]
Parameters:
  • content (Any)

  • status_code (int)

  • headers (Optional[Dict[str, str]])

  • content_type (str)

Response class for APIFromAnything.

This class represents an HTTP response and provides methods for setting response data, status code, and headers.

apifrom.performance.connection_pool.content

The response content.

apifrom.performance.connection_pool.status_code

The HTTP status code.

apifrom.performance.connection_pool.headers

HTTP headers.

apifrom.performance.connection_pool.content_type

The content type of the response.

Initialize a new Response instance.

param content:

The response content.

param status_code:

The HTTP status code.

param headers:

HTTP headers.

param content_type:

The content type of the response.