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

When call ProxyOnRequestHeaders get wrong action #14

Open
Taction opened this issue Oct 9, 2021 · 5 comments
Open

When call ProxyOnRequestHeaders get wrong action #14

Taction opened this issue Oct 9, 2021 · 5 comments
Assignees

Comments

@Taction
Copy link

Taction commented Oct 9, 2021

action

cd example and replace the wasm file to new file which handles HTTP headers like this. Then go run . run the example.

expected behaviour

If the header hit, Action::Pause will be returned from wasm side. ActionPause will be returned by the function ProxyOnRequestHeaders.

actual behaviour

Regardless of whether the expected header appears. ActionContinue will be returned by the function ProxyOnRequestHeaders

about the code

I noticed that the action ProxyOnRequestHeaders returned is actually from CallWasmFunction, and res is ignored.

Whether action should be ignored, the value of res should be converted into action format and returned. And what is a.Imports.Wait() actually do, is there any example for it?

@antJack
Copy link
Contributor

antJack commented Oct 11, 2021

I've noticed that, thanks. That's exactly a question for your rust example. And maybe we should consider the return value of both proxy_on_request_headers and a.Imports.Wait() on ABIContext.ProxyOnRequestHeaders, instead of simply taking the return value of a.Imports.Wait(), and ignoring proxy_on_request_headers.

a.Imports.Wait() is actually for async callout from the wasm module. For example, if you make a HTTP callout in your wasm module, then the go-host should launch the HTTP call, and wait for a response synchronously, and that's the Wait function exactly do. In this case, the ActionContinue from proxy_on_request_headers should be ignored, since you're waiting for the response of the previous HTTP callout to make further decision.

@Taction
Copy link
Author

Taction commented Oct 11, 2021

Thanks for your reply. When I stress test the code, whether it uses the original wasm or my own, memory leaks will occur. Do you have any ideas about it?

@antJack
Copy link
Contributor

antJack commented Oct 11, 2021

try pprof, may need to add the following pprof section into mosn config:
image

@Taction
Copy link
Author

Taction commented Oct 11, 2021

Thanks, I'll try it later.

@codefromthecrypt
Copy link
Contributor

codefromthecrypt commented Nov 3, 2022

@Taction this module is incompatible with any current version of proxy-wasm. The only thing I could get to build is an old version of 0.1.0. I would suggest working with mosn project leads to allow an alternate implementation in mosn. That or we add https://http-wasm.io/http-handler/ and leave proxy-wasm alone. Since you are an end user I think best you help drive action here. cc @taoyuanyuan

package main

import (
	"strconv"

	"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
	"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
)

// build main like below with proxy-wasm-go-sdk v0.0.14 which works with
// TinyGo 0.19.0 (which works with Go 1.16).
//
//	tinygo build -o main.wasm -scheduler=none -target=wasi -tags 'abi_010' --no-debug ./main.go
//
// Note: Old tooling is needed because mosn.io/proxy-wasm-go-host v2 is
// incompatible with the proxy-wasm specification.
func main() {
	proxywasm.SetNewHttpContext(newHttpContext)
}

type myHttpContext struct {
	// you must embed the default context so that you need not to re-implement all the methods by yourself
	proxywasm.DefaultHttpContext
	contextID uint32
}

func newHttpContext(rootContextID, contextID uint32) proxywasm.HttpContext {
	return &myHttpContext{contextID: contextID}
}

func (ctx *myHttpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
	key := "Wasm-Context"
	err := proxywasm.SetHttpRequestHeader(key, strconv.Itoa(int(ctx.contextID)))
	if err != nil {
		proxywasm.LogCritical("failed to set request header: " + key)
	}
	return types.ActionContinue
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants