feat(code-runner): admin 跳过代码运行速率限制

作者在 /admin/runner 沙箱调试代码块时,1 次/秒 + 50 次/天的读者限流
(RATE_LIMIT_CODE_EXEC_*)会频繁打断试运行。已登录 admin 改为跳过
check_code_exec_limit,仍受并发槽、资源钳制与源码大小校验约束。
This commit is contained in:
xfy 2026-07-05 14:32:56 +08:00
parent 575b6b533e
commit c83b834c8b

View File

@ -21,6 +21,8 @@ use crate::api::code_runner::{ExecRequest, ExecResult, ExecStatus, ExecTask};
// server-only 辅助模块与依赖:仅在 server function body被宏剥离到 WASM 之外)内使用。
#[cfg(feature = "server")]
use crate::api::auth::get_current_admin_user;
#[cfg(feature = "server")]
use crate::api::code_runner::languages::{is_supported_lang, LANGUAGES};
#[cfg(feature = "server")]
use crate::api::code_runner::progress::{
@ -60,12 +62,20 @@ fn client_ip() -> String {
///
/// 同步校验通过后立即返回 task_id容器在后台执行结果通过
/// [`get_exec_result`] 轮询查询。语言不支持 / 源码过大 / 触发限流时同步返回错误。
///
/// admin 角色跳过速率限制(便于作者在沙箱调试),但仍受并发槽、
/// 资源钳制与源码大小校验约束。
#[server(StartExec, "/api")]
pub async fn start_exec(req: ExecRequest) -> Result<String, ServerFnError> {
// 1. 速率限制(双层:每秒突发 + 每日总额)
let ip = client_ip();
if let Err(msg) = check_code_exec_limit(&ip) {
return Err(ServerFnError::new(msg));
// admin 放行:作者在 /admin/runner 沙箱试运行时不应被限流打断;
// 仍受并发槽、资源钳制、源码大小校验约束。
let is_admin = get_current_admin_user().await.is_ok();
if !is_admin {
let ip = client_ip();
if let Err(msg) = check_code_exec_limit(&ip) {
return Err(ServerFnError::new(msg));
}
}
// 2. 语言白名单