nat.middleware.timeout.timeout_middleware#

Timeout middleware that enforces time limits on intercepted function calls.

Classes#

TimeoutMiddleware

Middleware that enforces configurable time limits on intercepted calls.

Module Contents#

class TimeoutMiddleware(
config: nat.middleware.timeout.timeout_middleware_config.TimeoutMiddlewareConfig,
builder: nat.builder.builder.Builder,
)#

Bases: nat.middleware.dynamic.dynamic_function_middleware.DynamicFunctionMiddleware

Middleware that enforces configurable time limits on intercepted calls.

Raises TimeoutError when execution exceeds the configured duration. When used in a middleware chain, the timeout covers everything downstream from its position — place it last to time only the target function.

_timeout_config: nat.middleware.timeout.timeout_middleware_config.TimeoutMiddlewareConfig#
async function_middleware_invoke(
*args: Any,
call_next: nat.middleware.middleware.CallNext,
context: nat.middleware.middleware.FunctionMiddlewareContext,
\*\*kwargs: Any,
) Any#

Wrap the downstream call with an asyncio timeout.

Args:

args: Positional arguments for the function. call_next: Callable to invoke next middleware or target function. context: Static function metadata. kwargs: Keyword arguments for the function.

Returns:

The function output if it completes within the timeout.

Raises:

TimeoutError: If the downstream call exceeds the configured timeout.

async function_middleware_stream(
*args: Any,
call_next: nat.middleware.middleware.CallNextStream,
context: nat.middleware.middleware.FunctionMiddlewareContext,
\*\*kwargs: Any,
) collections.abc.AsyncIterator[Any]#

Wrap the downstream stream with an asyncio timeout.

The timeout covers the total stream duration (time from the first chunk request to the final chunk), not individual inter-chunk gaps.

Args:

args: Positional arguments for the function. call_next: Callable to invoke next middleware or target stream. context: Static function metadata. kwargs: Keyword arguments for the function.

Yields:

Stream chunks from the downstream call.

Raises:

TimeoutError: If the full stream exceeds the configured timeout.