harden sse connections to backends a little.

This commit is contained in:
fiatjaf
2020-10-09 20:55:58 -03:00
parent 7f41d73a84
commit 1d00060419
4 changed files with 82 additions and 57 deletions
+15 -7
View File
@@ -1,3 +1,4 @@
import trio # type: ignore
import random
import json
import httpx
@@ -96,10 +97,17 @@ class SparkWallet(Wallet):
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
url = self.url + "/stream?access-key=" + self.token
async with httpx.AsyncClient(timeout=None) as client:
async with client.stream("GET", url) as r:
async for line in r.aiter_lines():
if line.startswith("data:"):
data = json.loads(line[5:])
if "pay_index" in data and data.get("status") == "paid":
yield data["label"]
while True:
try:
async with httpx.AsyncClient(timeout=None) as client:
async with client.stream("GET", url) as r:
async for line in r.aiter_lines():
if line.startswith("data:"):
data = json.loads(line[5:])
if "pay_index" in data and data.get("status") == "paid":
yield data["label"]
except (OSError, httpx.ReadError):
pass
print("lost connection to spark /stream, retrying in 5 seconds")
await trio.sleep(5)