migrate to trio so c-lightning sockets stop hanging.

This commit is contained in:
fiatjaf
2020-10-04 12:06:20 -03:00
parent e74cf33f90
commit 9994e61615
15 changed files with 185 additions and 79 deletions
+28 -21
View File
@@ -1,7 +1,8 @@
import asyncio
import trio # type: ignore
from http import HTTPStatus
from typing import Optional, Tuple, List, Callable, Awaitable
from quart import Quart, Request, g
from quart import Request, g
from quart_trio import QuartTrio
from werkzeug.datastructures import Headers
from lnbits.db import open_db, open_ext_db
@@ -10,7 +11,7 @@ from lnbits.settings import WALLET
from .models import Payment
from .crud import get_standalone_payment
main_app: Optional[Quart] = None
main_app: Optional[QuartTrio] = None
def grab_app_for_later(state):
@@ -18,24 +19,30 @@ def grab_app_for_later(state):
main_app = state.app
def run_on_pseudo_request(awaitable: Awaitable):
async def run(awaitable):
fk = Request(
"GET",
"http",
"/background/pseudo",
b"",
Headers([("host", "lnbits.background")]),
"",
"1.1",
send_push_promise=lambda x, h: None,
)
async with main_app.request_context(fk):
with open_db() as g.db:
await awaitable
async def send_push_promise(a, b) -> None:
pass
loop = asyncio.get_event_loop()
loop.create_task(run(awaitable))
async def run_on_pseudo_request(func: Callable, *args):
fk = Request(
"GET",
"http",
"/background/pseudo",
b"",
Headers([("host", "lnbits.background")]),
"",
"1.1",
send_push_promise=send_push_promise,
)
assert main_app
async def run():
async with main_app.request_context(fk):
with open_db() as g.db: # type: ignore
await func(*args)
async with trio.open_nursery() as nursery:
nursery.start_soon(run)
invoice_listeners: List[Tuple[str, Callable[[Payment], Awaitable[None]]]] = []
@@ -59,7 +66,7 @@ async def webhook_handler():
async def invoice_listener():
async for checking_id in WALLET.paid_invoices_stream():
run_on_pseudo_request(invoice_callback_dispatcher(checking_id))
await run_on_pseudo_request(invoice_callback_dispatcher, checking_id)
async def invoice_callback_dispatcher(checking_id: str):