Handle an HTTP/2 connection.
(self, transport, ssl_object)
| 1464 | self._closed = True |
| 1465 | |
| 1466 | async def _handle_http2_connection(self, transport, ssl_object): |
| 1467 | """Handle an HTTP/2 connection.""" |
| 1468 | try: |
| 1469 | from gunicorn.http2.async_connection import AsyncHTTP2Connection |
| 1470 | |
| 1471 | peername = transport.get_extra_info('peername') |
| 1472 | sockname = transport.get_extra_info('sockname') |
| 1473 | |
| 1474 | # Use the reader created in connection_made |
| 1475 | # (data_received feeds data to self.reader) |
| 1476 | reader = self.reader |
| 1477 | protocol = asyncio.StreamReaderProtocol(reader) |
| 1478 | writer = asyncio.StreamWriter( |
| 1479 | transport, protocol, reader, self.worker.loop |
| 1480 | ) |
| 1481 | |
| 1482 | # Create HTTP/2 connection handler |
| 1483 | h2_conn = AsyncHTTP2Connection( |
| 1484 | self.cfg, reader, writer, peername |
| 1485 | ) |
| 1486 | await h2_conn.initiate_connection() |
| 1487 | |
| 1488 | self._h2_conn = h2_conn |
| 1489 | |
| 1490 | # Main loop - receive and handle requests |
| 1491 | while not h2_conn.is_closed and self.worker.alive: |
| 1492 | try: |
| 1493 | requests = await h2_conn.receive_data(timeout=1.0) |
| 1494 | except asyncio.TimeoutError: |
| 1495 | continue |
| 1496 | except Exception as e: |
| 1497 | self.log.debug("HTTP/2 receive error: %s", e) |
| 1498 | break |
| 1499 | |
| 1500 | for req in requests: |
| 1501 | try: |
| 1502 | await self._handle_http2_request( |
| 1503 | req, h2_conn, sockname, peername |
| 1504 | ) |
| 1505 | except Exception as e: |
| 1506 | self.log.exception("Error handling HTTP/2 request") |
| 1507 | try: |
| 1508 | await h2_conn.send_error( |
| 1509 | req.stream.stream_id, 500, str(e) |
| 1510 | ) |
| 1511 | except Exception: |
| 1512 | pass |
| 1513 | finally: |
| 1514 | h2_conn.cleanup_stream(req.stream.stream_id) |
| 1515 | |
| 1516 | # Increment worker request count |
| 1517 | self.worker.nr += len(requests) |
| 1518 | |
| 1519 | # Check max_requests |
| 1520 | if self.worker.nr >= self.worker.max_requests: |
| 1521 | self.log.info("Autorestarting worker after current request.") |
| 1522 | self.worker.alive = False |
| 1523 | break |
no test coverage detected