Refactors API to ensure context propagation (#482)
This is an API breaking change that does a few things:
* Stop encouraging practice that can break context propagation:
* Stops caching `context.Context` in `wazero.RuntimeConfig`
* Stops caching `context.Context` in `api.Module`
* Fixes context propagation in function calls:
* Changes `api.Function`'s arg0 from `api.Module` to `context.Context`
* Adds `context.Context` parameter in instantiation (propagates to
.start)
* Allows context propagation for heavy operations like compile:
* Adds `context.Context` as the initial parameter of `CompileModule`
The design we had earlier was a good start, but this is the only way to
ensure coherence when users start correlating or tracing. While adding a
`context.Context` parameter may seem difficult, wazero is a low-level
library and WebAssembly is notoriously difficult to troubleshoot. In
other words, it will be easier to explain to users to pass (even nil) as
the context parameter vs try to figure out things without coherent
context.
Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
21
config.go
21
config.go
@@ -1,7 +1,6 @@
|
||||
package wazero
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -18,14 +17,12 @@ import (
|
||||
type RuntimeConfig struct {
|
||||
enabledFeatures wasm.Features
|
||||
newEngine func(wasm.Features) wasm.Engine
|
||||
ctx context.Context
|
||||
memoryMaxPages uint32
|
||||
}
|
||||
|
||||
// engineLessConfig helps avoid copy/pasting the wrong defaults.
|
||||
var engineLessConfig = &RuntimeConfig{
|
||||
enabledFeatures: wasm.Features20191205,
|
||||
ctx: context.Background(),
|
||||
memoryMaxPages: wasm.MemoryMaxPages,
|
||||
}
|
||||
|
||||
@@ -34,7 +31,6 @@ func (c *RuntimeConfig) clone() *RuntimeConfig {
|
||||
return &RuntimeConfig{
|
||||
enabledFeatures: c.enabledFeatures,
|
||||
newEngine: c.newEngine,
|
||||
ctx: c.ctx,
|
||||
memoryMaxPages: c.memoryMaxPages,
|
||||
}
|
||||
}
|
||||
@@ -56,23 +52,6 @@ func NewRuntimeConfigInterpreter() *RuntimeConfig {
|
||||
return ret
|
||||
}
|
||||
|
||||
// WithContext sets the default context used to initialize the module. Defaults to context.Background if nil.
|
||||
//
|
||||
// Notes:
|
||||
// * If the Module defines a start function, this is used to invoke it.
|
||||
// * This is the outer-most ancestor of api.Module Context() during api.Function invocations.
|
||||
// * This is the default context of api.Function when callers pass nil.
|
||||
//
|
||||
// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#start-function%E2%91%A0
|
||||
func (c *RuntimeConfig) WithContext(ctx context.Context) *RuntimeConfig {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
ret := c.clone()
|
||||
ret.ctx = ctx
|
||||
return ret
|
||||
}
|
||||
|
||||
// WithMemoryMaxPages reduces the maximum number of pages a module can define from 65536 pages (4GiB) to a lower value.
|
||||
//
|
||||
// Notes:
|
||||
|
||||
Reference in New Issue
Block a user