[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"article-deep-evm-24-yibu-rust-shangxiawen-chuanbo-jiezhishijian":3},{"article":4,"author":51},{"id":5,"category_id":6,"title":7,"slug":8,"excerpt":9,"content_md":10,"content_html":11,"locale":12,"author_id":13,"published":14,"published_at":15,"meta_title":16,"meta_description":17,"focus_keyword":18,"og_image":19,"canonical_url":19,"robots_meta":20,"created_at":15,"updated_at":15,"tags":21,"category_name":31,"related_articles":32},"d3000000-0000-0000-0000-000000000124","a0000000-0000-0000-0000-000000000036","Deep EVM #24：异步Rust中的上下文传播——截止时间、取消和追踪","deep-evm-24-yibu-rust-shangxiawen-chuanbo-jiezhishijian","异步Rust中的上下文传播：截止时间传播、优雅取消和跨异步边界的分布式追踪。","## 上下文传播的问题\n\n在异步Rust系统中，操作跨越多个异步边界和任务。如何传播截止时间、取消信号和追踪上下文？\n\n## 截止时间传播\n\n使用tokio的timeout确保操作在截止时间内完成：\n\n```rust\nuse tokio::time::{timeout, Duration};\n\nasync fn simulate_with_deadline(\n    path: &ArbPath,\n    deadline: Duration,\n) -> Result\u003CSimResult> {\n    timeout(deadline, async {\n        \u002F\u002F 模拟逻辑\n        simulate(path).await\n    }).await?\n}\n```\n\n## 取消\n\ntokio的CancellationToken提供协作式取消：\n\n```rust\nuse tokio_util::sync::CancellationToken;\n\nlet token = CancellationToken::new();\nlet child_token = token.child_token();\n\ntokio::spawn(async move {\n    tokio::select! {\n        result = do_work() => handle_result(result),\n        _ = child_token.cancelled() => { \u002F* 清理 *\u002F },\n    }\n});\n\n\u002F\u002F 取消所有子任务\ntoken.cancel();\n```\n\n## 分布式追踪\n\n使用tracing crate进行结构化日志和追踪：\n\n```rust\n#[tracing::instrument(skip(db))]\nasync fn process_opportunity(\n    db: &CacheDB,\n    path: &ArbPath,\n) -> Result\u003C()> {\n    tracing::info!(\"Processing arbitrage path\");\n    \u002F\u002F ...\n}\n```\n\n## 总结\n\n上下文传播是构建可靠异步Rust系统的关键。截止时间、取消令牌和分布式追踪确保系统在高负载下仍然可控和可观测。","\u003Ch2 id=\"\">上下文传播的问题\u003C\u002Fh2>\n\u003Cp>在异步Rust系统中，操作跨越多个异步边界和任务。如何传播截止时间、取消信号和追踪上下文？\u003C\u002Fp>\n\u003Ch2 id=\"\">截止时间传播\u003C\u002Fh2>\n\u003Cp>使用tokio的timeout确保操作在截止时间内完成：\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-rust\">use tokio::time::{timeout, Duration};\n\nasync fn simulate_with_deadline(\n    path: &amp;ArbPath,\n    deadline: Duration,\n) -&gt; Result&lt;SimResult&gt; {\n    timeout(deadline, async {\n        \u002F\u002F 模拟逻辑\n        simulate(path).await\n    }).await?\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch2 id=\"\">取消\u003C\u002Fh2>\n\u003Cp>tokio的CancellationToken提供协作式取消：\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-rust\">use tokio_util::sync::CancellationToken;\n\nlet token = CancellationToken::new();\nlet child_token = token.child_token();\n\ntokio::spawn(async move {\n    tokio::select! {\n        result = do_work() =&gt; handle_result(result),\n        _ = child_token.cancelled() =&gt; { \u002F* 清理 *\u002F },\n    }\n});\n\n\u002F\u002F 取消所有子任务\ntoken.cancel();\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch2 id=\"\">分布式追踪\u003C\u002Fh2>\n\u003Cp>使用tracing crate进行结构化日志和追踪：\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-rust\">#[tracing::instrument(skip(db))]\nasync fn process_opportunity(\n    db: &amp;CacheDB,\n    path: &amp;ArbPath,\n) -&gt; Result&lt;()&gt; {\n    tracing::info!(\"Processing arbitrage path\");\n    \u002F\u002F ...\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch2 id=\"\">总结\u003C\u002Fh2>\n\u003Cp>上下文传播是构建可靠异步Rust系统的关键。截止时间、取消令牌和分布式追踪确保系统在高负载下仍然可控和可观测。\u003C\u002Fp>\n","zh","b0000000-0000-0000-0000-000000000001",true,"2026-03-28T10:44:26.312523Z","异步Rust中的上下文传播——截止时间、取消和追踪","异步Rust上下文传播：截止时间、tokio CancellationToken取消和结构化追踪。","上下文传播 异步rust",null,"index, follow",[22,27],{"id":23,"name":24,"slug":25,"created_at":26},"c0000000-0000-0000-0000-000000000022","Performance","performance","2026-03-28T10:44:21.513630Z",{"id":28,"name":29,"slug":30,"created_at":26},"c0000000-0000-0000-0000-000000000001","Rust","rust","工程",[33,39,45],{"id":34,"title":35,"slug":36,"excerpt":37,"locale":12,"category_name":31,"published_at":38},"d0000000-0000-0000-0000-000000000668","为什么Bali在2026年正在成为东南亚的影响力科技中心","weishenme-bali-2026-zhengzai-chengwei-dongnanya-yingxiangli-keji-zhongxin","Bali在东南亚创业生态系统中排名第16位。随着Web3构建者、AI可持续发展初创公司和生态旅游科技公司的集中，该岛正在打造区域影响力科技之都的独特定位。","2026-03-28T10:44:48.898750Z",{"id":40,"title":41,"slug":42,"excerpt":43,"locale":12,"category_name":31,"published_at":44},"d0000000-0000-0000-0000-000000000667","ASEAN数据保护拼图：开发者合规清单","asean-shuju-baohu-pintu-kaifazhe-heguiqingdan","七个ASEAN国家现已拥有全面的数据保护法律，各自具有不同的同意模型、本地化要求和处罚结构。这是一份为构建多国应用程序的开发者准备的实用合规清单。","2026-03-28T10:44:48.893467Z",{"id":46,"title":47,"slug":48,"excerpt":49,"locale":12,"category_name":31,"published_at":50},"d0000000-0000-0000-0000-000000000666","Indonesia 290亿美元数字化转型：软件公司的机遇","indonesia-290yi-meiyuan-shuzihua-zhuanxing-ruanjian-gongsi-jiyu","Indonesia IT服务市场预计在2026年达到290.3亿美元，高于2025年的243.7亿美元。云基础设施、AI、电子商务和数据中心正在推动东南亚最快的增长。","2026-03-28T10:44:48.875457Z",{"id":13,"name":52,"slug":53,"bio":54,"photo_url":19,"linkedin":19,"role":55,"created_at":56,"updated_at":56},"Open Soft Team","open-soft-team","The engineering team at Open Soft, building premium software solutions from Bali, Indonesia.","Engineering Team","2026-03-28T08:31:22.226811Z"]