wowlet-backend/fapi/routes.py
dsc 42bb0c832e Feather-ws rewrite;
- Move recurring tasks into their own class; inherits from `FeatherTask`
- CCS proposals: Don't use API, it's broken - webcrawl instead until it is fixed.
- Switch to hypercorn as the ASGI server, *with* support for multiple workers. You can now run feather-ws with, for example, `--workers 6`. See `Dockerfile`.
- Introduce support for various coins under `BlockheightTask`
- Introduce support for various Reddit communities under `RedditTask`
- Introduced weightvoting whilst validating third-party RPC blockheights - where nodes are filtered based on what other nodes are commonly reporting.
- Current blockheights are fetched from various block explorers and weightvoting is done to eliminate outliers under `BlockheightTask`.
- Don't filter/remove bad nodes from the rpc_nodes list; correctly label them as disabled/bad nodes.
- Multiple Feather instances (each for it's own coin) can now run on one machine, using only one Redis instance, as each coins has it's own Redis database index.
- Configuration options inside `settings.py` can now be controlled via environment variables.
- Better logging through custom log formatting and correct usage of `app.logger.*`
- Fixed a bug where one task could overlap with itself if the previous one did not finish yet. This was particularly noticable inside the `RPCNodeCheckTask` where the high timeout (for Tor nodes) could cause the task to run *longer* than the recurring task interval.
- Introduced a `docker-compose.yml` to combine the Feather container with Redis and Tor containers.
- Blocking IO operations are now done via `aiofiles`
2020-12-22 19:07:24 +01:00

69 lines
1.8 KiB
Python

# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2020, The Monero Project.
# Copyright (c) 2020, dsc@xmr.pm
import asyncio
import json
from quart import websocket, jsonify
from fapi.factory import app
from fapi.wsparse import WebsocketParse
from fapi.utils import collect_websocket, feather_data
@app.route("/")
async def root():
data = await feather_data()
return jsonify(data)
@app.websocket('/ws')
@collect_websocket
async def ws(queue):
data = await feather_data()
# blast available data on connect
for task_key, task_value in data.items():
if not task_value:
continue
await websocket.send(json.dumps({"cmd": task_key, "data": task_value}).encode())
async def rx():
while True:
buffer = await websocket.receive()
try:
blob = json.loads(buffer)
if "cmd" not in blob:
continue
cmd = blob.get('cmd')
_data = blob.get('data')
result = await WebsocketParse.parser(cmd, _data)
if result:
rtn = json.dumps({"cmd": cmd, "data": result}).encode()
await websocket.send(rtn)
except Exception as ex:
continue
async def tx():
while True:
data = await queue.get()
payload = json.dumps(data).encode()
await websocket.send(payload)
# bidirectional async rx and tx loops
consumer_task = asyncio.ensure_future(rx())
producer_task = asyncio.ensure_future(tx())
try:
await asyncio.gather(consumer_task, producer_task)
finally:
consumer_task.cancel()
producer_task.cancel()
@app.errorhandler(403)
@app.errorhandler(404)
@app.errorhandler(405)
@app.errorhandler(500)
def page_not_found(e):
return ":)", 500