format and types
This commit is contained in:
parent
1d8dc8803d
commit
036285b673
1 changed files with 18 additions and 19 deletions
37
nwcp.py
37
nwcp.py
|
|
@ -16,6 +16,11 @@ from lnbits.settings import settings
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
|
|
||||||
|
class RateLimit:
|
||||||
|
backoff: int = 0
|
||||||
|
last_attempt_time: int = 0
|
||||||
|
|
||||||
|
|
||||||
class MainSubscription:
|
class MainSubscription:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.requests_sub_id: Optional[str] = None
|
self.requests_sub_id: Optional[str] = None
|
||||||
|
|
@ -86,8 +91,8 @@ class NWCServiceProvider:
|
||||||
|
|
||||||
# Subscription
|
# Subscription
|
||||||
self.sub = None
|
self.sub = None
|
||||||
self.rate_limit = {}
|
self.rate_limit: Dict[str, RateLimit] = {}
|
||||||
|
|
||||||
# websocket connection
|
# websocket connection
|
||||||
self.ws = None
|
self.ws = None
|
||||||
|
|
||||||
|
|
@ -200,28 +205,22 @@ class NWCServiceProvider:
|
||||||
logger.debug("Waiting for connection...")
|
logger.debug("Waiting for connection...")
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
async def _ratelimit(self, unit, max_sleep_time = 120):
|
async def _ratelimit(self, unit: str, max_sleep_time: int = 120) -> None:
|
||||||
rate_limit = self.rate_limit.get(unit)
|
limit: Optional[RateLimit] = self.rate_limit.get(unit)
|
||||||
if not rate_limit:
|
if not limit:
|
||||||
self.rate_limit[unit] = rate_limit = {
|
self.rate_limit[unit] = limit = RateLimit()
|
||||||
"backoff": 0,
|
|
||||||
"last_attempt_time": 0
|
|
||||||
}
|
|
||||||
|
|
||||||
if time.time() - rate_limit["last_attempt_time"] > max_sleep_time:
|
if time.time() - limit.last_attempt_time > max_sleep_time:
|
||||||
# reset backoff if action lasted more than max_sleep_time
|
# reset backoff if action lasted more than max_sleep_time
|
||||||
rate_limit["backoff"] = 0
|
limit.backoff = 0
|
||||||
else:
|
else:
|
||||||
# increase backoff
|
# increase backoff
|
||||||
rate_limit["backoff"] = (
|
limit.backoff = (
|
||||||
min(rate_limit["backoff"] * 2, max_sleep_time)
|
min(limit.backoff * 2, max_sleep_time) if limit.backoff > 0 else 1
|
||||||
if rate_limit["backoff"] > 0
|
|
||||||
else 1
|
|
||||||
)
|
)
|
||||||
logger.debug(
|
logger.debug("Sleeping for " + str(limit.backoff) + " seconds before " + unit)
|
||||||
"Sleeping for " + str(rate_limit["backoff"]) + " seconds before " + unit)
|
await asyncio.sleep(limit.backoff)
|
||||||
await asyncio.sleep( rate_limit["backoff"])
|
limit.last_attempt_time = int(time.time())
|
||||||
rate_limit["last_attempt_time"] = time.time()
|
|
||||||
|
|
||||||
async def _subscribe(self):
|
async def _subscribe(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue