Skip to content

Commit

Permalink
add rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl committed Nov 19, 2024
1 parent 2e4f3dc commit 81224f6
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions nwcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ def __init__(self, private_key: Optional[str] = None, relay: Optional[str] = Non

# Subscription
self.sub = None

self.rate_limit = {}

# websocket connection
self.ws = None

Expand Down Expand Up @@ -199,6 +200,29 @@ async def _wait_for_connection(self):
logger.debug("Waiting for connection...")
await asyncio.sleep(1)

async def _ratelimit(self, unit, max_sleep_time = 120):
rate_limit = self.rate_limit.get(unit)
if not rate_limit:
self.rate_limit[unit] = rate_limit = {
"backoff": 0,
"last_attempt_time": 0
}

if time.time() - rate_limit["last_attempt_time"] > max_sleep_time:
# reset backoff if action lasted more than max_sleep_time
rate_limit["backoff"] = 0
else:
# increase backoff
rate_limit["backoff"] = (
min(rate_limit["backoff"] * 2, max_sleep_time)
if rate_limit["backoff"] > 0
else 1
)
logger.debug(
"Sleeping for " + str(rate_limit["backoff"]) + " seconds before " + unit)
await asyncio.sleep( rate_limit["backoff"])
rate_limit["last_attempt_time"] = time.time()

async def _subscribe(self):
"""
[Re]Subscribe to receive nip 47 requests and responses from the relay
Expand Down Expand Up @@ -383,6 +407,7 @@ async def _on_closed_message(self, msg):
+ info
+ " ... resubscribing..."
)
await self._ratelimit("subscribing")
await self._subscribe()

async def _on_message(self, ws, message: str):
Expand All @@ -403,7 +428,7 @@ async def _on_message(self, ws, message: str):
elif msg[0] == "OK":
pass
else:
raise Exception("Unknown message type")
raise Exception("Unknown message type " + str(msg[0]))
except Exception as e:
logger.error("Error parsing event: " + str(e))

Expand Down Expand Up @@ -443,8 +468,8 @@ async def _connect_to_relay(self):
self.connected = False
if not self._is_shutting_down():
# Wait some time before reconnecting
logger.debug("Reconnecting to NWC relay in 5 seconds...")
await asyncio.sleep(5)
logger.debug("Reconnecting to NWC relay...")
await self._ratelimit("connecting")

def _encrypt_content(
self, content: str, pubkey_hex: str, iv_seed: Optional[int] = None
Expand Down

0 comments on commit 81224f6

Please sign in to comment.