Skip to content

Commit 85924c9

Browse files
committed
refactor: merge shared to fungo
1 parent 5856942 commit 85924c9

11 files changed

Lines changed: 41 additions & 51 deletions

File tree

fungo/grpc.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"google.golang.org/grpc"
99

1010
"github.com/httprunner/funplugin/fungo/protoGen"
11-
"github.com/httprunner/funplugin/shared"
1211
jsoniter "github.com/json-iterator/go"
1312
)
1413

@@ -65,7 +64,7 @@ func (m *functionGRPCClient) Call(funcName string, funcArgs ...interface{}) (int
6564
// Here is the gRPC server that functionGRPCClient talks to.
6665
type functionGRPCServer struct {
6766
protoGen.UnimplementedDebugTalkServer
68-
Impl shared.IFuncCaller
67+
Impl IFuncCaller
6968
}
7069

7170
func (m *functionGRPCServer) GetNames(ctx context.Context, req *protoGen.Empty) (*protoGen.GetNamesResponse, error) {
@@ -104,7 +103,7 @@ func (m *functionGRPCServer) Call(ctx context.Context, req *protoGen.CallRequest
104103
// GRPCPlugin implements hashicorp's plugin.GRPCPlugin.
105104
type GRPCPlugin struct {
106105
plugin.Plugin
107-
Impl shared.IFuncCaller
106+
Impl IFuncCaller
108107
}
109108

110109
func (p *GRPCPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {

shared/config.go renamed to fungo/init.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
package shared
1+
package fungo
22

33
import (
4+
hclog "github.com/hashicorp/go-hclog"
45
"github.com/hashicorp/go-plugin"
56
)
67

7-
const Version = "v0.5.0"
8+
const Version = "v0.5.2"
9+
10+
var (
11+
logger = Logger
12+
)
13+
14+
var Logger = hclog.New(&hclog.LoggerOptions{
15+
Name: "fungo",
16+
Output: hclog.DefaultOutput,
17+
Level: hclog.Debug,
18+
Color: hclog.AutoColor,
19+
})
820

921
// PluginTypeEnvName is used to specify hashicorp go plugin type, rpc/grpc
1022
const PluginTypeEnvName = "HRP_PLUGIN_TYPE"
@@ -18,3 +30,9 @@ var HandshakeConfig = plugin.HandshakeConfig{
1830
MagicCookieKey: "HttpRunnerPlus",
1931
MagicCookieValue: "debugtalk",
2032
}
33+
34+
// IFuncCaller is the interface that we're exposing as a plugin.
35+
type IFuncCaller interface {
36+
GetNames() ([]string, error) // get all plugin function names list
37+
Call(funcName string, args ...interface{}) (interface{}, error) // call plugin function
38+
}

fungo/plugin.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ import (
77

88
hclog "github.com/hashicorp/go-hclog"
99
"github.com/hashicorp/go-plugin"
10-
11-
"github.com/httprunner/funplugin/shared"
12-
)
13-
14-
var (
15-
Version = shared.Version
16-
logger = shared.Logger.Named("fungo")
1710
)
1811

1912
// functionsMap stores plugin functions
@@ -43,7 +36,7 @@ func (p *functionPlugin) Call(funcName string, args ...interface{}) (interface{}
4336
return nil, fmt.Errorf("function %s not found", funcName)
4437
}
4538

46-
return shared.CallFunc(fn, args...)
39+
return CallFunc(fn, args...)
4740
}
4841

4942
var functions = make(functionsMap)
@@ -57,7 +50,7 @@ func Register(funcName string, fn interface{}) {
5750
logger.Info("register plugin function", "funcName", funcName)
5851
functions[funcName] = reflect.ValueOf(fn)
5952
// automatic registration with common name
60-
functions[shared.ConvertCommonName(funcName)] = functions[funcName]
53+
functions[ConvertCommonName(funcName)] = functions[funcName]
6154
}
6255

6356
// serveRPC starts a plugin server process in RPC mode.
@@ -74,7 +67,7 @@ func serveRPC() {
7467
}
7568
// start RPC server
7669
plugin.Serve(&plugin.ServeConfig{
77-
HandshakeConfig: shared.HandshakeConfig,
70+
HandshakeConfig: HandshakeConfig,
7871
Plugins: pluginMap,
7972
})
8073
}
@@ -93,15 +86,15 @@ func serveGRPC() {
9386
}
9487
// start gRPC server
9588
plugin.Serve(&plugin.ServeConfig{
96-
HandshakeConfig: shared.HandshakeConfig,
89+
HandshakeConfig: HandshakeConfig,
9790
Plugins: pluginMap,
9891
GRPCServer: plugin.DefaultGRPCServer,
9992
})
10093
}
10194

10295
// default to run plugin in gRPC mode
10396
func Serve() {
104-
if os.Getenv(shared.PluginTypeEnvName) == "rpc" {
97+
if os.Getenv(PluginTypeEnvName) == "rpc" {
10598
serveRPC()
10699
} else {
107100
// default

fungo/rpc.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"net/rpc"
66

77
"github.com/hashicorp/go-plugin"
8-
9-
"github.com/httprunner/funplugin/shared"
108
)
119

1210
func init() {
@@ -61,7 +59,7 @@ func (g *functionRPCClient) Call(funcName string, funcArgs ...interface{}) (inte
6159

6260
// functionRPCServer runs on the plugin side, executing the user custom function.
6361
type functionRPCServer struct {
64-
Impl shared.IFuncCaller
62+
Impl IFuncCaller
6563
}
6664

6765
// plugin execution
@@ -93,7 +91,7 @@ func (s *functionRPCServer) Call(args interface{}, resp *interface{}) error {
9391

9492
// RPCPlugin implements hashicorp's plugin.Plugin.
9593
type RPCPlugin struct {
96-
Impl shared.IFuncCaller
94+
Impl IFuncCaller
9795
}
9896

9997
func (p *RPCPlugin) Server(*plugin.MuxBroker) (interface{}, error) {

shared/utils.go renamed to fungo/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package shared
1+
package fungo
22

33
import (
44
"fmt"
@@ -10,7 +10,7 @@ import (
1010
func CallFunc(fn reflect.Value, args ...interface{}) (interface{}, error) {
1111
argumentsValue, err := convertArgs(fn, args...)
1212
if err != nil {
13-
Logger.Error("convert arguments failed", "error", err)
13+
logger.Error("convert arguments failed", "error", err)
1414
return nil, err
1515
}
1616
return call(fn, argumentsValue)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package shared
1+
package fungo
22

33
import (
44
"errors"

go_plugin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"reflect"
77
"runtime"
88

9-
"github.com/httprunner/funplugin/shared"
9+
"github.com/httprunner/funplugin/fungo"
1010
)
1111

1212
// goPlugin implements golang official plugin
@@ -74,7 +74,7 @@ func (p *goPlugin) Call(funcName string, args ...interface{}) (interface{}, erro
7474
return nil, fmt.Errorf("function %s not found", funcName)
7575
}
7676
fn := p.cachedFunctions[funcName]
77-
return shared.CallFunc(fn, args...)
77+
return fungo.CallFunc(fn, args...)
7878
}
7979

8080
func (p *goPlugin) Quit() error {

hashicorp_plugin.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/pkg/errors"
1212

1313
"github.com/httprunner/funplugin/fungo"
14-
"github.com/httprunner/funplugin/shared"
1514
)
1615

1716
type rpcType string
@@ -29,7 +28,7 @@ func (t rpcType) String() string {
2928
type hashicorpPlugin struct {
3029
client *plugin.Client
3130
rpcType rpcType
32-
funcCaller shared.IFuncCaller
31+
funcCaller fungo.IFuncCaller
3332
cachedFunctions sync.Map // cache loaded functions to improve performance, key is function name, value is bool
3433
path string // plugin file path
3534
option *pluginOption
@@ -42,7 +41,7 @@ func newHashicorpPlugin(path string, option *pluginOption) (*hashicorpPlugin, er
4241
}
4342

4443
// plugin type, grpc or rpc
45-
p.rpcType = rpcType(os.Getenv(shared.PluginTypeEnvName))
44+
p.rpcType = rpcType(os.Getenv(fungo.PluginTypeEnvName))
4645
if p.rpcType != rpcTypeRPC {
4746
p.rpcType = rpcTypeGRPC // default
4847
}
@@ -66,12 +65,12 @@ func newHashicorpPlugin(path string, option *pluginOption) (*hashicorpPlugin, er
6665
// hashicorp go plugin
6766
cmd = exec.Command(path)
6867
}
69-
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%s", shared.PluginTypeEnvName, p.rpcType))
68+
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%s", fungo.PluginTypeEnvName, p.rpcType))
7069

7170
// launch the plugin process
7271
logger.Info("launch the plugin process")
7372
p.client = plugin.NewClient(&plugin.ClientConfig{
74-
HandshakeConfig: shared.HandshakeConfig,
73+
HandshakeConfig: fungo.HandshakeConfig,
7574
Plugins: map[string]plugin.Plugin{
7675
rpcTypeRPC.String(): &fungo.RPCPlugin{},
7776
rpcTypeGRPC.String(): &fungo.GRPCPlugin{},
@@ -98,7 +97,7 @@ func newHashicorpPlugin(path string, option *pluginOption) (*hashicorpPlugin, er
9897

9998
// We should have a Function now! This feels like a normal interface
10099
// implementation but is in fact over an RPC connection.
101-
p.funcCaller = raw.(shared.IFuncCaller)
100+
p.funcCaller = raw.(fungo.IFuncCaller)
102101

103102
p.cachedFunctions = sync.Map{}
104103
logger.Info("load hashicorp go plugin success", "path", path)

init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import (
55
"fmt"
66
"path/filepath"
77

8-
"github.com/httprunner/funplugin/shared"
8+
"github.com/httprunner/funplugin/fungo"
99
)
1010

1111
var (
12-
logger = shared.Logger
12+
logger = fungo.Logger
1313
)
1414

1515
type IPlugin interface {

shared/interface.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)