2022-10-09 21:04:52 +00:00
|
|
|
import asyncio
|
2022-08-29 19:41:15 +00:00
|
|
|
import logging
|
2022-11-02 23:57:11 +00:00
|
|
|
import time
|
2022-08-29 19:41:15 +00:00
|
|
|
from threading import RLock
|
2022-10-25 17:36:40 +00:00
|
|
|
from typing import Any, Dict, List, Optional, Type, Union
|
2022-09-02 21:05:16 +00:00
|
|
|
from uuid import uuid4
|
2022-08-29 19:41:15 +00:00
|
|
|
|
2022-09-06 18:40:58 +00:00
|
|
|
from fastapi import WebSocket as FastAPIWebSocket
|
|
|
|
|
2022-08-29 19:41:15 +00:00
|
|
|
from freqtrade.rpc.api_server.ws.proxy import WebSocketProxy
|
2022-09-02 06:05:36 +00:00
|
|
|
from freqtrade.rpc.api_server.ws.serializer import (HybridJSONWebSocketSerializer,
|
|
|
|
WebSocketSerializer)
|
2022-08-29 19:41:15 +00:00
|
|
|
from freqtrade.rpc.api_server.ws.types import WebSocketType
|
2022-10-25 17:36:40 +00:00
|
|
|
from freqtrade.rpc.api_server.ws_schemas import WSMessageSchemaType
|
2022-08-29 19:41:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class WebSocketChannel:
|
|
|
|
"""
|
|
|
|
Object to help facilitate managing a websocket connection
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
websocket: WebSocketType,
|
2022-09-02 21:05:16 +00:00
|
|
|
channel_id: Optional[str] = None,
|
2022-10-22 15:30:18 +00:00
|
|
|
drain_timeout: int = 3,
|
2022-10-23 01:02:05 +00:00
|
|
|
throttle: float = 0.01,
|
2022-09-02 06:05:36 +00:00
|
|
|
serializer_cls: Type[WebSocketSerializer] = HybridJSONWebSocketSerializer
|
2022-08-29 19:41:15 +00:00
|
|
|
):
|
2022-09-02 21:05:16 +00:00
|
|
|
|
|
|
|
self.channel_id = channel_id if channel_id else uuid4().hex[:8]
|
|
|
|
|
2022-08-29 19:41:15 +00:00
|
|
|
# The WebSocket object
|
|
|
|
self._websocket = WebSocketProxy(websocket)
|
|
|
|
|
2022-10-22 15:30:18 +00:00
|
|
|
self.drain_timeout = drain_timeout
|
2022-10-23 01:02:05 +00:00
|
|
|
self.throttle = throttle
|
2022-10-22 15:30:18 +00:00
|
|
|
|
2022-08-29 21:48:29 +00:00
|
|
|
self._subscriptions: List[str] = []
|
2022-10-23 03:03:57 +00:00
|
|
|
# 32 is the size of the receiving queue in websockets package
|
2022-10-09 21:11:58 +00:00
|
|
|
self.queue: asyncio.Queue[Dict[str, Any]] = asyncio.Queue(maxsize=32)
|
2022-10-09 21:04:52 +00:00
|
|
|
self._relay_task = asyncio.create_task(self.relay())
|
2022-08-29 21:48:29 +00:00
|
|
|
|
2022-08-29 19:41:15 +00:00
|
|
|
# Internal event to signify a closed websocket
|
2022-11-02 19:26:27 +00:00
|
|
|
self._closed = asyncio.Event()
|
2022-08-29 19:41:15 +00:00
|
|
|
|
|
|
|
# Wrap the WebSocket in the Serializing class
|
2022-11-13 09:26:27 +00:00
|
|
|
self._wrapped_ws = serializer_cls(self._websocket)
|
2022-08-29 19:41:15 +00:00
|
|
|
|
2022-09-02 21:05:16 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return f"WebSocketChannel({self.channel_id}, {self.remote_addr})"
|
|
|
|
|
2022-10-23 01:02:05 +00:00
|
|
|
@property
|
2022-10-23 17:42:59 +00:00
|
|
|
def raw_websocket(self):
|
|
|
|
return self._websocket.raw_websocket
|
2022-10-23 01:02:05 +00:00
|
|
|
|
2022-09-02 21:05:16 +00:00
|
|
|
@property
|
|
|
|
def remote_addr(self):
|
|
|
|
return self._websocket.remote_addr
|
|
|
|
|
2022-10-10 00:49:04 +00:00
|
|
|
async def _send(self, data):
|
2022-08-29 19:41:15 +00:00
|
|
|
"""
|
|
|
|
Send data on the wrapped websocket
|
|
|
|
"""
|
|
|
|
await self._wrapped_ws.send(data)
|
|
|
|
|
2022-10-22 15:30:18 +00:00
|
|
|
async def send(self, data) -> bool:
|
2022-10-10 00:49:04 +00:00
|
|
|
"""
|
2022-10-22 15:30:18 +00:00
|
|
|
Add the data to the queue to be sent.
|
|
|
|
:returns: True if data added to queue, False otherwise
|
2022-10-10 00:49:04 +00:00
|
|
|
"""
|
2022-11-02 23:57:11 +00:00
|
|
|
|
|
|
|
# This block only runs if the queue is full, it will wait
|
2022-11-03 05:50:18 +00:00
|
|
|
# until self.drain_timeout for the relay to drain the outgoing queue
|
|
|
|
# We can't use asyncio.wait_for here because the queue may have been created with a
|
|
|
|
# different eventloop
|
2022-11-02 23:57:11 +00:00
|
|
|
start = time.time()
|
|
|
|
while self.queue.full():
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
if (time.time() - start) > self.drain_timeout:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# If for some reason the queue is still full, just return False
|
2022-10-22 15:30:18 +00:00
|
|
|
try:
|
2022-11-02 23:57:11 +00:00
|
|
|
self.queue.put_nowait(data)
|
|
|
|
except asyncio.QueueFull:
|
2022-10-22 15:30:18 +00:00
|
|
|
return False
|
2022-11-02 21:25:39 +00:00
|
|
|
|
2022-11-02 23:57:11 +00:00
|
|
|
# If we got here everything is ok
|
2022-11-02 21:25:39 +00:00
|
|
|
return True
|
2022-10-10 00:49:04 +00:00
|
|
|
|
2022-08-29 19:41:15 +00:00
|
|
|
async def recv(self):
|
|
|
|
"""
|
|
|
|
Receive data on the wrapped websocket
|
|
|
|
"""
|
|
|
|
return await self._wrapped_ws.recv()
|
|
|
|
|
|
|
|
async def ping(self):
|
|
|
|
"""
|
|
|
|
Ping the websocket
|
|
|
|
"""
|
|
|
|
return await self._websocket.ping()
|
|
|
|
|
|
|
|
async def close(self):
|
|
|
|
"""
|
|
|
|
Close the WebSocketChannel
|
|
|
|
"""
|
|
|
|
|
2022-11-02 19:26:27 +00:00
|
|
|
try:
|
|
|
|
await self.raw_websocket.close()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
self._closed.set()
|
2022-10-09 21:04:52 +00:00
|
|
|
self._relay_task.cancel()
|
2022-08-29 19:41:15 +00:00
|
|
|
|
2022-08-29 21:48:29 +00:00
|
|
|
def is_closed(self) -> bool:
|
|
|
|
"""
|
|
|
|
Closed flag
|
|
|
|
"""
|
2022-11-02 19:26:27 +00:00
|
|
|
return self._closed.is_set()
|
2022-08-29 19:41:15 +00:00
|
|
|
|
2022-08-29 21:48:29 +00:00
|
|
|
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
|
|
|
|
|
2022-10-09 21:04:52 +00:00
|
|
|
async def relay(self):
|
|
|
|
"""
|
|
|
|
Relay messages from the channel's queue and send them out. This is started
|
|
|
|
as a task.
|
|
|
|
"""
|
2022-11-02 19:26:27 +00:00
|
|
|
while not self._closed.is_set():
|
2022-10-09 21:04:52 +00:00
|
|
|
message = await self.queue.get()
|
|
|
|
try:
|
2022-10-10 00:49:04 +00:00
|
|
|
await self._send(message)
|
2022-10-09 21:04:52 +00:00
|
|
|
self.queue.task_done()
|
2022-10-11 05:32:10 +00:00
|
|
|
|
|
|
|
# Limit messages per sec.
|
|
|
|
# Could cause problems with queue size if too low, and
|
|
|
|
# problems with network traffik if too high.
|
2022-10-22 15:30:18 +00:00
|
|
|
# 0.01 = 100/s
|
2022-10-23 01:02:05 +00:00
|
|
|
await asyncio.sleep(self.throttle)
|
2022-10-09 21:04:52 +00:00
|
|
|
except RuntimeError:
|
|
|
|
# The connection was closed, just exit the task
|
|
|
|
return
|
|
|
|
|
2022-08-29 19:41:15 +00:00
|
|
|
|
|
|
|
class ChannelManager:
|
|
|
|
def __init__(self):
|
|
|
|
self.channels = dict()
|
|
|
|
self._lock = RLock() # Re-entrant Lock
|
|
|
|
|
|
|
|
async def on_connect(self, websocket: WebSocketType):
|
|
|
|
"""
|
|
|
|
Wrap websocket connection into Channel and add to list
|
|
|
|
|
|
|
|
:param websocket: The WebSocket object to attach to the Channel
|
|
|
|
"""
|
2022-09-06 18:40:58 +00:00
|
|
|
if isinstance(websocket, FastAPIWebSocket):
|
2022-08-29 19:41:15 +00:00
|
|
|
try:
|
|
|
|
await websocket.accept()
|
|
|
|
except RuntimeError:
|
|
|
|
# The connection was closed before we could accept it
|
|
|
|
return
|
|
|
|
|
|
|
|
ws_channel = WebSocketChannel(websocket)
|
|
|
|
|
|
|
|
with self._lock:
|
|
|
|
self.channels[websocket] = ws_channel
|
|
|
|
|
|
|
|
return ws_channel
|
|
|
|
|
|
|
|
async def on_disconnect(self, websocket: WebSocketType):
|
|
|
|
"""
|
|
|
|
Call close on the channel if it's not, and remove from channel list
|
|
|
|
|
|
|
|
:param websocket: The WebSocket objet attached to the Channel
|
|
|
|
"""
|
|
|
|
with self._lock:
|
|
|
|
channel = self.channels.get(websocket)
|
|
|
|
if channel:
|
2022-10-23 01:02:05 +00:00
|
|
|
logger.info(f"Disconnecting channel {channel}")
|
2022-08-29 19:41:15 +00:00
|
|
|
if not channel.is_closed():
|
|
|
|
await channel.close()
|
|
|
|
|
|
|
|
del self.channels[websocket]
|
|
|
|
|
|
|
|
async def disconnect_all(self):
|
|
|
|
"""
|
|
|
|
Disconnect all Channels
|
|
|
|
"""
|
|
|
|
with self._lock:
|
2022-10-23 01:02:05 +00:00
|
|
|
for websocket in self.channels.copy().keys():
|
|
|
|
await self.on_disconnect(websocket)
|
2022-08-29 19:41:15 +00:00
|
|
|
|
2022-10-25 17:36:40 +00:00
|
|
|
async def broadcast(self, message: WSMessageSchemaType):
|
2022-08-29 19:41:15 +00:00
|
|
|
"""
|
2022-10-23 17:42:59 +00:00
|
|
|
Broadcast a message on all Channels
|
2022-08-29 19:41:15 +00:00
|
|
|
|
2022-10-23 17:42:59 +00:00
|
|
|
:param message: The message to send
|
2022-08-29 19:41:15 +00:00
|
|
|
"""
|
|
|
|
with self._lock:
|
2022-10-23 01:02:05 +00:00
|
|
|
for channel in self.channels.copy().values():
|
2022-10-25 17:36:40 +00:00
|
|
|
if channel.subscribed_to(message.get('type')):
|
2022-10-23 17:42:59 +00:00
|
|
|
await self.send_direct(channel, message)
|
2022-08-29 19:41:15 +00:00
|
|
|
|
2022-10-25 17:36:40 +00:00
|
|
|
async def send_direct(
|
|
|
|
self, channel: WebSocketChannel, message: Union[WSMessageSchemaType, Dict[str, Any]]):
|
2022-08-29 19:41:15 +00:00
|
|
|
"""
|
2022-10-23 17:42:59 +00:00
|
|
|
Send a message directly through direct_channel only
|
2022-08-29 19:41:15 +00:00
|
|
|
|
2022-10-23 17:42:59 +00:00
|
|
|
:param direct_channel: The WebSocketChannel object to send the message through
|
|
|
|
:param message: The message to send
|
2022-08-29 19:41:15 +00:00
|
|
|
"""
|
2022-10-25 17:36:40 +00:00
|
|
|
if not await channel.send(message):
|
2022-10-23 17:42:59 +00:00
|
|
|
await self.on_disconnect(channel.raw_websocket)
|
2022-08-29 19:41:15 +00:00
|
|
|
|
|
|
|
def has_channels(self):
|
|
|
|
"""
|
|
|
|
Flag for more than 0 channels
|
|
|
|
"""
|
|
|
|
return len(self.channels) > 0
|