Skip to content

Commit 60111b7

Browse files
committed
Ratelimiter works properly now
1 parent 246cd1a commit 60111b7

1 file changed

Lines changed: 13 additions & 20 deletions

File tree

plugins/core_sieve.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,31 @@
11
import asyncio
22
import logging
3+
import functools
34

45
from time import time
56

67
from cloudbot import hook
78
from cloudbot.util.tokenbucket import TokenBucket
89

9-
inited = []
10-
11-
# when STRICT is enabled, every time a user gets ratelimted it wipes
12-
# their tokens so they have to wait at least X seconds to regen
13-
14-
buckets = {}
15-
1610
logger = logging.getLogger("cloudbot")
1711

1812

19-
def task_clear(loop):
20-
for uid, _bucket in buckets:
13+
def task_clear(loop, buckets):
14+
for uid, _bucket in buckets.items():
2115
if (time() - _bucket.timestamp) > 600:
2216
del buckets[uid]
23-
loop.call_later(600, task_clear, loop)
17+
_func = functools.partial(task_clear, loop, buckets)
18+
loop.call_later(600, _func)
2419

2520

2621
@asyncio.coroutine
27-
@hook.irc_raw('004')
28-
def init_tasks(loop, conn):
29-
global inited
30-
if conn.name in inited:
31-
# tasks already started
32-
return
22+
@hook.on_start
23+
def init_tasks(loop, bot):
24+
bot.memory["buckets"] = {}
25+
logger.info("[sieve] Bot is starting ratelimiter cleanup task.")
26+
_func = functools.partial(task_clear, loop, bot.memory["buckets"])
27+
loop.call_later(600, _func)
3328

34-
logger.info("[{}|sieve] Bot is starting ratelimiter cleanup task.".format(conn.name))
35-
loop.call_later(600, task_clear, loop)
36-
inited.append(conn.name)
3729

3830

3931
@asyncio.coroutine
@@ -84,7 +76,8 @@ def sieve_suite(bot, event, _hook):
8476
# check command spam tokens
8577
if _hook.type == "command":
8678
# right now ratelimiting is per-channel, but this can be changed
87-
uid = (event.chan, event.nick.lower())
79+
uid = "/".join(event.conn.name, event.chan, event.nick.lower())
80+
buckets = bot.memory["buckets"]
8881

8982
tokens = conn.config.get('ratelimit', {}).get('tokens', 17.5)
9083
restore_rate = conn.config.get('ratelimit', {}).get('restore_rate', 2.5)

0 commit comments

Comments
 (0)