Skip to content

feat(asyncfast): Add middleware#101

Merged
jackburridge merged 6 commits into
mainfrom
feat/add-asyncfast-middleware
Feb 20, 2026
Merged

feat(asyncfast): Add middleware#101
jackburridge merged 6 commits into
mainfrom
feat/add-asyncfast-middleware

Conversation

@jackburridge
Copy link
Copy Markdown
Contributor

@jackburridge jackburridge commented Feb 20, 2026

Add middleware to AsyncFast

from time import monotonic

from amgi_types import AMGIApplication
from amgi_types import AMGIReceiveCallable
from amgi_types import AMGISendCallable
from amgi_types import Scope
from asyncfast import AsyncFast


class TimingMiddleware:
    def __init__(self, app: AMGIApplication) -> None:
        self._app = app

    async def __call__(
        self, scope: Scope, receive: AMGIReceiveCallable, send: AMGISendCallable
    ) -> None:
        start = monotonic()
        await self._app(scope, receive, send)
        duration_ms = (monotonic() - start) * 1000
        print(f"{scope['type']} handled in {duration_ms:.2f}ms")


app = AsyncFast()
app.add_middleware(TimingMiddleware)


@app.channel("orders")
async def handle_order(order_id: int) -> None:
    print(f"processing order {order_id}")

This can be used for logging, timing, tracing, metrics, or translating errors.

@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented Feb 20, 2026

Documentation build overview

📚 AsyncFast | 🛠️ Build #31494988 | 📁 Comparing 53edd71 against latest (942057c)


🔍 Preview build

Show files changed (4 files in total): 📝 3 modified | ➕ 1 added | ➖ 0 deleted
File Status
dependencies.html 📝 modified
message_headers.html 📝 modified
middleware.html ➕ added
sending.html 📝 modified

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 20, 2026

Codecov Report

❌ Patch coverage is 99.65517% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
packages/asyncfast/src/asyncfast/_channel.py 98.14% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@jackburridge jackburridge force-pushed the feat/add-asyncfast-middleware branch from de54699 to 86e6cbc Compare February 20, 2026 21:03
@jackburridge jackburridge force-pushed the feat/add-asyncfast-middleware branch from 86e6cbc to 3bed4bb Compare February 20, 2026 21:28
@jackburridge jackburridge force-pushed the feat/add-asyncfast-middleware branch from f3ff1a9 to 53edd71 Compare February 20, 2026 22:24
@jackburridge jackburridge merged commit 983fe25 into main Feb 20, 2026
6 checks passed
@jackburridge jackburridge deleted the feat/add-asyncfast-middleware branch February 20, 2026 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant