style: introduce ruff as linter and formatter (#1356)

* style: remove necessary imports

* style: fix F841

* style: fix F401

* style: fix F811

* style: fix E402

* style: fix E721

* style: fix E722

* style: fix E722

* style: fix F541

* style: ruff format

* style: all passed

* style: add ruff in deps

* style: more ignores in ruff.toml

* style: add pre-commit
This commit is contained in:
Junyan Qin (Chin)
2025-04-29 17:24:07 +08:00
committed by GitHub
parent 09e70d70e9
commit 209f16af76
240 changed files with 5307 additions and 4689 deletions
+18 -9
View File
@@ -7,19 +7,19 @@ from ...core import app, entities as core_entities
preregistered_algos: list[typing.Type[ReteLimitAlgo]] = []
def algo_class(name: str):
def decorator(cls: typing.Type[ReteLimitAlgo]) -> typing.Type[ReteLimitAlgo]:
cls.name = name
preregistered_algos.append(cls)
return cls
return decorator
class ReteLimitAlgo(metaclass=abc.ABCMeta):
"""限流算法抽象类"""
name: str = None
ap: app.Application
@@ -31,11 +31,16 @@ class ReteLimitAlgo(metaclass=abc.ABCMeta):
pass
@abc.abstractmethod
async def require_access(self, query: core_entities.Query, launcher_type: str, launcher_id: typing.Union[int, str]) -> bool:
async def require_access(
self,
query: core_entities.Query,
launcher_type: str,
launcher_id: typing.Union[int, str],
) -> bool:
"""进入处理流程
这个方法对等待是友好的,意味着算法可以实现在这里等待一段时间以控制速率。
Args:
launcher_type (str): 请求者类型 群聊为 group 私聊为 person
launcher_id (int): 请求者ID
@@ -44,15 +49,19 @@ class ReteLimitAlgo(metaclass=abc.ABCMeta):
bool: 是否允许进入处理流程,若返回false,则直接丢弃该请求
"""
raise NotImplementedError
@abc.abstractmethod
async def release_access(self, query: core_entities.Query, launcher_type: str, launcher_id: typing.Union[int, str]):
async def release_access(
self,
query: core_entities.Query,
launcher_type: str,
launcher_id: typing.Union[int, str],
):
"""退出处理流程
Args:
launcher_type (str): 请求者类型 群聊为 group 私聊为 person
launcher_id (int): 请求者ID
"""
raise NotImplementedError
+20 -10
View File
@@ -5,9 +5,9 @@ import typing
from .. import algo
from ....core import entities as core_entities
# 固定窗口算法
class SessionContainer:
wait_lock: asyncio.Lock
records: dict[int, int]
@@ -18,9 +18,8 @@ class SessionContainer:
self.records = {}
@algo.algo_class("fixwin")
@algo.algo_class('fixwin')
class FixedWindowAlgo(algo.ReteLimitAlgo):
containers_lock: asyncio.Lock
"""访问记录容器锁"""
@@ -31,7 +30,12 @@ class FixedWindowAlgo(algo.ReteLimitAlgo):
self.containers_lock = asyncio.Lock()
self.containers = {}
async def require_access(self, query: core_entities.Query, launcher_type: str, launcher_id: typing.Union[int, str]) -> bool:
async def require_access(
self,
query: core_entities.Query,
launcher_type: str,
launcher_id: typing.Union[int, str],
) -> bool:
# 加锁,找容器
container: SessionContainer = None
@@ -46,7 +50,6 @@ class FixedWindowAlgo(algo.ReteLimitAlgo):
# 等待锁
async with container.wait_lock:
# 获取窗口大小和限制
window_size = query.pipeline_config['safety']['rate-limit']['window-length']
limitation = query.pipeline_config['safety']['rate-limit']['limitation']
@@ -69,13 +72,15 @@ class FixedWindowAlgo(algo.ReteLimitAlgo):
if count >= limitation:
if query.pipeline_config['safety']['rate-limit']['strategy'] == 'drop':
return False
elif query.pipeline_config['safety']['rate-limit']['strategy'] == 'wait':
elif (
query.pipeline_config['safety']['rate-limit']['strategy'] == 'wait'
):
# 等待下一窗口
await asyncio.sleep(window_size - time.time() % window_size)
now = int(time.time())
now = now - now % window_size
if now not in container.records:
container.records = {}
container.records[now] = 1
@@ -85,6 +90,11 @@ class FixedWindowAlgo(algo.ReteLimitAlgo):
# 返回True
return True
async def release_access(self, query: core_entities.Query, launcher_type: str, launcher_id: typing.Union[int, str]):
async def release_access(
self,
query: core_entities.Query,
launcher_type: str,
launcher_id: typing.Union[int, str],
):
pass
+13 -11
View File
@@ -4,22 +4,25 @@ import typing
from .. import entities, stage
from . import algo
from .algos import fixedwin
from ...core import entities as core_entities
from ...utils import importutil
from . import algos
importutil.import_modules_in_pkg(algos)
@stage.stage_class("RequireRateLimitOccupancy")
@stage.stage_class("ReleaseRateLimitOccupancy")
@stage.stage_class('RequireRateLimitOccupancy')
@stage.stage_class('ReleaseRateLimitOccupancy')
class RateLimit(stage.PipelineStage):
"""限速器控制阶段
不改写query,只检查是否需要限速。
"""
algo: algo.ReteLimitAlgo
async def initialize(self, pipeline_config: dict):
algo_name = 'fixwin'
algo_class = None
@@ -42,9 +45,8 @@ class RateLimit(stage.PipelineStage):
entities.StageProcessResult,
typing.AsyncGenerator[entities.StageProcessResult, None],
]:
"""处理
"""
if stage_inst_name == "RequireRateLimitOccupancy":
"""处理"""
if stage_inst_name == 'RequireRateLimitOccupancy':
if await self.algo.require_access(
query,
query.launcher_type.value,
@@ -58,10 +60,10 @@ class RateLimit(stage.PipelineStage):
return entities.StageProcessResult(
result_type=entities.ResultType.INTERRUPT,
new_query=query,
console_notice=f"根据限速规则忽略 {query.launcher_type.value}:{query.launcher_id} 消息",
user_notice=f"请求数超过限速器设定值,已丢弃本消息。"
console_notice=f'根据限速规则忽略 {query.launcher_type.value}:{query.launcher_id} 消息',
user_notice='请求数超过限速器设定值,已丢弃本消息。',
)
elif stage_inst_name == "ReleaseRateLimitOccupancy":
elif stage_inst_name == 'ReleaseRateLimitOccupancy':
await self.algo.release_access(
query,
query.launcher_type.value,