Skip to content

Commit 22d180f

Browse files
committed
Fix series increaser execution
1 parent 568e792 commit 22d180f

5 files changed

Lines changed: 40 additions & 14 deletions

File tree

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,19 @@ async def _():
121121
await test.finish("pass")
122122
```
123123

124+
串联多个限制器。
125+
```python
126+
cmd = on_startswith("cmd")
127+
@cmd.handle(parameterless=[
128+
Cooldown(UserScope(), 2, limit = 2, reject = "UserScope reject"),
129+
Cooldown(GlobalScope(), 3600, limit = 4, reject = "GlobalScope reject")
130+
])
131+
async def _(): ...
132+
```
133+
124134
多命令共享使用统计集合。
125-
注:请确保使用相同使用统计集合的限制器限流参数一致(限制对象,限制时间,最大使用量),否则可能会有预期之外的行为。
135+
> [!IMPORTANT]
136+
> 请确保使用相同使用统计集合的限制器限流参数一致(限制对象,限制时间,最大使用量),否则可能会有预期之外的行为。
126137
```python
127138
# 注意,不同限流算法下的使用统计集合无法共享
128139
cmd1 = on_startswith("cmd1")
@@ -154,7 +165,7 @@ async def _(increaser: Increaser):
154165
await cmd.finish("Run successed")
155166
```
156167

157-
使用固定窗口策略实现每日签到
168+
使用固定窗口策略实现每日签到
158169
```python
159170
dailysign = on_startswith("签到")
160171
@dailysign.handle(parameterless = [

nonebot_plugin_limiter/cooldown.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from collections import deque
2+
from collections.abc import Callable
23
from dataclasses import dataclass, field
34
from datetime import datetime, timedelta
5+
from functools import partial
46
from typing import cast
57

68
from apscheduler.triggers.base import BaseTrigger
@@ -33,6 +35,11 @@ def _limit_dep_wrapper(limit: int | _DependentCallable[int]) -> _DependentCallab
3335
limit_dep = limit
3436
return limit_dep
3537

38+
def inject_increaser(state: T_State, func: Callable):
39+
executors = state.setdefault("plugin_limiter:increaser", [])
40+
assert isinstance(executors, list)
41+
executors.append(func)
42+
3643
# region: FixWindow
3744
@dataclass
3845
class FixWindowUsage:
@@ -139,21 +146,27 @@ async def _limiter_dependency(
139146
bucket[entity_id] = FixWindowUsage(now, limit)
140147
usage = bucket[entity_id]
141148

142-
if usage.available > 0:
149+
def _increase_action(reset: bool = True):
150+
if reset:
151+
usage.start_time = now
152+
usage.available = limit
143153
usage.available -= 1
154+
155+
if usage.available > 0:
156+
if set_increaser:
157+
inject_increaser(state, partial(_increase_action, False))
158+
else:
159+
_increase_action(False)
144160
return
145161

146162
# Calculate reset time based on when the limitation was set
147163
reset_time = trigger.get_next_fire_time(usage.start_time, now)
148164
assert reset_time is not None, "reset_time should not be None"
149165

150-
def _increase_action():
151-
usage.start_time = now
152-
usage.available = limit - 1
153-
166+
# Reset
154167
if now >= reset_time:
155168
if set_increaser:
156-
state["plugin_limiter:increaser"] = _increase_action
169+
inject_increaser(state, _increase_action)
157170
else:
158171
_increase_action()
159172
return # Didn't exceed
@@ -273,7 +286,7 @@ def _increase_action():
273286

274287
if len(usage.timestamps) < limit:
275288
if set_increaser:
276-
state["plugin_limiter:increaser"] = _increase_action
289+
inject_increaser(state, _increase_action)
277290
else:
278291
_increase_action()
279292
return # Didn't exceed

nonebot_plugin_limiter/handler.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77

88
class _FuncWrapper:
9-
def __init__(self, func: Callable) -> None:
10-
self._func = func
9+
def __init__(self, funcs: list[Callable]) -> None:
10+
self._funcs = funcs
1111

1212
def execute(self):
13-
self._func()
13+
for func in self._funcs:
14+
func()
1415

1516
def get_increaser(state: T_State):
17+
print(state)
1618
ret = state.get("plugin_limiter:increaser")
1719
if ret is None:
1820
raise KeyError("Cannot get increaser, make sure you have enabled `set_increaser` in cooldown policy.")

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "nonebot-plugin-limiter"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
description = "通用消息冷却限制器"
55
readme = "README.md"
66
authors = [{ name = "MiddleRed", email = "middlered@outlook.com" }]

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)