Skip to content

Commit 597d9c2

Browse files
committed
feat (core): 支持 timeout
1 parent e098681 commit 597d9c2

6 files changed

Lines changed: 32 additions & 4 deletions

File tree

src-tauri/src/execution.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub async fn execute_code(
205205

206206
let mut stdout_lines = Vec::new();
207207
let mut stderr_lines = Vec::new();
208-
let timeout = std::time::Duration::from_secs(30);
208+
let timeout = std::time::Duration::from_secs(plugin.get_timeout());
209209

210210
// 主执行循环
211211
loop {
@@ -257,8 +257,12 @@ pub async fn execute_code(
257257
}),
258258
);
259259

260-
error!("执行代码 -> 超时,终止语言 [ {} ] 的执行", request.language);
261-
return Err("代码执行超时(30秒)".to_string());
260+
error!(
261+
"执行代码 -> 超时 ({} 秒),终止语言 [ {} ] 的执行",
262+
plugin.get_timeout(),
263+
request.language
264+
);
265+
return Err(format!("代码执行超时({} 秒)", plugin.get_timeout()));
262266
}
263267

264268
// 读取并发送 stdout

src-tauri/src/plugins/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub struct PluginConfig {
3939
pub after_compile: Option<String>, // 插件在编译完成后执行的命令
4040
pub run_command: Option<String>, // 插件执行的命令,例如 "python2 $filename"
4141
pub template: Option<String>, // 插件的模板
42+
pub timeout: Option<u64>, // 插件的超时时间
4243
}
4344

4445
// 语言插件接口
@@ -67,6 +68,13 @@ pub trait LanguagePlugin: Send + Sync {
6768
.map(PathBuf::from)
6869
}
6970

71+
// 获取超时时间
72+
fn get_timeout(&self) -> u64 {
73+
self.get_config()
74+
.map(|config| config.timeout.unwrap_or(30))
75+
.unwrap_or(30)
76+
}
77+
7078
// 获取插件支持的命令
7179
fn get_command(&self, file_path: Option<&str>) -> String {
7280
if let Some(config) = self.get_config() {

src-tauri/src/plugins/python2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl LanguagePlugin for Python2Plugin {
3434
run_command: Option::from(String::from("python2 $filename")),
3535
after_compile: None,
3636
template: None,
37+
timeout: Some(30),
3738
}
3839
}
3940

src-tauri/src/plugins/python3.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ impl LanguagePlugin for Python3Plugin {
3333
run_command: Option::from(String::from("python3 $filename")),
3434
after_compile: None,
3535
template: None,
36+
timeout: Some(30),
3637
}
3738
}
3839

src/components/setting/Language.vue

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@
7979
</div>
8080
</div>
8181

82+
<div>
83+
<label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
84+
超时时间(秒)
85+
</label>
86+
<div class="flex gap-2">
87+
<input v-model="pluginConfig.timeout"
88+
type="number"
89+
placeholder="超时时间(秒),默认 30 秒"
90+
class="flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:outline-none focus:ring-1 focus:ring-blue-500 focus:border-transparent text-sm"/>
91+
</div>
92+
</div>
93+
8294
<div>
8395
<label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
8496
文件模板
@@ -125,7 +137,8 @@ const pluginConfig = ref<PluginConfig>({
125137
before_compile: '',
126138
after_compile: '',
127139
run_command: '',
128-
template: ''
140+
template: '',
141+
timeout: 30
129142
})
130143
131144
const getSupportedLanguages = async () => {

src/types/plugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export default interface PluginConfig
88
after_compile?: string // 插件在编译完成后执行的命令
99
run_command?: string // 插件执行的命令,例如 "python2 $filename"
1010
template?: string // 插件的模板
11+
timeout?: number // 插件的超时时间
1112
}

0 commit comments

Comments
 (0)