-
Notifications
You must be signed in to change notification settings - Fork 351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
super basic wasm filter implementation using wazero #2947
Open
szuecs
wants to merge
3
commits into
master
Choose a base branch
from
feature/wasm-filter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+244
−0
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
//export request | ||
func request(x, y uint32) uint32 { | ||
return x + y | ||
} | ||
|
||
//export response | ||
func response(x, y uint32) uint32 { | ||
return x - y | ||
} | ||
|
||
// main is required for the `wasi` target, even if it isn't used. | ||
// See https://wazero.io/languages/tinygo/#why-do-i-have-to-define-main | ||
func main() {} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package wasm | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"os" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/tetratelabs/wazero" | ||
"github.com/tetratelabs/wazero/api" | ||
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" | ||
"github.com/zalando/skipper/filters" | ||
) | ||
|
||
type wasmSpec struct{} | ||
|
||
type wasm struct { | ||
code []byte | ||
mod api.Module | ||
request api.Function | ||
response api.Function | ||
} | ||
|
||
func NewWASM() filters.Spec { | ||
return &wasmSpec{} | ||
} | ||
|
||
// Name implements filters.Spec. | ||
func (*wasmSpec) Name() string { | ||
return filters.WASMName | ||
} | ||
|
||
// CreateFilter implements filters.Spec. | ||
func (*wasmSpec) CreateFilter(args []interface{}) (filters.Filter, error) { | ||
if len(args) != 1 { | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
src, ok := args[0].(string) | ||
if !ok { | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
u, err := url.Parse(src) | ||
if err != nil { | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
|
||
var code []byte | ||
|
||
switch u.Scheme { | ||
case "file": | ||
code, err = os.ReadFile(u.Path) | ||
if err != nil { | ||
logrus.Errorf("Failed to load file %q: %v", u.Path, err) | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
case "https": | ||
panic("not implemented") | ||
default: | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
|
||
ctx := context.Background() | ||
r := wazero.NewRuntime(ctx) // TODO: needs r.Close() | ||
// Instantiate WASI, which implements host functions needed for TinyGo to | ||
// implement `panic`. | ||
wasi_snapshot_preview1.MustInstantiate(ctx, r) | ||
|
||
// Instantiate the guest Wasm into the same runtime. It exports the `add` | ||
// function, implemented in WebAssembly. | ||
mod, err := r.Instantiate(ctx, code) | ||
if err != nil { | ||
logrus.Fatalf("failed to instantiate module: %v", err) | ||
} | ||
request := mod.ExportedFunction("request") | ||
response := mod.ExportedFunction("response") | ||
|
||
return &wasm{ | ||
code: code, | ||
mod: mod, | ||
request: request, | ||
response: response, | ||
}, nil | ||
} | ||
|
||
// Request implements filters.Filter. | ||
func (w *wasm) Request(filters.FilterContext) { | ||
|
||
result, err := w.request.Call(context.Background(), 2, 3) | ||
if err != nil { | ||
logrus.Errorf("failed to call add: %v", err) | ||
} | ||
logrus.Infof("request result: %v", result) | ||
|
||
} | ||
|
||
// Response implements filters.Filter. | ||
func (w *wasm) Response(filters.FilterContext) { | ||
result, err := w.response.Call(context.Background(), 3, 2) | ||
if err != nil { | ||
logrus.Errorf("failed to call add: %v", err) | ||
} | ||
logrus.Infof("response result: %v", result) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should make this lazy and cacheable and cleanup on filter close
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, as I wrote in the PR title super basic. I used https://github.com/tetratelabs/wazero/tree/main/examples/basic to test the super basic example.