txlyre hace 2 años
padre
commit
5d3c049351
Se han modificado 1 ficheros con 7 adiciones y 3 borrados
  1. 7 3
      byafn.py

+ 7 - 3
byafn.py

@@ -190,6 +190,7 @@ class Peer:
     self.receive_lock = asyncio.Lock()
     self.ticks = 0
     self.last_message_in_ts = -1
+    self.last_message_out_ts = -1
 
 async def write(peer, data):
   peer.writer.write(data)
@@ -205,7 +206,11 @@ async def read(peer, size):
 
 async def cooldown(peer, out=True):
   if out:
-    await asyncio.sleep(RATELIMIT)
+    diff = time.time() - peer.last_message_out_ts
+    if diff < RATELIMIT:
+      await asyncio.sleep(RATELIMIT - diff)
+
+    peer.last_message_out_ts = time.time()
   else:
     if time.time() - peer.last_message_in_ts < RATELIMIT:
       raise Error(f'rate limit (={RATELIMIT}s.) exceeded for incoming messages')
@@ -213,8 +218,6 @@ async def cooldown(peer, out=True):
     peer.last_message_in_ts = time.time()
 
 async def send(peer, message):
-  await cooldown(peer)
-
   if type(message) is ServiceMessage:
     buffer = bytes([message.kind])
 
@@ -252,6 +255,7 @@ async def send(peer, message):
   )
 
   async with peer.send_lock:
+    await cooldown(peer)
     await write(peer, buffer)
 
     if chunks_count: