Skip to content

Commit b5866f2

Browse files
committed
Sometimes the simple way is best
1 parent 6ab9486 commit b5866f2

1 file changed

Lines changed: 16 additions & 15 deletions

File tree

plugins/core_sieve.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
import asyncio
22
import logging
3-
import functools
43

54
from time import time
65

76
from cloudbot import hook
87
from cloudbot.util.tokenbucket import TokenBucket
98

9+
ready = False
10+
buckets = {}
1011
logger = logging.getLogger("cloudbot")
1112

1213

13-
def task_clear(loop, buckets):
14-
for uid, _bucket in buckets.items():
14+
def task_clear(loop):
15+
for uid, _bucket in buckets.copy():
1516
if (time() - _bucket.timestamp) > 600:
1617
del buckets[uid]
17-
_func = functools.partial(task_clear, loop, buckets)
18-
loop.call_later(600, _func)
18+
loop.call_later(600, task_clear, loop)
1919

2020

2121
@asyncio.coroutine
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)
22+
@hook.irc_raw('004')
23+
def init_tasks(loop, conn):
24+
global ready
25+
if ready:
26+
# tasks already started
27+
return
28+
29+
logger.info("[{}|sieve] Bot is starting ratelimiter cleanup task.".format(conn.name))
30+
loop.call_later(600, task_clear, loop)
31+
ready = True
2832

2933

3034
@asyncio.coroutine
@@ -75,8 +79,7 @@ def sieve_suite(bot, event, _hook):
7579
# check command spam tokens
7680
if _hook.type == "command":
7781
# right now ratelimiting is per-channel, but this can be changed
78-
uid = "/".join([event.conn.name, event.chan, event.nick.lower()])
79-
buckets = bot.memory["buckets"]
82+
uid = (event.chan, event.nick.lower())
8083

8184
tokens = conn.config.get('ratelimit', {}).get('tokens', 17.5)
8285
restore_rate = conn.config.get('ratelimit', {}).get('restore_rate', 2.5)
@@ -102,5 +105,3 @@ def sieve_suite(bot, event, _hook):
102105
return None
103106

104107
return event
105-
106-

0 commit comments

Comments
 (0)