|
1 | 1 | import asyncio |
2 | 2 | import logging |
| 3 | +import functools |
3 | 4 |
|
4 | 5 | from time import time |
5 | 6 |
|
6 | 7 | from cloudbot import hook |
7 | 8 | from cloudbot.util.tokenbucket import TokenBucket |
8 | 9 |
|
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 | | - |
16 | 10 | logger = logging.getLogger("cloudbot") |
17 | 11 |
|
18 | 12 |
|
19 | | -def task_clear(loop): |
20 | | - for uid, _bucket in buckets: |
| 13 | +def task_clear(loop, buckets): |
| 14 | + for uid, _bucket in buckets.items(): |
21 | 15 | if (time() - _bucket.timestamp) > 600: |
22 | 16 | 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) |
24 | 19 |
|
25 | 20 |
|
26 | 21 | @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) |
33 | 28 |
|
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) |
37 | 29 |
|
38 | 30 |
|
39 | 31 | @asyncio.coroutine |
@@ -84,7 +76,8 @@ def sieve_suite(bot, event, _hook): |
84 | 76 | # check command spam tokens |
85 | 77 | if _hook.type == "command": |
86 | 78 | # 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"] |
88 | 81 |
|
89 | 82 | tokens = conn.config.get('ratelimit', {}).get('tokens', 17.5) |
90 | 83 | restore_rate = conn.config.get('ratelimit', {}).get('restore_rate', 2.5) |
|
0 commit comments