make it so LNbitsWallet reconnects if the listener stream goes off.
This commit is contained in:
parent
42b3359d12
commit
3d489bf2ee
1 changed files with 21 additions and 11 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
import trio # type: ignore
|
||||||
import json
|
import json
|
||||||
import httpx
|
import httpx
|
||||||
from os import getenv
|
from os import getenv
|
||||||
|
|
@ -116,16 +117,25 @@ class LNbitsWallet(Wallet):
|
||||||
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
|
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
|
||||||
url = f"{self.endpoint}/api/v1/payments/sse"
|
url = f"{self.endpoint}/api/v1/payments/sse"
|
||||||
|
|
||||||
async with httpx.AsyncClient(timeout=None, headers=self.key) as client:
|
while True:
|
||||||
async with client.stream("GET", url) as r:
|
try:
|
||||||
async for line in r.aiter_lines():
|
async with httpx.AsyncClient(timeout=None, headers=self.key) as client:
|
||||||
if line.startswith("data:"):
|
async with client.stream("GET", url) as r:
|
||||||
try:
|
async for line in r.aiter_lines():
|
||||||
data = json.loads(line[5:])
|
if line.startswith("data:"):
|
||||||
except json.decoder.JSONDecodeError:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if type(data) is not dict:
|
try:
|
||||||
continue
|
data = json.loads(line[5:])
|
||||||
|
except json.decoder.JSONDecodeError:
|
||||||
|
continue
|
||||||
|
|
||||||
yield data["payment_hash"] # payment_hash
|
if type(data) is not dict:
|
||||||
|
continue
|
||||||
|
|
||||||
|
yield data["payment_hash"] # payment_hash
|
||||||
|
|
||||||
|
except (OSError, httpx.ReadError, httpx.ConnectError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
print("lost connection to lnbits /payments/sse, retrying in 5 seconds")
|
||||||
|
await trio.sleep(5)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue