| 1 |
import anyio |
| 2 |
|
| 3 |
from litestar import Litestar |
| 4 |
from litestar.middleware import ASGIMiddleware |
| 5 |
from litestar.types import ASGIApp, Receive, Scope, Send |
| 6 |
|
| 7 |
|
| 8 |
class TimeoutMiddleware(ASGIMiddleware): |
| 9 |
def __init__(self, timeout: float): |
| 10 |
self.timeout = timeout |
| 11 |
|
| 12 |
async def handle(self, scope: Scope, receive: Receive, send: Send, next_app: ASGIApp) -> None: |
| 13 |
with anyio.move_on_after(self.timeout): |
| 14 |
await next_app(scope, receive, send) |
| 15 |
|
| 16 |
|
| 17 |
app = Litestar(middleware=[TimeoutMiddleware(timeout=5)]) |