﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
32093	Optional Async Middleware without Context Switching	Andrew Chen Wang	nobody	"Currently, the MiddlewareMixin when using `__acall__` performs `sync_to_async` for both `process_request` and `process_response`. I think we should allow Middleware implementation to set some property such that:

{{{
class MiddlewareMixin:
    # Already defined
    sync_capable = True
    async_capable = True
    # New
    native_async = False

    async def __acall__(self, request):
        """"""
        Async version of __call__ that is swapped in when an async request
        is running.
        """"""
        response = None
        if hasattr(self, 'process_request'):
            if self.native_async:
                response = await self.process_request(request)
            else:
                response = await sync_to_async(
                    self.process_request,
                    thread_sensitive=True,
                )(request)
        response = response or await self.get_response(request)
        if hasattr(self, 'process_response'):
            if self.native_async:
               response = self.process_response(request, response)
            else:
                response = await sync_to_async(
                    self.process_response,
                    thread_sensitive=True,
                )(request, response)
        return response
}}}

in order to avoid context switching which will improve performance."	New feature	closed	Utilities	dev	Normal	wontfix	middleware		Unreviewed	0	0	0	0	0	0
