client_ws.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. """WebSocket client for asyncio."""
  2. import asyncio
  3. import sys
  4. from types import TracebackType
  5. from typing import Any, Optional, Type, cast
  6. import attr
  7. from ._websocket.reader import WebSocketDataQueue
  8. from .client_exceptions import ClientError, ServerTimeoutError, WSMessageTypeError
  9. from .client_reqrep import ClientResponse
  10. from .helpers import calculate_timeout_when, set_result
  11. from .http import (
  12. WS_CLOSED_MESSAGE,
  13. WS_CLOSING_MESSAGE,
  14. WebSocketError,
  15. WSCloseCode,
  16. WSMessage,
  17. WSMsgType,
  18. )
  19. from .http_websocket import _INTERNAL_RECEIVE_TYPES, WebSocketWriter
  20. from .streams import EofStream
  21. from .typedefs import (
  22. DEFAULT_JSON_DECODER,
  23. DEFAULT_JSON_ENCODER,
  24. JSONDecoder,
  25. JSONEncoder,
  26. )
  27. if sys.version_info >= (3, 11):
  28. import asyncio as async_timeout
  29. else:
  30. import async_timeout
  31. @attr.s(frozen=True, slots=True)
  32. class ClientWSTimeout:
  33. ws_receive = attr.ib(type=Optional[float], default=None)
  34. ws_close = attr.ib(type=Optional[float], default=None)
  35. DEFAULT_WS_CLIENT_TIMEOUT = ClientWSTimeout(ws_receive=None, ws_close=10.0)
  36. class ClientWebSocketResponse:
  37. def __init__(
  38. self,
  39. reader: WebSocketDataQueue,
  40. writer: WebSocketWriter,
  41. protocol: Optional[str],
  42. response: ClientResponse,
  43. timeout: ClientWSTimeout,
  44. autoclose: bool,
  45. autoping: bool,
  46. loop: asyncio.AbstractEventLoop,
  47. *,
  48. heartbeat: Optional[float] = None,
  49. compress: int = 0,
  50. client_notakeover: bool = False,
  51. ) -> None:
  52. self._response = response
  53. self._conn = response.connection
  54. self._writer = writer
  55. self._reader = reader
  56. self._protocol = protocol
  57. self._closed = False
  58. self._closing = False
  59. self._close_code: Optional[int] = None
  60. self._timeout = timeout
  61. self._autoclose = autoclose
  62. self._autoping = autoping
  63. self._heartbeat = heartbeat
  64. self._heartbeat_cb: Optional[asyncio.TimerHandle] = None
  65. self._heartbeat_when: float = 0.0
  66. if heartbeat is not None:
  67. self._pong_heartbeat = heartbeat / 2.0
  68. self._pong_response_cb: Optional[asyncio.TimerHandle] = None
  69. self._loop = loop
  70. self._waiting: bool = False
  71. self._close_wait: Optional[asyncio.Future[None]] = None
  72. self._exception: Optional[BaseException] = None
  73. self._compress = compress
  74. self._client_notakeover = client_notakeover
  75. self._ping_task: Optional[asyncio.Task[None]] = None
  76. self._reset_heartbeat()
  77. def _cancel_heartbeat(self) -> None:
  78. self._cancel_pong_response_cb()
  79. if self._heartbeat_cb is not None:
  80. self._heartbeat_cb.cancel()
  81. self._heartbeat_cb = None
  82. if self._ping_task is not None:
  83. self._ping_task.cancel()
  84. self._ping_task = None
  85. def _cancel_pong_response_cb(self) -> None:
  86. if self._pong_response_cb is not None:
  87. self._pong_response_cb.cancel()
  88. self._pong_response_cb = None
  89. def _reset_heartbeat(self) -> None:
  90. if self._heartbeat is None:
  91. return
  92. self._cancel_pong_response_cb()
  93. loop = self._loop
  94. assert loop is not None
  95. conn = self._conn
  96. timeout_ceil_threshold = (
  97. conn._connector._timeout_ceil_threshold if conn is not None else 5
  98. )
  99. now = loop.time()
  100. when = calculate_timeout_when(now, self._heartbeat, timeout_ceil_threshold)
  101. self._heartbeat_when = when
  102. if self._heartbeat_cb is None:
  103. # We do not cancel the previous heartbeat_cb here because
  104. # it generates a significant amount of TimerHandle churn
  105. # which causes asyncio to rebuild the heap frequently.
  106. # Instead _send_heartbeat() will reschedule the next
  107. # heartbeat if it fires too early.
  108. self._heartbeat_cb = loop.call_at(when, self._send_heartbeat)
  109. def _send_heartbeat(self) -> None:
  110. self._heartbeat_cb = None
  111. loop = self._loop
  112. now = loop.time()
  113. if now < self._heartbeat_when:
  114. # Heartbeat fired too early, reschedule
  115. self._heartbeat_cb = loop.call_at(
  116. self._heartbeat_when, self._send_heartbeat
  117. )
  118. return
  119. conn = self._conn
  120. timeout_ceil_threshold = (
  121. conn._connector._timeout_ceil_threshold if conn is not None else 5
  122. )
  123. when = calculate_timeout_when(now, self._pong_heartbeat, timeout_ceil_threshold)
  124. self._cancel_pong_response_cb()
  125. self._pong_response_cb = loop.call_at(when, self._pong_not_received)
  126. coro = self._writer.send_frame(b"", WSMsgType.PING)
  127. if sys.version_info >= (3, 12):
  128. # Optimization for Python 3.12, try to send the ping
  129. # immediately to avoid having to schedule
  130. # the task on the event loop.
  131. ping_task = asyncio.Task(coro, loop=loop, eager_start=True)
  132. else:
  133. ping_task = loop.create_task(coro)
  134. if not ping_task.done():
  135. self._ping_task = ping_task
  136. ping_task.add_done_callback(self._ping_task_done)
  137. else:
  138. self._ping_task_done(ping_task)
  139. def _ping_task_done(self, task: "asyncio.Task[None]") -> None:
  140. """Callback for when the ping task completes."""
  141. if not task.cancelled() and (exc := task.exception()):
  142. self._handle_ping_pong_exception(exc)
  143. self._ping_task = None
  144. def _pong_not_received(self) -> None:
  145. self._handle_ping_pong_exception(
  146. ServerTimeoutError(f"No PONG received after {self._pong_heartbeat} seconds")
  147. )
  148. def _handle_ping_pong_exception(self, exc: BaseException) -> None:
  149. """Handle exceptions raised during ping/pong processing."""
  150. if self._closed:
  151. return
  152. self._set_closed()
  153. self._close_code = WSCloseCode.ABNORMAL_CLOSURE
  154. self._exception = exc
  155. self._response.close()
  156. if self._waiting and not self._closing:
  157. self._reader.feed_data(WSMessage(WSMsgType.ERROR, exc, None), 0)
  158. def _set_closed(self) -> None:
  159. """Set the connection to closed.
  160. Cancel any heartbeat timers and set the closed flag.
  161. """
  162. self._closed = True
  163. self._cancel_heartbeat()
  164. def _set_closing(self) -> None:
  165. """Set the connection to closing.
  166. Cancel any heartbeat timers and set the closing flag.
  167. """
  168. self._closing = True
  169. self._cancel_heartbeat()
  170. @property
  171. def closed(self) -> bool:
  172. return self._closed
  173. @property
  174. def close_code(self) -> Optional[int]:
  175. return self._close_code
  176. @property
  177. def protocol(self) -> Optional[str]:
  178. return self._protocol
  179. @property
  180. def compress(self) -> int:
  181. return self._compress
  182. @property
  183. def client_notakeover(self) -> bool:
  184. return self._client_notakeover
  185. def get_extra_info(self, name: str, default: Any = None) -> Any:
  186. """extra info from connection transport"""
  187. conn = self._response.connection
  188. if conn is None:
  189. return default
  190. transport = conn.transport
  191. if transport is None:
  192. return default
  193. return transport.get_extra_info(name, default)
  194. def exception(self) -> Optional[BaseException]:
  195. return self._exception
  196. async def ping(self, message: bytes = b"") -> None:
  197. await self._writer.send_frame(message, WSMsgType.PING)
  198. async def pong(self, message: bytes = b"") -> None:
  199. await self._writer.send_frame(message, WSMsgType.PONG)
  200. async def send_frame(
  201. self, message: bytes, opcode: WSMsgType, compress: Optional[int] = None
  202. ) -> None:
  203. """Send a frame over the websocket."""
  204. await self._writer.send_frame(message, opcode, compress)
  205. async def send_str(self, data: str, compress: Optional[int] = None) -> None:
  206. if not isinstance(data, str):
  207. raise TypeError("data argument must be str (%r)" % type(data))
  208. await self._writer.send_frame(
  209. data.encode("utf-8"), WSMsgType.TEXT, compress=compress
  210. )
  211. async def send_bytes(self, data: bytes, compress: Optional[int] = None) -> None:
  212. if not isinstance(data, (bytes, bytearray, memoryview)):
  213. raise TypeError("data argument must be byte-ish (%r)" % type(data))
  214. await self._writer.send_frame(data, WSMsgType.BINARY, compress=compress)
  215. async def send_json(
  216. self,
  217. data: Any,
  218. compress: Optional[int] = None,
  219. *,
  220. dumps: JSONEncoder = DEFAULT_JSON_ENCODER,
  221. ) -> None:
  222. await self.send_str(dumps(data), compress=compress)
  223. async def close(self, *, code: int = WSCloseCode.OK, message: bytes = b"") -> bool:
  224. # we need to break `receive()` cycle first,
  225. # `close()` may be called from different task
  226. if self._waiting and not self._closing:
  227. assert self._loop is not None
  228. self._close_wait = self._loop.create_future()
  229. self._set_closing()
  230. self._reader.feed_data(WS_CLOSING_MESSAGE, 0)
  231. await self._close_wait
  232. if self._closed:
  233. return False
  234. self._set_closed()
  235. try:
  236. await self._writer.close(code, message)
  237. except asyncio.CancelledError:
  238. self._close_code = WSCloseCode.ABNORMAL_CLOSURE
  239. self._response.close()
  240. raise
  241. except Exception as exc:
  242. self._close_code = WSCloseCode.ABNORMAL_CLOSURE
  243. self._exception = exc
  244. self._response.close()
  245. return True
  246. if self._close_code:
  247. self._response.close()
  248. return True
  249. while True:
  250. try:
  251. async with async_timeout.timeout(self._timeout.ws_close):
  252. msg = await self._reader.read()
  253. except asyncio.CancelledError:
  254. self._close_code = WSCloseCode.ABNORMAL_CLOSURE
  255. self._response.close()
  256. raise
  257. except Exception as exc:
  258. self._close_code = WSCloseCode.ABNORMAL_CLOSURE
  259. self._exception = exc
  260. self._response.close()
  261. return True
  262. if msg.type is WSMsgType.CLOSE:
  263. self._close_code = msg.data
  264. self._response.close()
  265. return True
  266. async def receive(self, timeout: Optional[float] = None) -> WSMessage:
  267. receive_timeout = timeout or self._timeout.ws_receive
  268. while True:
  269. if self._waiting:
  270. raise RuntimeError("Concurrent call to receive() is not allowed")
  271. if self._closed:
  272. return WS_CLOSED_MESSAGE
  273. elif self._closing:
  274. await self.close()
  275. return WS_CLOSED_MESSAGE
  276. try:
  277. self._waiting = True
  278. try:
  279. if receive_timeout:
  280. # Entering the context manager and creating
  281. # Timeout() object can take almost 50% of the
  282. # run time in this loop so we avoid it if
  283. # there is no read timeout.
  284. async with async_timeout.timeout(receive_timeout):
  285. msg = await self._reader.read()
  286. else:
  287. msg = await self._reader.read()
  288. self._reset_heartbeat()
  289. finally:
  290. self._waiting = False
  291. if self._close_wait:
  292. set_result(self._close_wait, None)
  293. except (asyncio.CancelledError, asyncio.TimeoutError):
  294. self._close_code = WSCloseCode.ABNORMAL_CLOSURE
  295. raise
  296. except EofStream:
  297. self._close_code = WSCloseCode.OK
  298. await self.close()
  299. return WSMessage(WSMsgType.CLOSED, None, None)
  300. except ClientError:
  301. # Likely ServerDisconnectedError when connection is lost
  302. self._set_closed()
  303. self._close_code = WSCloseCode.ABNORMAL_CLOSURE
  304. return WS_CLOSED_MESSAGE
  305. except WebSocketError as exc:
  306. self._close_code = exc.code
  307. await self.close(code=exc.code)
  308. return WSMessage(WSMsgType.ERROR, exc, None)
  309. except Exception as exc:
  310. self._exception = exc
  311. self._set_closing()
  312. self._close_code = WSCloseCode.ABNORMAL_CLOSURE
  313. await self.close()
  314. return WSMessage(WSMsgType.ERROR, exc, None)
  315. if msg.type not in _INTERNAL_RECEIVE_TYPES:
  316. # If its not a close/closing/ping/pong message
  317. # we can return it immediately
  318. return msg
  319. if msg.type is WSMsgType.CLOSE:
  320. self._set_closing()
  321. self._close_code = msg.data
  322. if not self._closed and self._autoclose:
  323. await self.close()
  324. elif msg.type is WSMsgType.CLOSING:
  325. self._set_closing()
  326. elif msg.type is WSMsgType.PING and self._autoping:
  327. await self.pong(msg.data)
  328. continue
  329. elif msg.type is WSMsgType.PONG and self._autoping:
  330. continue
  331. return msg
  332. async def receive_str(self, *, timeout: Optional[float] = None) -> str:
  333. msg = await self.receive(timeout)
  334. if msg.type is not WSMsgType.TEXT:
  335. raise WSMessageTypeError(
  336. f"Received message {msg.type}:{msg.data!r} is not WSMsgType.TEXT"
  337. )
  338. return cast(str, msg.data)
  339. async def receive_bytes(self, *, timeout: Optional[float] = None) -> bytes:
  340. msg = await self.receive(timeout)
  341. if msg.type is not WSMsgType.BINARY:
  342. raise WSMessageTypeError(
  343. f"Received message {msg.type}:{msg.data!r} is not WSMsgType.BINARY"
  344. )
  345. return cast(bytes, msg.data)
  346. async def receive_json(
  347. self,
  348. *,
  349. loads: JSONDecoder = DEFAULT_JSON_DECODER,
  350. timeout: Optional[float] = None,
  351. ) -> Any:
  352. data = await self.receive_str(timeout=timeout)
  353. return loads(data)
  354. def __aiter__(self) -> "ClientWebSocketResponse":
  355. return self
  356. async def __anext__(self) -> WSMessage:
  357. msg = await self.receive()
  358. if msg.type in (WSMsgType.CLOSE, WSMsgType.CLOSING, WSMsgType.CLOSED):
  359. raise StopAsyncIteration
  360. return msg
  361. async def __aenter__(self) -> "ClientWebSocketResponse":
  362. return self
  363. async def __aexit__(
  364. self,
  365. exc_type: Optional[Type[BaseException]],
  366. exc_val: Optional[BaseException],
  367. exc_tb: Optional[TracebackType],
  368. ) -> None:
  369. await self.close()