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.

Overview

Classes

Functions

Classes

class apifrom.performance.request_coalescing.CoalescedRequest(key, func, args, kwargs)[source]
Parameters:

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

apifrom.performance.request_coalescing.execute()

Execute the request.

returns:

Result of executing the function.

rtype:

Any

class apifrom.performance.request_coalescing.RequestCoalescer(func, window_time=0.1, max_requests=None)[source]
Parameters:
  • func (Callable[Ellipsis, R])

  • window_time (float)

  • max_requests (Optional[int])

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

apifrom.performance.request_coalescing._create_key(args, kwargs)

Create a cache key from the arguments.

param args:

Positional arguments.

param kwargs:

Keyword arguments.

returns:

Cache key.

rtype:

str

apifrom.performance.request_coalescing.get_stats()

Get statistics about the coalescer.

returns:

Statistics about the coalescer.

rtype:

dict

class apifrom.performance.request_coalescing.RequestCoalescingManager[source]

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.

apifrom.performance.request_coalescing.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

apifrom.performance.request_coalescing.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

apifrom.performance.request_coalescing.get_stats()

Get statistics about the manager.

returns:

Statistics about the manager.

rtype:

dict

class apifrom.performance.request_coalescing.RequestCoalescingMiddleware(window_time=0.1, max_requests=None)[source]
Parameters:
  • window_time (float)

  • max_requests (Optional[int])

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

apifrom.performance.request_coalescing.get_stats()

Get statistics about the middleware.

returns:

Statistics about the middleware.

rtype:

dict

apifrom.performance.request_coalescing.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

apifrom.performance.request_coalescing.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

apifrom.performance.request_coalescing.coalesce_requests(window_time=0.1, max_requests=None)[source]
Parameters:
  • window_time (float)

  • max_requests (Optional[int])

Return type:

Callable[[Callable[Ellipsis, R]], Callable[Ellipsis, R]]

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