update typing, remove unneeded try block, readd sleep

This commit is contained in:
Timothy Pogue 2022-11-20 16:18:24 -07:00
parent 60a167bdef
commit 48a1f2418f
1 changed files with 15 additions and 6 deletions

View File

@ -3,7 +3,7 @@ import logging
import time import time
from collections import deque from collections import deque
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from typing import Any, Deque, Dict, List, Optional, Type, Union from typing import Any, AsyncGenerator, Deque, Dict, List, Optional, Type, Union
from uuid import uuid4 from uuid import uuid4
from freqtrade.rpc.api_server.ws.proxy import WebSocketProxy from freqtrade.rpc.api_server.ws.proxy import WebSocketProxy
@ -99,6 +99,15 @@ class WebSocketChannel:
logger.info(f"Connection for {self} is too far behind, disconnecting") logger.info(f"Connection for {self} is too far behind, disconnecting")
raise raise
# Without this sleep, messages would send to one channel
# first then another after the first one finished and prevent
# any normal Rest API calls from processing at the same time.
# With the sleep call, it gives control to the event
# loop to schedule other channel send methods, and helps
# throttle how fast we send.
# 0.01 = 100 messages/second max throughput
await asyncio.sleep(0.01)
async def recv(self): async def recv(self):
""" """
Receive a message on the wrapped websocket Receive a message on the wrapped websocket
@ -180,10 +189,7 @@ class WebSocketChannel:
task.cancel() task.cancel()
# Wait for tasks to finish cancelling # Wait for tasks to finish cancelling
try: await asyncio.wait(self._channel_tasks)
await asyncio.wait(self._channel_tasks)
except asyncio.CancelledError:
pass
self._channel_tasks = [] self._channel_tasks = []
@ -199,7 +205,10 @@ class WebSocketChannel:
@asynccontextmanager @asynccontextmanager
async def create_channel(websocket: WebSocketType, **kwargs): async def create_channel(
websocket: WebSocketType,
**kwargs
) -> AsyncGenerator[WebSocketChannel, None]:
""" """
Context manager for safely opening and closing a WebSocketChannel Context manager for safely opening and closing a WebSocketChannel
""" """