apifrom.performance.request_coalescing ====================================== Request coalescing module for APIFromAnything. This module provides functionality to coalesce multiple identical requests into a single request, reducing the load on backend services and improving performance. .. py:currentmodule:: apifrom.performance.request_coalescing Overview -------- **Classes** * :py:class:`CoalescedRequest` * :py:class:`RequestCoalescer` * :py:class:`RequestCoalescingManager` * :py:class:`RequestCoalescingMiddleware` **Functions** * :py:func:`coalesce_requests` Classes ------- .. py:class:: CoalescedRequest(key, func, args, kwargs) Represents a coalesced request. This class is used to track multiple identical requests that have been coalesced into a single request. Initialize a coalesced request. :param key: Cache key for the request. :type key: str :param func: Function to execute. :type func: Callable :param args: Positional arguments for the function. :type args: tuple :param kwargs: Keyword arguments for the function. :type kwargs: dict .. :: args .. :: count .. :: error :annotation: Optional[Exception] .. :: func .. :: is_error .. :: is_executed .. :: key .. :: kwargs .. :: result .. :: timestamp .. method:: execute() Execute the request. :returns: Result of executing the function. :rtype: Any .. py:class:: RequestCoalescer(func, window_time = 0.1, max_requests = None) Coalescer for handling duplicate requests. This class provides functionality to coalesce multiple identical requests into a single request, reducing the load on backend services and improving performance. Initialize the request coalescer. :param func: Function to execute. :type func: Callable :param window_time: Time window in seconds to coalesce requests. :type window_time: float :param max_requests: Maximum number of requests to coalesce. :type max_requests: int, optional .. :: func .. :: lock .. :: lock_acquired .. :: max_requests .. :: pending_requests :annotation: Dict[str, Dict[str, Any]] .. :: request_counts :annotation: Dict[str, int] .. :: results_cache :annotation: Dict[str, Any] .. :: stats .. :: window_time .. method:: _create_key(args, kwargs) Create a cache key from the arguments. :param args: Positional arguments. :param kwargs: Keyword arguments. :returns: Cache key. :rtype: str .. method:: get_stats() Get statistics about the coalescer. :returns: Statistics about the coalescer. :rtype: dict .. py:class:: RequestCoalescingManager Manager for coalescing requests across the application. This class provides a centralized way to manage request coalescing across the application. Initialize the request coalescing manager. .. :: coalescers :annotation: Dict[str, RequestCoalescer] .. :: lock .. :: stats .. method:: execute(func, *args, **kwargs) Execute a request, potentially coalescing it with other identical requests. :param func: Function to execute. :type func: Callable :param \*args: Positional arguments for the function. :param \*\*kwargs: Keyword arguments for the function. :returns: Result of executing the function. :rtype: Any .. method:: get_coalescer(func, window_time = 0.1, max_requests = None) Get or create a coalescer for a function. :param func: Function to coalesce. :type func: Callable :param window_time: Time window in seconds to coalesce requests. :type window_time: float :param max_requests: Maximum number of requests to coalesce. :type max_requests: int, optional :returns: Coalescer for the function. :rtype: RequestCoalescer .. method:: get_stats() Get statistics about the manager. :returns: Statistics about the manager. :rtype: dict .. py:class:: RequestCoalescingMiddleware(window_time = 0.1, max_requests = None) Middleware for coalescing requests. This middleware can be used to coalesce requests at the middleware level. Initialize the request coalescing middleware. :param window_time: Time window in seconds to coalesce requests. :type window_time: float :param max_requests: Maximum number of requests to coalesce. :type max_requests: int, optional .. :: manager .. :: max_requests .. :: window_time .. method:: get_stats() Get statistics about the middleware. :returns: Statistics about the middleware. :rtype: dict .. method:: process_request(request) Process a request, potentially coalescing it with other identical requests. :param request: Request to process. :type request: Any :returns: Processed request. :rtype: Any .. method:: process_response(request, response) Process a response. :param request: Request that generated the response. :type request: Any :param response: Response to process. :type response: Any :returns: Processed response. :rtype: Any Functions --------- .. py:function:: coalesce_requests(window_time = 0.1, max_requests = None) Decorator for coalescing multiple identical requests into a single request. :param window_time: Time window in seconds to coalesce requests. :type window_time: float :param max_requests: Maximum number of requests to coalesce. :type max_requests: int, optional :returns: Decorated function that coalesces requests. :rtype: Callable