remove timer

This commit is contained in:
callebtc 2023-03-08 17:02:41 +01:00
parent 4bcdd106be
commit a8b0f9b511

View file

@ -1,16 +1,10 @@
from http import HTTPStatus from http import HTTPStatus
import asyncio import asyncio
import ssl
import json import json
import datetime
from typing import List, Union from typing import List, Union
from fastapi import Request, WebSocket, WebSocketDisconnect from fastapi import WebSocket, WebSocketDisconnect
from fastapi.param_functions import Query from fastapi.param_functions import Query
from fastapi.params import Depends from fastapi.params import Depends
from fastapi.responses import JSONResponse
from starlette.exceptions import HTTPException
from sse_starlette.sse import EventSourceResponse
from loguru import logger from loguru import logger
from . import nostrclient_ext from . import nostrclient_ext
@ -21,10 +15,8 @@ from .crud import get_relays, add_relay, delete_relay
from .models import RelayList, Relay, Event, Filter, Filters from .models import RelayList, Relay, Event, Filter, Filters
from .nostr.event import Event as NostrEvent from .nostr.event import Event as NostrEvent
from .nostr.event import EncryptedDirectMessage
from .nostr.filter import Filter as NostrFilter from .nostr.filter import Filter as NostrFilter
from .nostr.filter import Filters as NostrFilters from .nostr.filter import Filters as NostrFilters
from .nostr.message_type import ClientMessageType
from lnbits.decorators import ( from lnbits.decorators import (
WalletTypeInfo, WalletTypeInfo,
@ -121,7 +113,6 @@ async def ws_relay(websocket: WebSocket):
await websocket.accept() await websocket.accept()
my_subscriptions: List[str] = [] my_subscriptions: List[str] = []
connected: bool = True connected: bool = True
last_sent: datetime.datetime = datetime.datetime.now()
async def client_to_nostr(websocket): async def client_to_nostr(websocket):
"""Receives requests / data from the client and forwards it to relays. If the """Receives requests / data from the client and forwards it to relays. If the
@ -129,7 +120,6 @@ async def ws_relay(websocket: WebSocket):
Remembers the subscription id so we can send back responses from the relay to this Remembers the subscription id so we can send back responses from the relay to this
client in `nostr_to_client`""" client in `nostr_to_client`"""
nonlocal my_subscriptions nonlocal my_subscriptions
nonlocal last_sent
nonlocal connected nonlocal connected
while True: while True:
try: try:
@ -147,9 +137,6 @@ async def ws_relay(websocket: WebSocket):
# publish data # publish data
client.relay_manager.publish_message(json_str) client.relay_manager.publish_message(json_str)
# update timestamp of last sent data
last_sent = datetime.datetime.now()
async def nostr_to_client(websocket): async def nostr_to_client(websocket):
"""Sends responses from relays back to the client. Polls the subscriptions of this client """Sends responses from relays back to the client. Polls the subscriptions of this client
stored in `my_subscriptions`. Then gets all responses for this subscription id from `received_subscription_events` which stored in `my_subscriptions`. Then gets all responses for this subscription id from `received_subscription_events` which
@ -183,12 +170,10 @@ async def ws_relay(websocket: WebSocket):
asyncio.create_task(client_to_nostr(websocket)) asyncio.create_task(client_to_nostr(websocket))
asyncio.create_task(nostr_to_client(websocket)) asyncio.create_task(nostr_to_client(websocket))
# we kill this websocket and the subscriptions if no data was sent for # we kill this websocket and the subscriptions if the user disconnects and thus `connected==False`
# more than 10 minutes _or_ if the user disconnects and thus `connected==False`
while True: while True:
await asyncio.sleep(10) await asyncio.sleep(10)
if ( if not connected:
datetime.datetime.now() - last_sent > datetime.timedelta(minutes=10) for s in my_subscriptions:
or not connected client.relay_manager.close_subscription(s)
):
break break