|
6 | 6 | import ssl |
7 | 7 | import struct |
8 | 8 | import sys |
| 9 | +import weakref |
9 | 10 |
|
10 | 11 | if sys.platform == "win32": |
11 | 12 | import _overlapped # type: ignore[import-not-found] |
@@ -57,15 +58,26 @@ class BaseEventsServer(asyncio.base_events.Server): |
57 | 58 | else: |
58 | 59 | _loop: EventsAbstractEventLoop |
59 | 60 | _sockets: Iterable[socket.socket] |
60 | | - _active_count: int |
| 61 | + if sys.version_info >= (3, 13): |
| 62 | + # https://github.com/python/cpython/blob/v3.13.7/Lib/asyncio/base_events.py#L283 |
| 63 | + _clients: weakref.WeakSet[object] |
| 64 | + else: |
| 65 | + _active_count: int |
61 | 66 | _protocol_factory: _ProtocolFactory |
62 | 67 | _backlog: int |
63 | 68 | _ssl_context: _SSLContext |
64 | 69 | _ssl_handshake_timeout: Optional[float] |
65 | 70 |
|
66 | | - def _attach(self) -> None: ... |
| 71 | + if sys.version_info >= (3, 13): |
| 72 | + # https://github.com/python/cpython/blob/bcee1c322115c581da27600f2ae55e5439c027eb/Lib/asyncio/base_events.py#L296 |
| 73 | + def _attach(self, transport: object) -> None: ... |
| 74 | + |
| 75 | + def _detach(self, transport: object) -> None: ... |
| 76 | + else: |
| 77 | + |
| 78 | + def _attach(self) -> None: ... |
67 | 79 |
|
68 | | - def _detach(self) -> None: ... |
| 80 | + def _detach(self) -> None: ... |
69 | 81 |
|
70 | 82 | def _start_serving(self) -> None: ... |
71 | 83 |
|
@@ -132,20 +144,22 @@ def __init__( |
132 | 144 | max_concurrent_connections if max_concurrent_connections is not None else global_max_concurrent_connections |
133 | 145 | ) |
134 | 146 |
|
135 | | - def _attach(self) -> None: |
136 | | - super()._attach() |
137 | | - logging.getLogger(__name__).debug(f"New connection. Total connections: {self._active_count}") |
138 | | - if not self._paused and self._active_count >= self.max_concurrent_connections: |
| 147 | + def _attach(self, *args: object, **kwargs: object) -> None: |
| 148 | + super()._attach(*args, **kwargs) |
| 149 | + active_connections = self._chia_active_connections() |
| 150 | + logging.getLogger(__name__).debug(f"New connection. Total connections: {active_connections}") |
| 151 | + if not self._paused and active_connections >= self.max_concurrent_connections: |
139 | 152 | self._chia_pause() |
140 | 153 |
|
141 | | - def _detach(self) -> None: |
142 | | - super()._detach() |
143 | | - logging.getLogger(__name__).debug(f"Connection lost. Total connections: {self._active_count}") |
| 154 | + def _detach(self, *args: object, **kwargs: object) -> None: |
| 155 | + super()._detach(*args, **kwargs) |
| 156 | + active_connections = self._chia_active_connections() |
| 157 | + logging.getLogger(__name__).debug(f"Connection lost. Total connections: {active_connections}") |
144 | 158 | if ( |
145 | | - self._active_count > 0 |
| 159 | + active_connections > 0 |
146 | 160 | and self._sockets is not None |
147 | 161 | and self._paused |
148 | | - and self._active_count < self.max_concurrent_connections |
| 162 | + and active_connections < self.max_concurrent_connections |
149 | 163 | ): |
150 | 164 | self._chia_resume() |
151 | 165 |
|
@@ -180,6 +194,12 @@ def _chia_resume(self) -> None: |
180 | 194 | ) |
181 | 195 | logging.getLogger(__name__).debug("Resumed accepting connections.") |
182 | 196 |
|
| 197 | + def _chia_active_connections(self) -> int: |
| 198 | + if sys.version_info >= (3, 13): |
| 199 | + return len(self._clients) |
| 200 | + else: |
| 201 | + return self._active_count |
| 202 | + |
183 | 203 |
|
184 | 204 | async def _chia_create_server( |
185 | 205 | cls: Any, |
|
0 commit comments