Handle completion of a request (called via method_queue on main thread).
(self, conn, fs)
| 416 | s.close() |
| 417 | |
| 418 | def finish_request(self, conn, fs): |
| 419 | """Handle completion of a request (called via method_queue on main thread).""" |
| 420 | try: |
| 421 | result = fs.result() if not fs.cancelled() else False |
| 422 | |
| 423 | if result is _DEFER and self.alive: |
| 424 | # Connection deferred - no data arrived within timeout. |
| 425 | # Put it on the poller to wait for data without consuming a thread. |
| 426 | conn.sock.setblocking(False) |
| 427 | # Use keepalive timeout for pending connections too |
| 428 | conn.timeout = time.monotonic() + self.cfg.keepalive |
| 429 | self.pending_conns.append(conn) |
| 430 | self.poller.register(conn.sock, selectors.EVENT_READ, |
| 431 | partial(self.on_pending_socket_readable, conn)) |
| 432 | elif result and self.alive: |
| 433 | # Keepalive - put connection back in the poller |
| 434 | conn.sock.setblocking(False) |
| 435 | conn.set_timeout() |
| 436 | self.keepalived_conns.append(conn) |
| 437 | self.poller.register(conn.sock, selectors.EVENT_READ, |
| 438 | partial(self.on_client_socket_readable, conn)) |
| 439 | else: |
| 440 | self.nr_conns -= 1 |
| 441 | conn.close(graceful=True) |
| 442 | except Exception: |
| 443 | self.nr_conns -= 1 |
| 444 | conn.close() |
| 445 | |
| 446 | def handle(self, conn): |
| 447 | """Handle a request on a connection. Runs in a worker thread.""" |