freqtrade_origin/freqtrade/rpc/api_server/ws/channel.py

241 lines
7.3 KiB
Python
Raw Normal View History

import asyncio
import logging
2022-11-20 21:09:45 +00:00
import time
from collections import deque
from collections.abc import AsyncIterator
2022-11-15 03:27:45 +00:00
from contextlib import asynccontextmanager
from typing import Any
from uuid import uuid4
2022-11-21 19:21:40 +00:00
from fastapi import WebSocketDisconnect
from websockets.exceptions import ConnectionClosed
from freqtrade.rpc.api_server.ws.proxy import WebSocketProxy
from freqtrade.rpc.api_server.ws.serializer import (
HybridJSONWebSocketSerializer,
WebSocketSerializer,
)
from freqtrade.rpc.api_server.ws.types import WebSocketType
from freqtrade.rpc.api_server.ws_schemas import WSMessageSchemaType
logger = logging.getLogger(__name__)
class WebSocketChannel:
"""
Object to help facilitate managing a websocket connection
"""
2024-05-12 14:51:11 +00:00
def __init__(
self,
websocket: WebSocketType,
channel_id: str | None = None,
serializer_cls: type[WebSocketSerializer] = HybridJSONWebSocketSerializer,
2024-05-12 14:51:11 +00:00
send_throttle: float = 0.01,
):
self.channel_id = channel_id if channel_id else uuid4().hex[:8]
self._websocket = WebSocketProxy(websocket)
# Internal event to signify a closed websocket
2022-11-02 19:26:27 +00:00
self._closed = asyncio.Event()
2022-11-20 21:09:45 +00:00
# The async tasks created for the channel
self._channel_tasks: list[asyncio.Task] = []
2022-11-20 21:09:45 +00:00
# Deque for average send times
self._send_times: deque[float] = deque([], maxlen=10)
2022-11-20 21:09:45 +00:00
# High limit defaults to 3 to start
self._send_high_limit = 3
2022-11-27 20:14:49 +00:00
self._send_throttle = send_throttle
2022-11-15 05:21:40 +00:00
# The subscribed message types
self._subscriptions: list[str] = []
2022-11-15 03:27:45 +00:00
# Wrap the WebSocket in the Serializing class
2022-11-13 09:26:27 +00:00
self._wrapped_ws = serializer_cls(self._websocket)
def __repr__(self):
return f"WebSocketChannel({self.channel_id}, {self.remote_addr})"
@property
def raw_websocket(self):
return self._websocket.raw_websocket
@property
def remote_addr(self):
return self._websocket.remote_addr
@property
def avg_send_time(self):
return sum(self._send_times) / len(self._send_times)
2022-11-20 21:09:45 +00:00
def _calc_send_limit(self):
"""
Calculate the send high limit for this channel
"""
# Only update if we have enough data
if len(self._send_times) == self._send_times.maxlen:
# At least 1s or twice the average of send times, with a
# maximum of 3 seconds per message
self._send_high_limit = min(max(self.avg_send_time * 2, 1), 3)
2022-11-20 21:09:45 +00:00
async def send(self, message: WSMessageSchemaType | dict[str, Any], use_timeout: bool = False):
"""
2022-11-20 21:09:45 +00:00
Send a message on the wrapped websocket. If the sending
takes too long, it will raise a TimeoutError and
disconnect the connection.
:param message: The message to send
:param use_timeout: Enforce send high limit, defaults to False
"""
2022-11-20 21:09:45 +00:00
try:
_ = time.time()
# If the send times out, it will raise
# a TimeoutError and bubble up to the
# message_endpoint to close the connection
await asyncio.wait_for(
self._wrapped_ws.send(message),
timeout=self._send_high_limit if use_timeout else None,
2022-11-20 21:09:45 +00:00
)
total_time = time.time() - _
self._send_times.append(total_time)
self._calc_send_limit()
except asyncio.TimeoutError:
logger.info(f"Connection for {self} timed out, disconnecting")
2022-11-20 21:09:45 +00:00
raise
2022-11-25 20:08:41 +00:00
# Explicitly give control back to event loop as
# websockets.send does not
2022-11-27 20:14:49 +00:00
# Also throttles how fast we send
await asyncio.sleep(self._send_throttle)
async def recv(self):
"""
2022-11-15 03:27:45 +00:00
Receive a message on the wrapped websocket
"""
return await self._wrapped_ws.recv()
async def ping(self):
"""
Ping the websocket
"""
return await self._websocket.ping()
2022-11-15 03:27:45 +00:00
async def accept(self):
"""
2022-11-24 18:35:50 +00:00
Accept the underlying websocket connection,
if the connection has been closed before we can
accept, just close the channel.
2022-11-15 03:27:45 +00:00
"""
2022-11-24 18:35:50 +00:00
try:
return await self._websocket.accept()
except RuntimeError:
await self.close()
2022-11-15 03:27:45 +00:00
async def close(self):
"""
Close the WebSocketChannel
"""
self._closed.set()
2022-11-02 19:26:27 +00:00
try:
2022-11-15 03:27:45 +00:00
await self._websocket.close()
except RuntimeError:
2022-11-02 19:26:27 +00:00
pass
def is_closed(self) -> bool:
"""
Closed flag
"""
2022-11-02 19:26:27 +00:00
return self._closed.is_set()
def set_subscriptions(self, subscriptions: list[str]) -> None:
"""
Set which subscriptions this channel is subscribed to
:param subscriptions: List of subscriptions, List[str]
"""
self._subscriptions = subscriptions
def subscribed_to(self, message_type: str) -> bool:
"""
Check if this channel is subscribed to the message_type
:param message_type: The message type to check
"""
return message_type in self._subscriptions
async def run_channel_tasks(self, *tasks, **kwargs):
"""
Create and await on the channel tasks unless an exception
was raised, then cancel them all.
:params *tasks: All coros or tasks to be run concurrently
:param **kwargs: Any extra kwargs to pass to gather
"""
2022-11-24 18:35:50 +00:00
if not self.is_closed():
# Wrap the coros into tasks if they aren't already
self._channel_tasks = [
task if isinstance(task, asyncio.Task) else asyncio.create_task(task)
for task in tasks
]
2022-11-24 18:35:50 +00:00
try:
return await asyncio.gather(*self._channel_tasks, **kwargs)
except Exception:
# If an exception occurred, cancel the rest of the tasks
await self.cancel_channel_tasks()
async def cancel_channel_tasks(self):
"""
Cancel and wait on all channel tasks
"""
for task in self._channel_tasks:
task.cancel()
2022-11-24 18:35:50 +00:00
# Wait for tasks to finish cancelling
try:
await task
except (
asyncio.CancelledError,
asyncio.TimeoutError,
WebSocketDisconnect,
ConnectionClosed,
2024-05-12 14:51:11 +00:00
RuntimeError,
2022-11-24 18:35:50 +00:00
):
pass
except Exception as e:
logger.info(f"Encountered unknown exception: {e}", exc_info=e)
self._channel_tasks = []
async def __aiter__(self):
"""
Generator for received messages
"""
# We can not catch any errors here as websocket.recv is
# the first to catch any disconnects and bubble it up
# so the connection is garbage collected right away
while not self.is_closed():
yield await self.recv()
@asynccontextmanager
2024-05-12 14:51:11 +00:00
async def create_channel(websocket: WebSocketType, **kwargs) -> AsyncIterator[WebSocketChannel]:
"""
Context manager for safely opening and closing a WebSocketChannel
"""
channel = WebSocketChannel(websocket, **kwargs)
try:
await channel.accept()
logger.info(f"Connected to channel - {channel}")
yield channel
finally:
await channel.close()
logger.info(f"Disconnected from channel - {channel}")