Merge pull request #1205 from lnbits/fix/wallets/lnbits/invoice_listener

LnbitsWallet invoice listener fix: disable compression
This commit is contained in:
calle 2022-12-16 17:34:45 +01:00 committed by GitHub
commit 6b0b28e717
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 12 deletions

View File

@ -416,7 +416,7 @@ async def subscribe_wallet_invoices(request: Request, wallet: Wallet):
yield dict(data=jdata, event=typ)
except asyncio.CancelledError as e:
logger.debug(f"CancelledError on listener {uid}: {e}")
logger.debug(f"removing listener for wallet {uid}")
api_invoice_listeners.pop(uid)
task.cancel()
return

View File

@ -147,18 +147,26 @@ class LNbitsWallet(Wallet):
while True:
try:
async with httpx.AsyncClient(timeout=None, headers=self.key) as client:
async with client.stream("GET", url) as r:
del client.headers[
"accept-encoding"
] # we have to disable compression for SSEs
async with client.stream(
"GET", url, content="text/event-stream"
) as r:
sse_trigger = False
async for line in r.aiter_lines():
if line.startswith("data:"):
try:
data = json.loads(line[5:])
except json.decoder.JSONDecodeError:
continue
if type(data) is not dict:
continue
yield data["payment_hash"] # payment_hash
# The data we want to listen to is of this shape:
# event: payment-received
# data: {.., "payment_hash" : "asd"}
if line.startswith("event: payment-received"):
sse_trigger = True
continue
elif sse_trigger and line.startswith("data:"):
data = json.loads(line[len("data:") :])
sse_trigger = False
yield data["payment_hash"]
else:
sse_trigger = False
except (OSError, httpx.ReadError, httpx.ConnectError, httpx.ReadTimeout):
pass