2022-08-29 19:41:15 +00:00
|
|
|
import logging
|
2022-11-25 19:48:57 +00:00
|
|
|
import time
|
2022-08-31 16:40:26 +00:00
|
|
|
from typing import Any, Dict
|
2022-08-29 19:41:15 +00:00
|
|
|
|
2022-11-15 03:27:45 +00:00
|
|
|
from fastapi import APIRouter, Depends
|
2022-11-18 20:32:27 +00:00
|
|
|
from fastapi.websockets import WebSocket
|
2022-09-08 19:58:28 +00:00
|
|
|
from pydantic import ValidationError
|
2022-08-29 19:41:15 +00:00
|
|
|
|
2022-08-31 16:40:26 +00:00
|
|
|
from freqtrade.enums import RPCMessageType, RPCRequestType
|
2022-09-10 12:19:11 +00:00
|
|
|
from freqtrade.rpc.api_server.api_auth import validate_ws_token
|
2022-11-15 03:27:45 +00:00
|
|
|
from freqtrade.rpc.api_server.deps import get_message_stream, get_rpc
|
2022-11-18 20:32:27 +00:00
|
|
|
from freqtrade.rpc.api_server.ws.channel import WebSocketChannel, create_channel
|
2022-11-15 03:27:45 +00:00
|
|
|
from freqtrade.rpc.api_server.ws.message_stream import MessageStream
|
2022-09-08 19:58:28 +00:00
|
|
|
from freqtrade.rpc.api_server.ws_schemas import (WSAnalyzedDFMessage, WSMessageSchema,
|
|
|
|
WSRequestSchema, WSWhitelistMessage)
|
2022-09-02 02:06:36 +00:00
|
|
|
from freqtrade.rpc.rpc import RPC
|
2022-08-29 19:41:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
# Private router, protected by API Key authentication
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
2022-11-15 03:27:45 +00:00
|
|
|
async def channel_reader(channel: WebSocketChannel, rpc: RPC):
|
|
|
|
"""
|
|
|
|
Iterate over the messages from the channel and process the request
|
|
|
|
"""
|
2022-11-18 20:32:27 +00:00
|
|
|
async for message in channel:
|
|
|
|
await _process_consumer_request(message, channel, rpc)
|
2022-11-15 03:27:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def channel_broadcaster(channel: WebSocketChannel, message_stream: MessageStream):
|
2022-09-07 21:08:01 +00:00
|
|
|
"""
|
2022-11-15 03:27:45 +00:00
|
|
|
Iterate over messages in the message stream and send them
|
2022-09-07 21:08:01 +00:00
|
|
|
"""
|
2022-11-25 19:48:57 +00:00
|
|
|
async for message, ts in message_stream:
|
2022-11-22 16:42:09 +00:00
|
|
|
if channel.subscribed_to(message.get('type')):
|
2022-11-25 19:48:57 +00:00
|
|
|
# Log a warning if this channel is behind
|
|
|
|
# on the message stream by a lot
|
|
|
|
if (time.time() - ts) > 60:
|
2022-11-25 20:10:22 +00:00
|
|
|
logger.warning(f"Channel {channel} is behind MessageStream by 1 minute,"
|
2022-11-25 19:48:57 +00:00
|
|
|
" this can cause a memory leak if you see this message"
|
|
|
|
" often, consider reducing pair list size or amount of"
|
|
|
|
" consumers.")
|
|
|
|
|
2022-11-22 16:42:09 +00:00
|
|
|
await channel.send(message, timeout=True)
|
2022-09-04 16:22:10 +00:00
|
|
|
|
|
|
|
|
2022-09-02 02:06:36 +00:00
|
|
|
async def _process_consumer_request(
|
|
|
|
request: Dict[str, Any],
|
|
|
|
channel: WebSocketChannel,
|
2022-11-15 03:27:45 +00:00
|
|
|
rpc: RPC
|
2022-09-02 02:06:36 +00:00
|
|
|
):
|
2022-09-07 21:08:01 +00:00
|
|
|
"""
|
|
|
|
Validate and handle a request from a websocket consumer
|
|
|
|
"""
|
|
|
|
# Validate the request, makes sure it matches the schema
|
|
|
|
try:
|
|
|
|
websocket_request = WSRequestSchema.parse_obj(request)
|
|
|
|
except ValidationError as e:
|
|
|
|
logger.error(f"Invalid request from {channel}: {e}")
|
|
|
|
return
|
|
|
|
|
|
|
|
type, data = websocket_request.type, websocket_request.data
|
2022-09-08 16:34:37 +00:00
|
|
|
response: WSMessageSchema
|
2022-08-31 16:40:26 +00:00
|
|
|
|
2022-09-02 21:05:16 +00:00
|
|
|
logger.debug(f"Request of type {type} from {channel}")
|
|
|
|
|
2022-08-31 16:40:26 +00:00
|
|
|
# If we have a request of type SUBSCRIBE, set the topics in this channel
|
|
|
|
if type == RPCRequestType.SUBSCRIBE:
|
2022-09-02 02:06:36 +00:00
|
|
|
# If the request is empty, do nothing
|
|
|
|
if not data:
|
|
|
|
return
|
|
|
|
|
2022-08-31 16:40:26 +00:00
|
|
|
# If all topics passed are a valid RPCMessageType, set subscriptions on channel
|
|
|
|
if all([any(x.value == topic for x in RPCMessageType) for topic in data]):
|
|
|
|
channel.set_subscriptions(data)
|
|
|
|
|
2022-09-07 21:08:01 +00:00
|
|
|
# We don't send a response for subscriptions
|
2022-09-08 16:34:37 +00:00
|
|
|
return
|
2022-09-07 21:08:01 +00:00
|
|
|
|
2022-09-02 05:52:13 +00:00
|
|
|
elif type == RPCRequestType.WHITELIST:
|
2022-09-07 21:08:01 +00:00
|
|
|
# Get whitelist
|
2022-09-02 05:52:13 +00:00
|
|
|
whitelist = rpc._ws_request_whitelist()
|
2022-09-02 02:06:36 +00:00
|
|
|
|
2022-09-07 21:08:01 +00:00
|
|
|
# Format response
|
2022-10-23 17:42:59 +00:00
|
|
|
response = WSWhitelistMessage(data=whitelist)
|
2022-11-15 03:27:45 +00:00
|
|
|
await channel.send(response.dict(exclude_none=True))
|
2022-09-02 02:06:36 +00:00
|
|
|
|
2022-09-02 05:52:13 +00:00
|
|
|
elif type == RPCRequestType.ANALYZED_DF:
|
2022-11-18 20:32:27 +00:00
|
|
|
# Limit the amount of candles per dataframe to 'limit' or 1500
|
|
|
|
limit = min(data.get('limit', 1500), 1500) if data else None
|
2022-09-02 21:05:16 +00:00
|
|
|
|
2022-11-14 06:25:52 +00:00
|
|
|
# For every pair in the generator, send a separate message
|
2022-11-26 12:33:54 +00:00
|
|
|
for message in rpc._ws_request_analyzed_df(limit):
|
2022-11-15 03:27:45 +00:00
|
|
|
# Format response
|
2022-10-23 17:42:59 +00:00
|
|
|
response = WSAnalyzedDFMessage(data=message)
|
2022-11-15 03:27:45 +00:00
|
|
|
await channel.send(response.dict(exclude_none=True))
|
2022-09-02 02:06:36 +00:00
|
|
|
|
2022-08-31 16:40:26 +00:00
|
|
|
|
2022-08-29 19:41:15 +00:00
|
|
|
@router.websocket("/message/ws")
|
|
|
|
async def message_endpoint(
|
2022-11-15 03:27:45 +00:00
|
|
|
websocket: WebSocket,
|
|
|
|
token: str = Depends(validate_ws_token),
|
2022-09-02 02:06:36 +00:00
|
|
|
rpc: RPC = Depends(get_rpc),
|
2022-11-15 03:27:45 +00:00
|
|
|
message_stream: MessageStream = Depends(get_message_stream)
|
2022-08-29 19:41:15 +00:00
|
|
|
):
|
2022-11-24 20:41:10 +00:00
|
|
|
if token:
|
|
|
|
async with create_channel(websocket) as channel:
|
|
|
|
await channel.run_channel_tasks(
|
|
|
|
channel_reader(channel, rpc),
|
|
|
|
channel_broadcaster(channel, message_stream)
|
|
|
|
)
|