MCPcopy
hub / github.com/benoitc/gunicorn / _handle_connection

Method _handle_connection

gunicorn/asgi/protocol.py:749–843  ·  view source on GitHub ↗

Main request handling loop using callback-based parser. Uses synchronous parsing in data_received(), avoiding the async overhead of pull-based parsing. The parser fires callbacks when headers and body data are available, and this loop waits on events rather than acti

(self)

Source from the content-addressed store, hash-verified

747 pass
748
749 async def _handle_connection(self):
750 """Main request handling loop using callback-based parser.
751
752 Uses synchronous parsing in data_received(), avoiding the async
753 overhead of pull-based parsing. The parser fires callbacks when
754 headers and body data are available, and this loop waits on
755 events rather than actively parsing.
756 """
757 try:
758 peername = self.transport.get_extra_info('peername')
759 sockname = self.transport.get_extra_info('sockname')
760
761 # Check protocol type - use separate path for uWSGI
762 protocol_type = getattr(self.cfg, 'protocol', 'http')
763 if protocol_type == 'uwsgi':
764 await self._handle_connection_uwsgi(peername, sockname)
765 return
766
767 while not self._closed:
768 self.req_count += 1
769 self._cancel_keepalive_timer()
770
771 # Wait for headers to be parsed (callback sets the event and _current_request)
772 # Don't clear if request already arrived (data_received ran before us)
773 if not self._request_ready.is_set():
774 try:
775 await self._request_ready.wait()
776 except asyncio.CancelledError:
777 break
778
779 if self._closed or self._current_request is None:
780 break
781
782 request = self._current_request
783
784 # If PROXY protocol provided a real client address, use it.
785 effective_peer = self._effective_peername(peername)
786
787 # Check for WebSocket upgrade
788 if self._is_websocket_upgrade(request):
789 await self._handle_websocket(request, sockname, effective_peer)
790 break # WebSocket takes over the connection
791
792 # Handle HTTP request
793 keepalive = await self._handle_http_request(
794 request, sockname, effective_peer
795 )
796
797 # Increment worker request count
798 self.worker.nr += 1
799
800 # Check max_requests
801 if self.worker.nr >= self.worker.max_requests:
802 self.log.info("Autorestarting worker after current request.")
803 self.worker.alive = False
804 keepalive = False
805
806 if not keepalive or not self.worker.alive:

Callers 1

connection_madeMethod · 0.95

Calls 15

_effective_peernameMethod · 0.95
_is_websocket_upgradeMethod · 0.95
_handle_websocketMethod · 0.95
_handle_http_requestMethod · 0.95
_resume_readingMethod · 0.95
_arm_keepalive_timerMethod · 0.95
_close_transportMethod · 0.95
get_extra_infoMethod · 0.45
waitMethod · 0.45
infoMethod · 0.45

Tested by

no test coverage detected