-
Notifications
You must be signed in to change notification settings - Fork 266
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
propagate Go context into HostFunctionCallContext #203
propagate Go context into HostFunctionCallContext #203
Conversation
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 rip the bandaid and make Call require a context param. breaking the api has little effect now, and we'll have to break it again later. Otherwise looks great!
@@ -37,8 +44,8 @@ func Test_hostFunc(t *testing.T) { | |||
bufAddr := ret[0] | |||
|
|||
// Store the address info to the memory. | |||
binary.LittleEndian.PutUint32(ctx.Memory.Buffer[retBufPtr:], uint32(bufAddr)) | |||
binary.LittleEndian.PutUint32(ctx.Memory.Buffer[retBufSize:], bufferSize) | |||
ctx.Memory.PutUint32(retBufPtr, uint32(bufAddr)) |
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.
not for this PR, but this reminds me we are overdue making things interfaces as direct access to buffer like this is easy to get wrong! #204
wasm/store.go
Outdated
@@ -314,6 +315,10 @@ func (s *Store) Instantiate(module *Module, name string) error { | |||
} | |||
|
|||
func (s *Store) CallFunction(moduleName, funcName string, params ...uint64) (results []uint64, resultTypes []ValueType, err error) { |
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 can delete this option and then don't have to name it differently.
ex. dapr where there's no option except with context, it is simpler naming
https://github.com/dapr/go-sdk/blob/main/client/client.go
Since this is an early stage, we can break api now
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.
Did you know that I work on Dapr or was this coincidence? 😀
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 did notice 😎 so decided to speak Lingua Dapra
PS context propagation is absolutely important and best to decouple from things like lifecycle hooks. Later, we'll likely build on this with a begin/end hook design which can allow transparent metrics or tracing. This is bedrock for that also. |
Yeah I like this |
@codefromthecrypt Totally agree! Changes made. |
e08a024
to
e4c1736
Compare
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.
one doc backfill suggestion, then a question of how or if we want to handle a nil context on the CallFunction API. Otherwise, LGTM
return fmt.Errorf("calling start function failed: %v", err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (s *Store) CallFunction(moduleName, funcName string, params ...uint64) (results []uint64, resultTypes []ValueType, err error) { | ||
// Note: this API is unstable. See tetratelabs/wazero#170 | ||
func (s *Store) CallFunction(ctx context.Context, moduleName, funcName string, params ...uint64) (results []uint64, resultTypes []ValueType, err error) { |
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.
Should we fail early, fail late or default any nil context here?
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.
My gut says to set it to context.Background() so that host calls can assume a valid context is passed. That or fail/return an error.
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.
ok let's go with context.Background, and update the docs to say that's what it defaults if nil. TestStore_CallFunction should verify this is never nil
wasm/store.go
Outdated
@@ -844,6 +847,7 @@ func DecodeBlockType(types []*TypeInstance, r io.Reader) (*FunctionType, uint64, | |||
|
|||
// HostFunctionCallContext is the first argument of all host functions. | |||
type HostFunctionCallContext struct { | |||
context.Context |
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 don't typically use struct embedding, so we should trailing comment why if we do. Alternatively, we can de-embed this. wdyt @pkedy @mathetake @nullpo-head?
context.Context | |
context.Context |
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.
yeah without concrete reason, we would like to avoid embedding
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.
but this case seems reasonable to me so trailing comments would help
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.
Embedding context I think is pretty common in web frameworks so you can pass it along. The standard http library does not so it might be a matter of preference. What are the reasons not to embed it? Just curious 😀
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.
Clarifying the owner of fields/methods at callsite is one!
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.
sg!
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.
Nice. I have the change ready. Committing now.
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 will say that it will be a little weird that Context()
is a func and Memory
is a field. But as you said, this will be reworked anyway.
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.
Ack and I promise I'll do my best to unweird it soon
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.
also PS I'm not doing this now because I don't want to drift your PR :D
e4c1736
to
f0c1643
Compare
ps oops on arm drift
|
…alls Signed-off-by: Phil Kedy <phil.kedy@gmail.com>
…ion (breaking change)
…c instead of embedding it.
Signed-off-by: Phil Kedy <phil.kedy@gmail.com>
e5ce7f9
to
fb45917
Compare
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.
excellent!
Signed-off-by: Phil Kedy <phil.kedy@gmail.com>
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.
@pkedy will merge when you say go. good work, here
Good to go. Thanks! |
ps drift alert that this was renamed to ModuleContext. Also, if you are using something besides tinygo and/or need a WASI API not defined here, please pipe up in the issue #271 |
This PR adds an alternate
store.CallFunctionContext(ctx context.Context, moduleName, funcName string, params ...uint64)
func so the host can set values in a Go context that are accessible via the context.Value in*wasm.HostFunctionCallContext
. This would allow custom data as well as distributed tracing values to flow through the call stack in a way that Go developers would expect.See
examples/host_func_test.go
for usageThis is a simpler and more idiomatic alternative to #183 and #184.
/cc @codefromthecrypt