-
Notifications
You must be signed in to change notification settings - Fork 8
/
hashicorp_plugin.go
189 lines (159 loc) · 4.59 KB
/
hashicorp_plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package funplugin
import (
"fmt"
"os"
"os/exec"
"sync"
"time"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
"github.com/pkg/errors"
"github.com/httprunner/funplugin/fungo"
)
type rpcType string
const (
rpcTypeRPC rpcType = "rpc" // go net/rpc
rpcTypeGRPC rpcType = "grpc" // default
)
func (t rpcType) String() string {
return string(t)
}
// hashicorpPlugin implements hashicorp/go-plugin
type hashicorpPlugin struct {
client *plugin.Client
rpcType rpcType
funcCaller fungo.IFuncCaller
cachedFunctions sync.Map // cache loaded functions to improve performance, key is function name, value is bool
path string // plugin file path
option *pluginOption
}
func newHashicorpPlugin(path string, option *pluginOption) (*hashicorpPlugin, error) {
p := &hashicorpPlugin{
path: path,
option: option,
}
// plugin type, grpc or rpc
p.rpcType = rpcType(os.Getenv(fungo.PluginTypeEnvName))
if p.rpcType != rpcTypeRPC {
p.rpcType = rpcTypeGRPC // default
}
// logger
logger = logger.ResetNamed(fmt.Sprintf("hc-%v-%v", p.rpcType, p.option.langType))
// 失败则继续尝试,连续三次失败则返回错误
err := p.startPlugin()
if err == nil {
return p, err
}
logger.Info("load hashicorp go plugin success", "path", path)
return nil, err
}
func (p *hashicorpPlugin) Type() string {
return fmt.Sprintf("hashicorp-%s-%v", p.rpcType, p.option.langType)
}
func (p *hashicorpPlugin) Path() string {
return p.path
}
func (p *hashicorpPlugin) Has(funcName string) bool {
logger.Debug("check if plugin has function", "funcName", funcName)
flag, ok := p.cachedFunctions.Load(funcName)
if ok {
return flag.(bool)
}
funcNames, err := p.funcCaller.GetNames()
if err != nil {
return false
}
for _, name := range funcNames {
if name == funcName {
p.cachedFunctions.Store(funcName, true) // cache as exists
return true
}
}
p.cachedFunctions.Store(funcName, false) // cache as not exists
return false
}
func (p *hashicorpPlugin) Call(funcName string, args ...interface{}) (interface{}, error) {
return p.funcCaller.Call(funcName, args...)
}
func (p *hashicorpPlugin) StartHeartbeat() {
ticker := time.NewTicker(15 * time.Second)
defer ticker.Stop()
var err error
for range ticker.C {
// Check the client connection status
logger.Info("heartbreak......")
if p.client.Exited() {
logger.Error(fmt.Sprintf("plugin exited, restarting..."))
err = p.startPlugin()
if err != nil {
break
}
}
}
}
func (p *hashicorpPlugin) startPlugin() error {
var cmd *exec.Cmd
if p.option.langType == langTypePython {
// hashicorp python plugin
cmd = exec.Command(p.option.python3, p.path)
// hashicorp python plugin only supports gRPC
p.rpcType = rpcTypeGRPC
} else {
// hashicorp go plugin
cmd = exec.Command(p.path)
// hashicorp go plugin supports grpc and rpc
p.rpcType = rpcType(os.Getenv(fungo.PluginTypeEnvName))
if p.rpcType != rpcTypeRPC {
p.rpcType = rpcTypeGRPC // default
}
}
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%s", fungo.PluginTypeEnvName, p.rpcType))
var err error
maxRetryCount := 3
for i := 0; i < maxRetryCount; i++ {
err = p.tryStartPlugin(cmd, logger)
if err == nil {
return nil
}
time.Sleep(time.Second * time.Duration(i*i)) // sleep temporarily before next try
}
logger.Error("failed to start plugin after max retries")
return errors.Wrap(err, "failed to start plugin after max retries")
}
func (p *hashicorpPlugin) tryStartPlugin(cmd *exec.Cmd, logger hclog.Logger) error {
// launch the plugin process
p.client = plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: fungo.HandshakeConfig,
Plugins: map[string]plugin.Plugin{
rpcTypeRPC.String(): &fungo.RPCPlugin{},
rpcTypeGRPC.String(): &fungo.GRPCPlugin{},
},
Cmd: cmd,
Logger: logger,
AllowedProtocols: []plugin.Protocol{
plugin.ProtocolNetRPC,
plugin.ProtocolGRPC,
},
})
// Connect via RPC/gRPC
rpcClient, err := p.client.Client()
if err != nil {
return errors.Wrap(err, fmt.Sprintf("connect %s plugin failed", p.rpcType))
}
// Request the plugin
raw, err := rpcClient.Dispense(p.rpcType.String())
if err != nil {
return errors.Wrap(err, fmt.Sprintf("request %s plugin failed", p.rpcType))
}
// We should have a Function now! This feels like a normal interface
// implementation but is in fact over an RPC connection.
p.funcCaller = raw.(fungo.IFuncCaller)
p.cachedFunctions = sync.Map{}
return nil
}
func (p *hashicorpPlugin) Quit() error {
// kill hashicorp plugin process
logger.Info("quit hashicorp plugin process")
p.client.Kill()
return fungo.CloseLogFile()
}