Wait for data to be available on the socket. Uses selectors to wait for the socket to become readable within the given timeout. This prevents slow clients from blocking thread pool slots indefinitely. Args: timeout: Maximum time to wait in seconds.
(self, timeout)
| 90 | self.timeout = time.monotonic() + self.cfg.keepalive |
| 91 | |
| 92 | def wait_for_data(self, timeout): |
| 93 | """Wait for data to be available on the socket. |
| 94 | |
| 95 | Uses selectors to wait for the socket to become readable within |
| 96 | the given timeout. This prevents slow clients from blocking |
| 97 | thread pool slots indefinitely. |
| 98 | |
| 99 | Args: |
| 100 | timeout: Maximum time to wait in seconds. |
| 101 | |
| 102 | Returns: |
| 103 | True if data is available, False if timeout expired. |
| 104 | """ |
| 105 | if self.data_ready: |
| 106 | return True |
| 107 | |
| 108 | # Use a temporary selector to wait for data |
| 109 | sel = selectors.DefaultSelector() |
| 110 | try: |
| 111 | sel.register(self.sock, selectors.EVENT_READ) |
| 112 | events = sel.select(timeout=timeout) |
| 113 | if events: |
| 114 | self.data_ready = True |
| 115 | return True |
| 116 | return False |
| 117 | except (OSError, ValueError): |
| 118 | # Socket closed or invalid |
| 119 | return False |
| 120 | finally: |
| 121 | sel.close() |
| 122 | |
| 123 | def close(self, graceful=False): |
| 124 | if graceful: |