Skip to content

Commit e37ae6e

Browse files
DefectingCatclaude
andcommitted
refactor(lua): replace std::sync::Mutex with parking_lot::Mutex
Replace std::sync::Mutex with parking_lot::Mutex throughout Lua modules to simplify lock handling. parking_lot::Mutex never poisons, eliminating the need for Result handling on lock() calls. Changes: - Add parking_lot dependency to Cargo.toml - Update handler.rs, structures.rs, and userdata.rs to use parking_lot::Mutex - Remove all .map_err() and .unwrap() calls on mutex locks - Simplify get_method implementation by removing panic recovery logic - Update tests to use the new Mutex API Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8c63974 commit e37ae6e

5 files changed

Lines changed: 56 additions & 111 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ crc32fast = "1.4.2"
5555
hmac = "0.12.1"
5656
sha1 = "0.10.6"
5757
# core
58+
parking_lot = "0.12"
5859
tokio = { version = "1.49.0", features = [
5960
"rt-multi-thread",
6061
"time",

src/http/lua/handler.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::hash_map::DefaultHasher;
22
use std::hash::{Hash, Hasher};
3-
use std::sync::{Arc, Mutex};
3+
use std::sync::Arc;
44
use std::time::{SystemTime, UNIX_EPOCH};
55

66
use anyhow::{Context, anyhow};
@@ -11,6 +11,7 @@ use axum::{
1111
};
1212
use http::Uri;
1313
use http::header::HeaderMap;
14+
use parking_lot::Mutex;
1415
use tokio::fs;
1516
use tracing::{debug, error, info};
1617

@@ -123,10 +124,9 @@ fn build_response(res: CandyResponse, output_buffer: String) -> RouteResult<Resp
123124

124125
// 添加响应头
125126
let headers = response.headers_mut().unwrap();
126-
if let Ok(guard) = res.headers.headers.lock() {
127-
for (name, value) in guard.iter() {
128-
headers.append(name.clone(), value.clone());
129-
}
127+
let guard = res.headers.headers.lock();
128+
for (name, value) in guard.iter() {
129+
headers.append(name.clone(), value.clone());
130130
}
131131

132132
response
@@ -360,10 +360,7 @@ pub async fn lua(
360360

361361
// 检查请求状态中的输出缓冲区(来自 print 调用)
362362
let output_buffer = {
363-
let state = ctx
364-
.req_state
365-
.lock()
366-
.map_err(|e| RouteError::Any(anyhow!("Failed to acquire request state lock: {}", e)))?;
363+
let state = ctx.req_state.lock();
367364
state.output_buffer.clone()
368365
};
369366

src/http/lua/structures.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use std::sync::{Arc, Mutex};
1+
use std::sync::Arc;
22

33
use http::{HeaderMap, Uri};
4+
use parking_lot::Mutex;
45

56
/// 为 Lua 脚本提供 HTTP 请求上下文
67
#[derive(Clone, Debug)]
@@ -89,10 +90,7 @@ impl CandyHeaders {
8990

9091
/// 获取所有 headers 作为 Lua table
9192
pub fn get_headers_table(&self, lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
92-
let headers = self
93-
.headers
94-
.lock()
95-
.map_err(|e| mlua::Error::external(anyhow::anyhow!("Failed to lock headers: {}", e)))?;
93+
let headers = self.headers.lock();
9694

9795
let table = lua.create_table()?;
9896
for (name, value) in headers.iter() {
@@ -231,7 +229,8 @@ mod tests {
231229
fn test_new() {
232230
let headers = HeaderMap::new();
233231
let candy_headers = CandyHeaders::new(headers);
234-
assert!(candy_headers.headers.lock().is_ok());
232+
// parking_lot Mutex::lock() 总是成功,返回 MutexGuard
233+
let _guard = candy_headers.headers.lock();
235234
}
236235

237236
#[test]
@@ -295,7 +294,7 @@ mod tests {
295294
);
296295

297296
let candy_headers = CandyHeaders::new(headers);
298-
let guard = candy_headers.headers.lock().unwrap();
297+
let guard = candy_headers.headers.lock();
299298

300299
assert_eq!(
301300
guard.get(http::header::CONTENT_TYPE).unwrap(),
@@ -337,7 +336,7 @@ mod tests {
337336
body: Arc::new(Mutex::new(Some(body_data.to_vec()))),
338337
};
339338

340-
let guard = request.body.lock().unwrap();
339+
let guard = request.body.lock();
341340
assert_eq!(guard.as_ref().unwrap(), body_data);
342341
}
343342

@@ -351,7 +350,7 @@ mod tests {
351350
body: Arc::new(Mutex::new(None)),
352351
};
353352

354-
let guard = request.body.lock().unwrap();
353+
let guard = request.body.lock();
355354
assert!(guard.is_none());
356355
}
357356
}
@@ -484,7 +483,6 @@ mod tests {
484483
let headers = Arc::new(Mutex::new(HeaderMap::new()));
485484
headers
486485
.lock()
487-
.unwrap()
488486
.insert(http::header::HOST, HeaderValue::from_static("localhost"));
489487

490488
let state = CandyReqState {
@@ -503,7 +501,7 @@ mod tests {
503501
assert_eq!(state.uri_path, "/original");
504502

505503
// Verify header access
506-
let guard = state.headers.lock().unwrap();
504+
let guard = state.headers.lock();
507505
assert_eq!(guard.get(http::header::HOST).unwrap(), "localhost");
508506
}
509507

0 commit comments

Comments
 (0)