Skip to content
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
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

szuecs
Copy link
Member

@szuecs szuecs commented Feb 19, 2024

A very simple implementation to test how WASM support #2946 could look like.

wasm code compile with tinygo, but it could also be some other compiler:

% tinygo build -o add.wasm -target=wasi add.go

export request (add(x,y)) and response (sub(x,y))

% cat /home/sszuecs/go/src/github.com/zalando/skipper/filters/wasm/testdata/add.go
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() {}
% ./bin/skipper -inline-routes='r: * -> status(200) -> wasm("file:///home/sszuecs/go/src/github.com/zalando/skipper/filters/wasm/testdata/add.wasm") -> <shunt>'
[APP]INFO[0000] Expose metrics in codahale format
[APP]INFO[0000] enable swarm: false
[APP]INFO[0000] Replacing tee filter specification
[APP]INFO[0000] Replacing teenf filter specification
[APP]INFO[0000] Replacing lua filter specification
[APP]INFO[0000] support listener on :9911
[APP]INFO[0000] Dataclients are updated once, first load complete
[APP]INFO[0000] Listen on :9090
[APP]INFO[0000] route settings, reset, route: r: * -> status(200) -> wasm("file:///home/sszuecs/go/src/github.com/zalando/skipper/filters/wasm/testdata/add.wasm") -> <shunt>
[APP]INFO[0000] route settings received
[APP]INFO[0000] TLS settings not found, defaulting to HTTP
[APP]INFO[0000] route settings applied
[APP]INFO[0012] request result: [5]     <- request(2,3) result
[APP]INFO[0012] response result: [1]    <- response(3,2) result
::1 - - [19/Feb/2024:17:35:54 +0100] "GET /foo HTTP/1.1" 200 0 "-" "curl/7.49.0" 0 localhost:9090 - -

Load error would look like:

[APP]INFO[0000] route settings, reset, route: r: * -> status(200) -> wasm("file://filters/wasm/testdata/add.wasm") -> <shunt>
[APP]INFO[0000] route settings received
[APP]ERRO[0000] Failed to load file "/wasm/testdata/add.wasm": open /wasm/testdata/add.wasm: no such file or directory
[APP]ERRO[0000] failed to process route r: failed to create filter "wasm": invalid filter parameters
[APP]INFO[0000] route settings applied

Signed-off-by: Sandor Szücs <sandor.szuecs@zalando.de>

switch u.Scheme {
case "file":
code, err = os.ReadFile(u.Path)
Copy link
Member

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

Copy link
Member Author

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.

feature: use compilation cache
refactor: pass context from request to wasm call

Signed-off-by: Sandor Szücs <sandor.szuecs@zalando.de>
Signed-off-by: Sandor Szücs <sandor.szuecs@zalando.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants