-
Notifications
You must be signed in to change notification settings - Fork 312
Proposal: an interface for modeling long-running processes for HTTP serving #807
Description
Hi there,
I'd like to bring attention to an area of ambiguity in the wasi:http specification concerning runtime behavior for incoming requests. Currently, implementations like wasmtime serve reinitialize the wasm Store for each invocation of the incoming-handler, effectively treating wasi:http as a stateless, serverless framework akin to Lambda/Azure functions. This approach leverages the benefits of small wasm module size and quick startup times. However, the specification does not explicitly address an alternative scenario where the wasm module acts as a long-running process, maintaining multiple sockets in memory. This approach offers its own set of tradeoffs, such as
- amortizing costs over frequent external network calls, similar to a database connection pool.
- sharing states across requests
- more efficient use of system resources by avoiding frequent spinning up/tearing down instances.
- monitoring
An example of this implementation can be seen in @brendanburns's work with wasi-go.
The lack of explicit guidance in the spec could lead to divergent runtime assumptions and decisions, potentially confusing developers. To illustrate, consider this HTTP handler code in Go:
package main
import (
"fmt"
"net/http"
"github.com/dev-wasm/dev-wasm-go/http/server/handler"
)
var count = 0
func init() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
count++
w.WriteHeader(200)
w.Write([]byte(fmt.Sprintf("Hello from WASM! (%d)", count)))
})
handler.ListenAndServe(nil)
}
func main() {}In the wasmtime serve implementation, the count variable always prints 1 due to the creation of a new wasm instance for each request, preventing the sharing of local variables across requests. This behavior may differ in other implementations, as the spec does not explicitly define these semantics.
I propose that we consider the potential benefits of a wasi:http world that models long-running, container-like wasm modules. If this seems valuable, I would suggest introducing wasi:http/stateful-proxy to represent this concept. Correspondingly, to maintain clarity, wasi:http/proxy could be renamed to wasi:http/serverless-proxy.