initial revision

This commit is contained in:
Timothy Pogue
2022-11-14 20:27:45 -07:00
parent a951b49541
commit 659c8c237f
7 changed files with 494 additions and 241 deletions

View File

@@ -1,7 +1,6 @@
import asyncio
import logging
from ipaddress import IPv4Address
from threading import Thread
from typing import Any, Dict
import orjson
@@ -15,7 +14,7 @@ from starlette.responses import JSONResponse
from freqtrade.constants import Config
from freqtrade.exceptions import OperationalException
from freqtrade.rpc.api_server.uvicorn_threaded import UvicornServer
from freqtrade.rpc.api_server.ws import ChannelManager
from freqtrade.rpc.api_server.ws.message_stream import MessageStream
from freqtrade.rpc.api_server.ws_schemas import WSMessageSchemaType
from freqtrade.rpc.rpc import RPC, RPCException, RPCHandler
@@ -51,9 +50,10 @@ class ApiServer(RPCHandler):
# Exchange - only available in webserver mode.
_exchange = None
# websocket message queue stuff
_ws_channel_manager = None
_ws_thread = None
_ws_loop = None
# _ws_channel_manager = None
# _ws_thread = None
# _ws_loop = None
_message_stream = None
def __new__(cls, *args, **kwargs):
"""
@@ -71,14 +71,15 @@ class ApiServer(RPCHandler):
return
self._standalone: bool = standalone
self._server = None
self._ws_queue = None
self._ws_background_task = None
self._ws_publisher_task = None
ApiServer.__initialized = True
api_config = self._config['api_server']
ApiServer._ws_channel_manager = ChannelManager()
# ApiServer._ws_channel_manager = ChannelManager()
self.app = FastAPI(title="Freqtrade API",
docs_url='/docs' if api_config.get('enable_openapi', False) else None,
@@ -107,18 +108,18 @@ class ApiServer(RPCHandler):
logger.info("Stopping API Server")
self._server.cleanup()
if self._ws_thread and self._ws_loop:
logger.info("Stopping API Server background tasks")
# if self._ws_thread and self._ws_loop:
# logger.info("Stopping API Server background tasks")
if self._ws_background_task:
# Cancel the queue task
self._ws_background_task.cancel()
# if self._ws_background_task:
# # Cancel the queue task
# self._ws_background_task.cancel()
self._ws_thread.join()
# self._ws_thread.join()
self._ws_thread = None
self._ws_loop = None
self._ws_background_task = None
# self._ws_thread = None
# self._ws_loop = None
# self._ws_background_task = None
@classmethod
def shutdown(cls):
@@ -170,51 +171,102 @@ class ApiServer(RPCHandler):
)
app.add_exception_handler(RPCException, self.handle_rpc_exception)
app.add_event_handler(
event_type="startup",
func=self._api_startup_event
)
app.add_event_handler(
event_type="shutdown",
func=self._api_shutdown_event
)
def start_message_queue(self):
if self._ws_thread:
return
async def _api_startup_event(self):
if not ApiServer._message_stream:
ApiServer._message_stream = MessageStream()
# Create a new loop, as it'll be just for the background thread
self._ws_loop = asyncio.new_event_loop()
if not self._ws_queue:
self._ws_queue = ThreadedQueue()
# Start the thread
self._ws_thread = Thread(target=self._ws_loop.run_forever)
self._ws_thread.start()
if not self._ws_publisher_task:
self._ws_publisher_task = asyncio.create_task(
self._publish_messages()
)
# Finally, submit the coro to the thread
self._ws_background_task = asyncio.run_coroutine_threadsafe(
self._broadcast_queue_data(), loop=self._ws_loop)
async def _api_shutdown_event(self):
if ApiServer._message_stream:
ApiServer._message_stream = None
async def _broadcast_queue_data(self):
# Instantiate the queue in this coroutine so it's attached to our loop
self._ws_queue = ThreadedQueue()
async_queue = self._ws_queue.async_q
try:
while True:
logger.debug("Getting queue messages...")
# Get data from queue
message: WSMessageSchemaType = await async_queue.get()
logger.debug(f"Found message of type: {message.get('type')}")
async_queue.task_done()
# Broadcast it
await self._ws_channel_manager.broadcast(message)
except asyncio.CancelledError:
pass
# For testing, shouldn't happen when stable
except Exception as e:
logger.exception(f"Exception happened in background task: {e}")
finally:
# Disconnect channels and stop the loop on cancel
await self._ws_channel_manager.disconnect_all()
self._ws_loop.stop()
# Avoid adding more items to the queue if they aren't
# going to get broadcasted.
if self._ws_queue:
self._ws_queue = None
if self._ws_publisher_task:
self._ws_publisher_task.cancel()
async def _publish_messages(self):
"""
Background task that reads messages from the queue and adds them
to the message stream
"""
try:
async_queue = self._ws_queue.async_q
message_stream = ApiServer._message_stream
while message_stream:
message: WSMessageSchemaType = await async_queue.get()
message_stream.publish(message)
# Make sure to throttle how fast we
# publish messages as some clients will be
# slower than others
await asyncio.sleep(0.01)
async_queue.task_done()
finally:
self._ws_queue = None
# def start_message_queue(self):
# if self._ws_thread:
# return
# # Create a new loop, as it'll be just for the background thread
# self._ws_loop = asyncio.new_event_loop()
# # Start the thread
# self._ws_thread = Thread(target=self._ws_loop.run_forever)
# self._ws_thread.start()
# # Finally, submit the coro to the thread
# self._ws_background_task = asyncio.run_coroutine_threadsafe(
# self._broadcast_queue_data(), loop=self._ws_loop)
# async def _broadcast_queue_data(self):
# # Instantiate the queue in this coroutine so it's attached to our loop
# self._ws_queue = ThreadedQueue()
# async_queue = self._ws_queue.async_q
# try:
# while True:
# logger.debug("Getting queue messages...")
# # Get data from queue
# message: WSMessageSchemaType = await async_queue.get()
# logger.debug(f"Found message of type: {message.get('type')}")
# async_queue.task_done()
# # Broadcast it
# await self._ws_channel_manager.broadcast(message)
# except asyncio.CancelledError:
# pass
# # For testing, shouldn't happen when stable
# except Exception as e:
# logger.exception(f"Exception happened in background task: {e}")
# finally:
# # Disconnect channels and stop the loop on cancel
# await self._ws_channel_manager.disconnect_all()
# self._ws_loop.stop()
# # Avoid adding more items to the queue if they aren't
# # going to get broadcasted.
# self._ws_queue = None
def start_api(self):
"""
Start API ... should be run in thread.
@@ -253,7 +305,7 @@ class ApiServer(RPCHandler):
if self._standalone:
self._server.run()
else:
self.start_message_queue()
# self.start_message_queue()
self._server.run_in_thread()
except Exception:
logger.exception("Api server failed to start.")