Skip to content

Commit

Permalink
Improve plugin resolving on module creation (#114)
Browse files Browse the repository at this point in the history
* Capture full reference to plugin on function info

* Simplify plugin resolver
  • Loading branch information
mattjohnsonpint authored Mar 20, 2024
1 parent ad445a8 commit b917809
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 19 deletions.
8 changes: 4 additions & 4 deletions functions/fncall.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func CallFunction(ctx context.Context, mod wasm.Module, info schema.FunctionInfo

// Call the wasm function
logger.Info(ctx).
Str("plugin", info.PluginName).
Str("plugin", info.Plugin.Name()).
Str("function", fnName).
Str("resolver", schema.Resolver()).
Bool("user_visible", true).
Expand All @@ -56,7 +56,7 @@ func CallFunction(ctx context.Context, mod wasm.Module, info schema.FunctionInfo
duration := time.Since(start)
if err != nil {
logger.Err(ctx, err).
Str("plugin", info.PluginName).
Str("plugin", info.Plugin.Name()).
Str("function", fnName).
Dur("duration_ms", duration).
Bool("user_visible", true).
Expand All @@ -70,7 +70,7 @@ func CallFunction(ctx context.Context, mod wasm.Module, info schema.FunctionInfo
result, err := convertResult(mem, *schema.FieldDef.Type, def.ResultTypes()[0], res[0])
if err != nil {
logger.Err(ctx, err).
Str("plugin", info.PluginName).
Str("plugin", info.Plugin.Name()).
Str("function", fnName).
Dur("duration_ms", duration).
Str("schema_type", schema.FieldDef.Type.NamedType).
Expand All @@ -81,7 +81,7 @@ func CallFunction(ctx context.Context, mod wasm.Module, info schema.FunctionInfo
}

logger.Info(ctx).
Str("plugin", info.PluginName).
Str("plugin", info.Plugin.Name()).
Str("function", fnName).
Dur("duration_ms", duration).
Bool("user_visible", true).
Expand Down
6 changes: 3 additions & 3 deletions functions/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func registerFunctions(ctx context.Context, gqlSchema string) error {
for _, fn := range module.ExportedFunctions() {
fnName := fn.ExportNames()[0]
if strings.EqualFold(fnName, scma.FunctionName()) {
info := schema.FunctionInfo{PluginName: plugin.Name(), Schema: scma}
info := schema.FunctionInfo{Plugin: &plugin, Schema: scma}
resolver := scma.Resolver()
oldInfo, existed := FunctionsMap[resolver]
if existed && reflect.DeepEqual(oldInfo, info) {
Expand All @@ -77,13 +77,13 @@ func registerFunctions(ctx context.Context, gqlSchema string) error {
break
}
}
_, foundPlugin := host.Plugins.GetByName(info.PluginName)
_, foundPlugin := host.Plugins.GetByName(info.Plugin.Name())
if !foundSchema || !foundPlugin {
delete(FunctionsMap, resolver)
logger.Info(ctx).
Str("resolver", resolver).
Str("function", info.FunctionName()).
Str("plugin", info.PluginName).
Str("plugin", info.Plugin.Name()).
Msg("Unregistered function.")
}
}
Expand Down
10 changes: 2 additions & 8 deletions host/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func unloadPlugin(ctx context.Context, plugin Plugin) error {
return (*plugin.Module).Close(ctx)
}

func GetModuleInstance(ctx context.Context, pluginName string) (wasm.Module, buffers, error) {
func GetModuleInstance(ctx context.Context, plugin *Plugin) (wasm.Module, buffers, error) {

// Get the logger and writers for the plugin's stdout and stderr.
log := logger.Get(ctx).With().Bool("user_visible", true).Logger()
Expand All @@ -171,15 +171,9 @@ func GetModuleInstance(ctx context.Context, pluginName string) (wasm.Module, buf
wOut := io.MultiWriter(buf.Stdout, wInfoLog)
wErr := io.MultiWriter(buf.Stderr, wErrorLog)

// Get the plugin.
plugin, ok := Plugins.GetByName(pluginName)
if !ok {
return nil, buf, fmt.Errorf("plugin not found with name '%s'", pluginName)
}

// Configure the module instance.
cfg := wazero.NewModuleConfig().
WithName(pluginName + "_" + uuid.NewString()).
WithName(plugin.Name() + "_" + uuid.NewString()).
WithSysWalltime().WithSysNanotime().
WithStdout(wOut).WithStderr(wErr)

Expand Down
5 changes: 3 additions & 2 deletions schema/fnschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"fmt"

"hmruntime/host"
"hmruntime/logger"

"github.com/dgraph-io/gqlparser/ast"
Expand All @@ -16,8 +17,8 @@ import (
)

type FunctionInfo struct {
PluginName string
Schema FunctionSchema
Plugin *host.Plugin
Schema FunctionSchema
}

type FunctionSchema struct {
Expand Down
4 changes: 2 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
// Each request will get its own instance of the plugin module,
// so that we can run multiple requests in parallel without risk
// of corrupting the module's memory.
mod, buf, err := host.GetModuleInstance(ctx, info.PluginName)
mod, buf, err := host.GetModuleInstance(ctx, info.Plugin)
if err != nil {
logger.Err(ctx, err).
Str("plugin", info.PluginName).
Str("plugin", info.Plugin.Name()).
Msg("Failed to get module instance.")
err := writeErrorResponse(w, err)
if err != nil {
Expand Down

0 comments on commit b917809

Please sign in to comment.