WASI 0.3 正式發布,async(非同步)正式成為 WebAssembly Component 的原生能力。WASI 子群組投票通過 WASI 0.3.0 規格,將 WASI 重新奠基於 WebAssembly Component Model 的 async 原語之上。0.3.0 規格已進入穩定階段,runtime 與 toolchain 的支援也正在陸續到位。
過去 WASI 0.2 中 wasi:io 負責處理的事項(pollable、input-stream、output-stream),現在已成為 canonical ABI 的一部分,由 Component Model 原生提供這些基本元素。因此,從 WASI 0.2 升級到 0.3 的大部分改動其實是純機械式的,舊有函式簽章也大幅簡化。
新的 async 原語屬於 Component Model 的 canonical ABI,能讓 binding generator 為各種程式語言產生符合該語言慣用法的 async 綁定。
Component Model Async ABI
在 WASI 0.2 時代,每個 component 都必須自己維護一套 event loop 或 async runtime。這代表 component 雖然可以單獨跑在 host 上,但彼此的 event loop 之間無法協調。如果某個 component 使用了 streaming 或 async API,就無法與其他 component 組合使用。
WASI 0.3 改變了這個局面:host 統一管理一條 event loop,所有 component 共享。這個機制透過將 stream<T>、future<T> 與 async 提升為 canonical ABI 的 first-class 結構來實現:
stream<T>與future<T>的運作方式類似 resource type:每個都是一個 owned handle,跨 component 邊界傳遞時所有權會從 caller 轉移到 callee。與 resource type 不同的是,它們不能被借用。- 排程由 runtime 統一驅動,而不是由各個 component 自己處理。當某個值被傳遞到 future 之後,runtime 會排程等待該值的工作,即使這個 future 已經穿越了多個 component 邊界也一樣。寫入端的角色可以是 host、另一個 component,甚至是持有讀端的同一個 component。
- async 模型採用「完成式」(completion-based)而非「就緒式」(readiness-based),這與 Linux 的 io_uring 以及 Windows 的 IOCP / IoRing 這類高效 API 精神一致。對於需要相容性的程式,epoll / kqueue 風格的就緒式 API 可以在這個模型之上模擬出來。
- Component 直接 export 與 import
async func。WASI 0.2 時代的「start-foo / finish-foo / subscribe」三步驟 dance 已經走入歷史。
WASI 介面的改動
0.3 介面的改動大多屬於純機械式調整。WASI 0.2 為了讓 async 能運作,不得不做一些特殊處理;現在 async 已經是 component model 的原生能力,我們可以寫出跟以前一模一樣的功能,但語法變得直觀許多。
以下整理了 WASI 0.2 透過 wasi:io 套件編碼的模式,以及在 WASI 0.3 透過 Component Model async 後對應的寫法:
| WASI 0.2(wasi:io) | WASI 0.3(Component Model) |
|---|---|
resource pollable | future<T> |
resource input-stream | stream<u8> |
resource output-stream | stream<u8>(寫入方向) |
poll(list<pollable>) | 對 future 進行 await(由 runtime 處理) |
在 resource 上的 subscribe() | 呼叫直接回傳 future<...> |
start-foo / finish-foo | foo: async func(...) |
WASI 0.2 的一個問題是:每個 stream 的讀取動作會把 terminal error 內聯回傳,呼叫端只有一直讀下去才會知道結果;如果提早停止,就無法分辨 stream 是「正常關閉」還是「發生錯誤」。
WASI 0.3 改用更乾淨的解法:stream 會額外回傳一個 future,這個 future 的解析結果與 stream 被消費多少無關,徹底解決了 WASI 0.2 的 stream 狀態判斷問題:
// WASI 0.2 透過 stream 讀取:
func() -> result<input-stream, error-code>;
// WASI 0.3 透過 stream 讀取:
func() -> tuple<stream<u8>, future<result<_, error-code>>>;
語言綁定的改變
Component Model 最強大的優勢之一,就是能輕鬆產生跨語言的綁定。隨著 first-class async 加入,guest 端的 binding generator 就能為各種語言產出「像該語言原生寫法」的 async 綁定。
以 wasi:http/handler 介面為例,它只暴露一個函式 handle,標記為 async:
interface handler {
handle: async func(request: request) -> result<response, error-code>;
}
在 Rust 中,可以用 wit-bindgen crate 實作 HTTP server。這個 crate 會把 handler 介面映射成 Guest trait,把 handle: async func 映射成 async fn handle:
use wasi::http::types::{ErrorCode, Request, Response};
impl Guest for Component {
async fn handle(request: Request) -> Result<Response, ErrorCode> {
// ...
}
}
Python、JavaScript、C#、C 等語言的 guest binding generator 對 async 的支援也正在進行中。這些語言基本上都依賴 stackless coroutine。但 Component Model 的 async ABI 從設計之初就能同時容納 stackful 與 stackless coroutine。
Go 是一個 stackful coroutine 的例子:它不像其他語言同時暴露 async 與非 async 函式,而是 Go runtime 會把看起來同步的呼叫轉成 async 呼叫,並透過「goroutine」這種虛擬執行緒提供並行執行。
透過 componentize-go,我們可以 export 一個 Handle 函式來實作 HTTP server,並用 goroutine 做阻塞式呼叫來串流 HTTP body。Runtime 會在 ABI 邊界暫停 goroutine,等 stream 準備好之後再恢復執行,不會卡住整個程式:
package export_wasi_http_handler
import (
. "wit_component/wasi_http_types"
. "go.bytecodealliance.org/pkg/wit/types"
)
func Handle(request *Request) Result[*Response, ErrorCode] {
tx, rx := MakeStreamU8() // ← 1. 建立一對 channel
go func() { // ← 2. 啟動一個虛擬執行緒
defer tx.Drop()
tx.WriteAll([]uint8("Hello, world!")) // ← 3. 寫入 channel
}()
response, send := ResponseNew( // ← 4. 建立 HTTP response
FieldsFromList([]Tuple2[string, []byte]{
{F0: "content-type", F1: []byte("text/plain")},
}).Ok(),
Some(rx), // ← 5. 把接收端當作 HTTP body
trailersFuture(),
)
send.Drop()
return Ok[*Response, ErrorCode](response) // ← 6. 回傳 HTTP response
}
隨著 WASI 0.3 正式發布,並內建 component model async 的支援,guest toolchain 與 host runtime 將能開始將相關功能穩定下來。在接下來幾週到幾個月內,預期會看到各專案陸續宣布支援 WASI 0.3。
wasi:http 的改動
改動幅度最大的介面是 wasi:http。這次不只是把 poll-based 介面機械式轉成原生 async,更重新組織了 world 結構,並調整了部分核心抽象。
wasi:http 現在暴露兩個 world:wasi:http/service 與 wasi:http/middleware:
interface client { /* ... */ }
interface handler { /* ... */ }
// 給 guest binding generator 使用時,
// 授予透過 `client` import 發起 HTTP 呼叫的能力,
// 並透過 `handler` export 接收傳入的 HTTP request。
world service {
import client;
export handler;
}
// middleware world 是 service world 的超集。
world middleware {
include service; // ← 涵蓋 service 能做的一切
import handler; // ← 額外把傳入的 request 轉給下一個 handler
}
middleware world 取代了 0.2 時代的 proxy world,用來定義可以將 request 轉發給其他 handler 的 HTTP handler。
WASI 0.3 的新賣點是「service chaining」:component 之間可以直接組合,無需走網路 round-trip。這意味著對那些需要頻繁互相呼叫的微服務元件來說,runtime 可以選擇在同個行程內直接把它們組合起來,免除跨網路呼叫。對大多數微服務情境來說,這能把呼叫其他微服務的時間從「毫秒級」壓到「奈秒級」——整整快了一百萬倍。
結語
很高興正式對外宣布 WASI 0.3 已經發布,這代表:
- 規格已批准:WASI 0.3 已通過 WASI 子群組投票。這是穩定版本(stable release),代表你今天為它編譯的程式未來都還能繼續運作。即使後續我們會繼續每兩個月釋出 patch release(例如 0.3.x),向下相容性也會被保留。
- Wasmtime:Wasmtime 45 現已可執行最新的 release candidate;Wasmtime 46 將正式隨出貨預設啟用 Component Model Async 支援的 WASI 0.3.0。
- jco 緊追在後:jco(JavaScript Component Model toolchain)也支援完整的 WASI 0.3。預設啟用支援的版本很快就會釋出。
- 接下來是 guest toolchain:同時,各團隊正同步推進在 guest toolchain 中啟用 WASI 0.3 的工作。等這些版本釋出後,你就能用 Rust、Go、JavaScript、Python 等語言撰寫 WASI 0.3 component。
想開始接觸 WebAssembly Component 的話,最佳入口是 Wasm Component Model book。想知道完整改動清單,可以看 WASI 0.3.0 release notes。如果想了解 WebAssembly Component 與 WASI 的後續發展,可以參考 The Road to Component Model 1.0。
WASI 0.3 是整個社群的心血。感謝 WASI 子群組中所有參與設計、辯論、迭代這個版本的人;感謝 runtime 與 toolchain 的維護者打造出讓規格能落地的實作;感謝每一位送 issue、review PR、問出那個讓規格變得更好的尖銳問題的貢獻者。也感謝在 WASI 之上打造應用的使用者:你們寫的 component、你們的回饋、還有你們願意試用 release candidate 的態度,決定了最終出貨版本的樣貌。
Component Model 的原生 async 走過了漫長的開發歲月,它開啟了嶄新的一章——在這一章中,async 與 composability 都是 first-class,而同一套原語能服務所有程式語言。我們迫不及待想看到你們在它之上打造出什麼。
歡迎來到 WASI 0.3。
網友熱門留言 (5)