Timeout Decorator

Enforce strict time limits on request processing to prevent hanging connections.

Usage

The @timeout decorator cancels the underlying task if it takes too long.

routes/search.py
from jec_api.decorators import timeout

# Fails if not completed in 500ms
@timeout(seconds=0.5)
async def quick_search():
    ...

Side Effects

  • Raises 504 Gateway Timeout if execution exceeds limit.
  • Cancels the underlying asyncio task.

Signature

def timeout(seconds: float = 30.0, message: str = None)
  • seconds: Maximum execution time before timeout. Default: 30.0.
  • message: Custom error message for timeout responses.