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

Wasm middleware: add ability to pass config to guest #2918

Merged
merged 14 commits into from
Aug 7, 2023

Conversation

Taction
Copy link
Member

@Taction Taction commented Jun 17, 2023

Description

Add config for http wasm middleware.
Wasm code can get this configuration via handler.Host.GetConfig(), examples in middleware/http/wasm/internal/e2e-guests/config/main.go.

Also, fix some inconsistencies in the benchmark tests.

Issue reference

We strive to have all PR being opened based on an issue, where the problem or feature have been discussed prior to implementation.

Please reference the issue this PR will close: #2917

Checklist

Please make sure you've completed the relevant tasks for this PR, out of the following list:

@Taction Taction requested review from a team as code owners June 17, 2023 06:11
@ItalyPaleAle ItalyPaleAle added this to the v1.12 milestone Jun 30, 2023
@yaron2
Copy link
Member

yaron2 commented Jul 11, 2023

@codefromthecrypt can you please take a look?

Copy link
Contributor

@codefromthecrypt codefromthecrypt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good as only minor things. Like usual we need the docs updated with text on what "config" means because plain property names are ambiguous (e.g. is this the guest or is this the host?)

middleware/http/wasm/benchmark_test.go Outdated Show resolved Hide resolved
middleware/http/wasm/httpwasm.go Outdated Show resolved Hide resolved
@@ -54,7 +58,8 @@ func (m *middleware) getHandler(ctx context.Context, metadata dapr.Metadata) (*r
WithRandSource(rand.Reader).
WithSysNanosleep().
WithSysWalltime().
WithSysNanosleep()))
WithSysNanosleep()),
handler.GuestConfig(wasmConfig))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you raise a PR upstream to coerce empty to nil?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When writing into memory, the upstream has checked the len of config bytes, does it necessary to coerce empty to nil?

// getConfig implements the WebAssembly host function handler.FuncGetConfig.
func (m *middleware) getConfig(_ context.Context, mod wazeroapi.Module, stack []uint64) {
	buf := uint32(stack[0])
	bufLimit := handler.BufLimit(stack[1])

	configLen := writeIfUnderLimit(mod.Memory(), buf, bufLimit, m.guestConfig)

	stack[0] = uint64(configLen)
}

func writeIfUnderLimit(mem wazeroapi.Memory, offset, limit handler.BufLimit, v []byte) (vLen uint32) {
	vLen = uint32(len(v))
	if vLen > limit {
		return // caller can retry with a larger limit
	} else if vLen == 0 {
		return // nothing to write
	}
	mem.Write(offset, v)
	return
}

middleware/http/wasm/internal/e2e-guests/rewrite/main.wasm Outdated Show resolved Hide resolved
middleware/http/wasm/internal/e2e_test.go Outdated Show resolved Hide resolved
Signed-off-by: zhangchao <[email protected]>

# Conflicts:
#	middleware/http/wasm/example/router.wasm
#	middleware/http/wasm/httpwasm.go
#	middleware/http/wasm/internal/e2e-guests/output/main.wasm
#	middleware/http/wasm/internal/e2e-guests/rewrite/main.wasm
Signed-off-by: zhangchao <[email protected]>
Signed-off-by: zhangchao <[email protected]>
@Taction
Copy link
Member Author

Taction commented Jul 12, 2023

Thanks, I will open a doc issue and pr soon.

Copy link
Contributor

@codefromthecrypt codefromthecrypt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only request is to revert the change to the other binaries so that we don't thrash git with binary changes. I think tinygo isn't 100pct idempotent, and we only changed the config one (new one)

internal/wasm/wasm.go Outdated Show resolved Hide resolved
Copy link
Contributor

@ItalyPaleAle ItalyPaleAle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but can you please add the copyright headers?

Also, does the binding need the configuration too?

middleware/http/wasm/httpwasm.go Outdated Show resolved Hide resolved
Signed-off-by: zhangchao <[email protected]>
@evacchi
Copy link
Contributor

evacchi commented Jul 28, 2023

I think this PR only needs to rebase

Signed-off-by: zhangchao <[email protected]>

# Conflicts:
#	middleware/http/wasm/benchmark_test.go
#	middleware/http/wasm/httpwasm.go
#	middleware/http/wasm/internal/e2e_test.go
@ItalyPaleAle
Copy link
Contributor

@Taction is this ready to be merged? Does the binding need configuration too?

@ItalyPaleAle ItalyPaleAle changed the title Add config for http wasm middleware Wasm middleware: add ability to pass config to guest Aug 4, 2023
@Taction
Copy link
Member Author

Taction commented Aug 7, 2023

@ItalyPaleAle This is ready to be merged. The binding doesn't need this.

@ItalyPaleAle
Copy link
Contributor

@Taction ok I’ll merge once CI is green.

why do bindings not need this?

@Taction
Copy link
Member Author

Taction commented Aug 7, 2023

For middleware, since it can not access the env and file system, there needs a way for wasm code to get the configuration.
For binding, it accepts request data as stdin and request metadata as cli args, this is flexible enough to allow the application to pass request data and configuration information to the binding wasm code. So I think it is ok to not add this unless there are any end users that want a similar way to pass global configurations.

@codefromthecrypt
Copy link
Contributor

well put @Taction. In technical terms, there are no ABI requirements for binding as it should be fully described via the command/exec pattern.

Signed-off-by: zhangchao <[email protected]>
@ItalyPaleAle ItalyPaleAle merged commit c957420 into dapr:master Aug 7, 2023
82 of 83 checks passed
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

Successfully merging this pull request may close these issues.

Add config metadata for Wasm http middleware
5 participants