skills/rust-async-concurrency/SKILL.md
Rust 非同步與並行模式。用於 tokio runtime、async/await、Send+Sync、共享狀態、graceful shutdown、channel 模式。
npx skillsauth add vincent119/ai-rules-kit rust-async-concurrencyInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// 或手動建立 runtime
let rt = tokio::runtime::Builder::new_multi_thread()
.worker_threads(4)
.enable_all()
.build()?;
rt.block_on(async { run().await })
}
use std::sync::Arc;
use tokio::sync::RwLock;
#[derive(Debug, Clone)]
pub struct AppState {
db: Arc<DatabasePool>,
cache: Arc<RwLock<HashMap<String, String>>>,
}
// 讀取(多個 reader 並行)
async fn get_cached(state: &AppState, key: &str) -> Option<String> {
state.cache.read().await.get(key).cloned()
}
// 寫入(獨佔)
async fn set_cached(state: &AppState, key: String, value: String) {
state.cache.write().await.insert(key, value);
}
use tokio::signal;
async fn shutdown_signal() {
let ctrl_c = async {
signal::ctrl_c().await.expect("failed to listen for ctrl+c");
};
#[cfg(unix)]
let terminate = async {
signal::unix::signal(signal::unix::SignalKind::terminate())
.expect("failed to listen for SIGTERM")
.recv()
.await;
};
#[cfg(not(unix))]
let terminate = std::future::pending::<()>();
tokio::select! {
_ = ctrl_c => {},
_ = terminate => {},
}
}
// 搭配 axum
async fn run() -> anyhow::Result<()> {
let app = Router::new().route("/", get(|| async { "ok" }));
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await?;
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal())
.await?;
Ok(())
}
use tokio::sync::mpsc;
async fn producer_consumer() {
let (tx, mut rx) = mpsc::channel::<String>(32);
// Producer
tokio::spawn(async move {
for i in 0..10 {
tx.send(format!("msg-{i}")).await.unwrap();
}
});
// Consumer
while let Some(msg) = rx.recv().await {
println!("received: {msg}");
}
}
.await 持有的資料必須 SendRc、RefCell(非 Send)Arc<Mutex<T>> 或 tokio::sync::Mutex 替代dyn Trait 時加 Send + Sync bound// 正確
pub struct Service {
handler: Box<dyn Handler + Send + Sync>,
}
// 錯誤:不能跨 await
async fn bad() {
let rc = std::rc::Rc::new(42);
tokio::time::sleep(Duration::from_secs(1)).await; // rc 不是 Send
println!("{rc}");
}
// 長時間 CPU 密集任務需加 yield
async fn process_large_batch(items: Vec<Item>) {
for chunk in items.chunks(100) {
process_chunk(chunk).await;
tokio::task::yield_now().await; // 讓出執行權
}
}
tools
基於 SLA/SLO 量化評估事故影響的計算模型與業務影響矩陣。適用於「SLA 影響」、「SLO 違反」、「影響評估」、「營收損失估算」、「Error Budget」、「可用性計算」、「事故成本評估」等量化事故業務影響的任務。強化 impact-assessor 的評估能力。注意:事故原因分析與改善規劃不在此技能範圍內。
research
根因分析(RCA)方法論詳細指南。提供 5 Whys、Fishbone 圖、Fault Tree Analysis、變更分析等結構化 RCA 技術,以及認知偏誤防範清單。適用於「根因分析」、「RCA」、「5 Whys」、「魚骨圖」、「Fault Tree」、「原因分析方法論」、「變更分析」等事故原因分析任務。強化 root-cause-investigator 的分析能力。注意:時間軸重建與改善規劃不在此技能範圍內。
testing
事故事後分析(Postmortem)完整流程。協調 7 個執行階段:資訊收集 → 時間軸重建 → 根因分析 → 影響評估 → 改善規劃 → 報告審查 → 整合報告,最終產出完整的 Postmortem 報告。適用於「寫事故報告」、「post-incident 分析」、「RCA 報告」、「事故時間軸整理」、「建立改善措施」等請求。注意:即時 Incident Response(on-call)、監控系統設定、告警配置不在此技能範圍內。
content-media
投影片版面模式庫。提供 20 種投影片類型的最佳版面配置、格線系統、色彩與字型設計 Token。適用於「投影片版面」、「Slide Layout」、「設計系統」、「格線」、「字型」、「色彩規範」等投影片視覺設計任務。強化 visual-designer 的設計能力。注意:PPT/Keynote 檔案直接輸出不在此技能範圍內。