From fead0c0a31e78520371e52b94bc2d9329dc54db0 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 25 Feb 2022 22:23:57 +0800 Subject: [PATCH] refactor: plugin structure --- boomer.go | 6 +++--- parser.go | 6 +++--- plugin/{common => internal}/call.go | 2 +- plugin/{common => internal}/call_test.go | 2 +- plugin/{shared => internal}/config.go | 6 +++--- plugin/{common => internal}/go_plugin.go | 2 +- plugin/{common => internal}/go_plugin_test.go | 2 +- .../{common => internal}/hashicorp_plugin.go | 16 +++++++------- .../hashicorp_plugin_test.go | 2 +- .../{shared/interface.go => internal/host.go} | 17 ++++++++++----- plugin/{common => internal}/init.go | 21 ++++++------------- plugin/plugin.go | 11 +++++----- runner.go | 8 +++---- 13 files changed, 48 insertions(+), 53 deletions(-) rename plugin/{common => internal}/call.go (99%) rename plugin/{common => internal}/call_test.go (99%) rename plugin/{shared => internal}/config.go (83%) rename plugin/{common => internal}/go_plugin.go (98%) rename plugin/{common => internal}/go_plugin_test.go (98%) rename plugin/{common => internal}/hashicorp_plugin.go (85%) rename plugin/{common => internal}/hashicorp_plugin_test.go (98%) rename plugin/{shared/interface.go => internal/host.go} (79%) rename plugin/{common => internal}/init.go (67%) diff --git a/boomer.go b/boomer.go index 17090d81..cb1fdd6d 100644 --- a/boomer.go +++ b/boomer.go @@ -9,7 +9,7 @@ import ( "github.com/httprunner/hrp/internal/boomer" "github.com/httprunner/hrp/internal/ga" - "github.com/httprunner/hrp/plugin/common" + pluginInternal "github.com/httprunner/hrp/plugin/internal" ) func NewBoomer(spawnCount int, spawnRate float64) *HRPBoomer { @@ -22,8 +22,8 @@ func NewBoomer(spawnCount int, spawnRate float64) *HRPBoomer { type HRPBoomer struct { *boomer.Boomer - plugins []common.Plugin // each task has its own plugin process - pluginsMutex *sync.RWMutex // avoid data race + plugins []pluginInternal.IPlugin // each task has its own plugin process + pluginsMutex *sync.RWMutex // avoid data race } // Run starts to run load test for one or multiple testcases. diff --git a/parser.go b/parser.go index ce2a8fe3..7445c68a 100644 --- a/parser.go +++ b/parser.go @@ -13,7 +13,7 @@ import ( "github.com/rs/zerolog/log" "github.com/httprunner/hrp/internal/builtin" - "github.com/httprunner/hrp/plugin/common" + pluginInternal "github.com/httprunner/hrp/plugin/internal" ) func newParser() *parser { @@ -21,7 +21,7 @@ func newParser() *parser { } type parser struct { - plugin common.Plugin // plugin is used to call functions + plugin pluginInternal.IPlugin // plugin is used to call functions } func buildURL(baseURL, stepURL string) string { @@ -252,7 +252,7 @@ func (p *parser) callFunc(funcName string, arguments ...interface{}) (interface{ fn := reflect.ValueOf(function) // call with builtin function - return common.CallFunc(fn, arguments...) + return pluginInternal.CallFunc(fn, arguments...) } // merge two variables mapping, the first variables have higher priority diff --git a/plugin/common/call.go b/plugin/internal/call.go similarity index 99% rename from plugin/common/call.go rename to plugin/internal/call.go index 02238cc9..16ce2a42 100644 --- a/plugin/common/call.go +++ b/plugin/internal/call.go @@ -1,4 +1,4 @@ -package common +package pluginInternal import ( "fmt" diff --git a/plugin/common/call_test.go b/plugin/internal/call_test.go similarity index 99% rename from plugin/common/call_test.go rename to plugin/internal/call_test.go index 03b33ba6..b47633e4 100644 --- a/plugin/common/call_test.go +++ b/plugin/internal/call_test.go @@ -1,4 +1,4 @@ -package common +package pluginInternal import ( "errors" diff --git a/plugin/shared/config.go b/plugin/internal/config.go similarity index 83% rename from plugin/shared/config.go rename to plugin/internal/config.go index 423688b1..b5597fde 100644 --- a/plugin/shared/config.go +++ b/plugin/internal/config.go @@ -1,8 +1,8 @@ -package shared +package pluginInternal import "github.com/hashicorp/go-plugin" -const Name = "debugtalk" +const PluginName = "debugtalk" // handshakeConfigs are used to just do a basic handshake between // a plugin and host. If the handshake fails, a user friendly error is shown. @@ -11,5 +11,5 @@ const Name = "debugtalk" var HandshakeConfig = plugin.HandshakeConfig{ ProtocolVersion: 1, MagicCookieKey: "HttpRunnerPlus", - MagicCookieValue: Name, + MagicCookieValue: PluginName, } diff --git a/plugin/common/go_plugin.go b/plugin/internal/go_plugin.go similarity index 98% rename from plugin/common/go_plugin.go rename to plugin/internal/go_plugin.go index 58716964..1fb454e5 100644 --- a/plugin/common/go_plugin.go +++ b/plugin/internal/go_plugin.go @@ -1,4 +1,4 @@ -package common +package pluginInternal import ( "fmt" diff --git a/plugin/common/go_plugin_test.go b/plugin/internal/go_plugin_test.go similarity index 98% rename from plugin/common/go_plugin_test.go rename to plugin/internal/go_plugin_test.go index 6eb1ce90..20c1569c 100644 --- a/plugin/common/go_plugin_test.go +++ b/plugin/internal/go_plugin_test.go @@ -1,7 +1,7 @@ // +build linux freebsd darwin // go plugin doesn't support windows -package common +package pluginInternal import ( "fmt" diff --git a/plugin/common/hashicorp_plugin.go b/plugin/internal/hashicorp_plugin.go similarity index 85% rename from plugin/common/hashicorp_plugin.go rename to plugin/internal/hashicorp_plugin.go index c535accb..246ba0ef 100644 --- a/plugin/common/hashicorp_plugin.go +++ b/plugin/internal/hashicorp_plugin.go @@ -1,4 +1,4 @@ -package common +package pluginInternal import ( "os" @@ -6,8 +6,6 @@ import ( "github.com/hashicorp/go-hclog" "github.com/hashicorp/go-plugin" - "github.com/httprunner/hrp/plugin/shared" - pluginShared "github.com/httprunner/hrp/plugin/shared" "github.com/rs/zerolog/log" ) @@ -16,13 +14,13 @@ var client *plugin.Client // HashicorpPlugin implements hashicorp/go-plugin type HashicorpPlugin struct { logOn bool // turn on plugin log - pluginShared.FuncCaller + FuncCaller cachedFunctions map[string]bool // cache loaded functions to improve performance } func (p *HashicorpPlugin) Init(path string) error { loggerOptions := &hclog.LoggerOptions{ - Name: shared.Name, + Name: PluginName, Output: os.Stdout, } if p.logOn { @@ -32,9 +30,9 @@ func (p *HashicorpPlugin) Init(path string) error { } // launch the plugin process client = plugin.NewClient(&plugin.ClientConfig{ - HandshakeConfig: shared.HandshakeConfig, + HandshakeConfig: HandshakeConfig, Plugins: map[string]plugin.Plugin{ - shared.Name: &shared.HashicorpPlugin{}, + PluginName: &HRPPlugin{}, }, Cmd: exec.Command(path), Logger: hclog.New(loggerOptions), @@ -48,7 +46,7 @@ func (p *HashicorpPlugin) Init(path string) error { } // Request the plugin - raw, err := rpcClient.Dispense(shared.Name) + raw, err := rpcClient.Dispense(PluginName) if err != nil { log.Error().Err(err).Msg("request plugin failed") return err @@ -56,7 +54,7 @@ func (p *HashicorpPlugin) Init(path string) error { // We should have a Function now! This feels like a normal interface // implementation but is in fact over an RPC connection. - p.FuncCaller = raw.(shared.FuncCaller) + p.FuncCaller = raw.(FuncCaller) p.cachedFunctions = make(map[string]bool) log.Info().Str("path", path).Msg("load hashicorp go plugin success") diff --git a/plugin/common/hashicorp_plugin_test.go b/plugin/internal/hashicorp_plugin_test.go similarity index 98% rename from plugin/common/hashicorp_plugin_test.go rename to plugin/internal/hashicorp_plugin_test.go index 2e374041..de6fe7e0 100644 --- a/plugin/common/hashicorp_plugin_test.go +++ b/plugin/internal/hashicorp_plugin_test.go @@ -1,4 +1,4 @@ -package common +package pluginInternal import ( "os" diff --git a/plugin/shared/interface.go b/plugin/internal/host.go similarity index 79% rename from plugin/shared/interface.go rename to plugin/internal/host.go index 9f334808..a8422a38 100644 --- a/plugin/shared/interface.go +++ b/plugin/internal/host.go @@ -1,4 +1,4 @@ -package shared +package pluginInternal import ( "encoding/gob" @@ -89,15 +89,22 @@ func (s *functionRPCServer) Call(args interface{}, resp *interface{}) error { return nil } -// HashicorpPlugin implements hashicorp's plugin.Plugin. -type HashicorpPlugin struct { +// HRPPlugin implements hashicorp's plugin.Plugin. +type HRPPlugin struct { Impl FuncCaller } -func (p *HashicorpPlugin) Server(*plugin.MuxBroker) (interface{}, error) { +func (p *HRPPlugin) Server(*plugin.MuxBroker) (interface{}, error) { return &functionRPCServer{Impl: p.Impl}, nil } -func (HashicorpPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) { +func (HRPPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) { return &functionRPC{client: c}, nil } + +type IPlugin interface { + Init(path string) error // init plugin + Has(funcName string) bool // check if plugin has function + Call(funcName string, args ...interface{}) (interface{}, error) // call function + Quit() error // quit plugin +} diff --git a/plugin/common/init.go b/plugin/internal/init.go similarity index 67% rename from plugin/common/init.go rename to plugin/internal/init.go index c23c4bf3..b0f0ed3f 100644 --- a/plugin/common/init.go +++ b/plugin/internal/init.go @@ -1,33 +1,24 @@ -package common +package pluginInternal import ( "fmt" "os" "path/filepath" - - pluginShared "github.com/httprunner/hrp/plugin/shared" ) type pluginFile string const ( - goPluginFile pluginFile = pluginShared.Name + ".so" // built from go plugin - hashicorpGoPluginFile pluginFile = pluginShared.Name + ".bin" // built from hashicorp go plugin - hashicorpPyPluginFile pluginFile = pluginShared.Name + ".py" + goPluginFile pluginFile = PluginName + ".so" // built from go plugin + hashicorpGoPluginFile pluginFile = PluginName + ".bin" // built from hashicorp go plugin + hashicorpPyPluginFile pluginFile = PluginName + ".py" ) -type Plugin interface { - Init(path string) error // init plugin - Has(funcName string) bool // check if plugin has function - Call(funcName string, args ...interface{}) (interface{}, error) // call function - Quit() error // quit plugin -} - -func Init(path string, logOn bool) (Plugin, error) { +func Init(path string, logOn bool) (IPlugin, error) { if path == "" { return nil, nil } - var plugin Plugin + var plugin IPlugin // priority: hashicorp plugin > go plugin // locate hashicorp plugin file diff --git a/plugin/plugin.go b/plugin/plugin.go index 4dc25731..1b49bffd 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -8,8 +8,7 @@ import ( hclog "github.com/hashicorp/go-hclog" "github.com/hashicorp/go-plugin" - "github.com/httprunner/hrp/plugin/common" - pluginShared "github.com/httprunner/hrp/plugin/shared" + pluginInternal "github.com/httprunner/hrp/plugin/internal" ) // functionsMap stores plugin functions @@ -37,7 +36,7 @@ func (p *functionPlugin) Call(funcName string, args ...interface{}) (interface{} return nil, fmt.Errorf("function %s not found", funcName) } - return common.CallFunc(fn, args...) + return pluginInternal.CallFunc(fn, args...) } var functions = make(functionsMap) @@ -55,17 +54,17 @@ func Register(funcName string, fn interface{}) { func Serve() { funcPlugin := &functionPlugin{ logger: hclog.New(&hclog.LoggerOptions{ - Name: pluginShared.Name, + Name: pluginInternal.PluginName, Output: os.Stdout, Level: hclog.Info, }), functions: functions, } var pluginMap = map[string]plugin.Plugin{ - pluginShared.Name: &pluginShared.HashicorpPlugin{Impl: funcPlugin}, + pluginInternal.PluginName: &pluginInternal.HRPPlugin{Impl: funcPlugin}, } plugin.Serve(&plugin.ServeConfig{ - HandshakeConfig: pluginShared.HandshakeConfig, + HandshakeConfig: pluginInternal.HandshakeConfig, Plugins: pluginMap, }) } diff --git a/runner.go b/runner.go index 2b12761d..d9c2c0f7 100644 --- a/runner.go +++ b/runner.go @@ -32,7 +32,7 @@ import ( "github.com/httprunner/hrp/internal/builtin" "github.com/httprunner/hrp/internal/ga" - "github.com/httprunner/hrp/plugin/common" + pluginInternal "github.com/httprunner/hrp/plugin/internal" ) const ( @@ -282,8 +282,8 @@ func (r *caseRunner) run() error { return nil } -func initPlugin(path string, logOn bool) (plugin common.Plugin, err error) { - plugin, err = common.Init(path, logOn) +func initPlugin(path string, logOn bool) (plugin pluginInternal.IPlugin, err error) { + plugin, err = pluginInternal.Init(path, logOn) if plugin == nil { return } @@ -299,7 +299,7 @@ func initPlugin(path string, logOn bool) (plugin common.Plugin, err error) { // report event for initializing plugin var pluginType string - if _, ok := plugin.(*common.GoPlugin); ok { + if _, ok := plugin.(*pluginInternal.GoPlugin); ok { pluginType = "go" } else { pluginType = "hashicorp"