/
main.go
70 lines (55 loc) · 1.45 KB
/
main.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
package plugin
import (
"context"
"time"
"github.com/MXWXZ/skynet/sn"
"google.golang.org/grpc"
"github.com/MXWXZ/skynet/plugin/proto"
"github.com/hashicorp/go-plugin"
)
const magicKey = "SKYNET_PLUGIN"
const magicValue = "rainhurt"
//go:generate sh -c "protoc -I=./proto --go_out=. --go-grpc_out=. ./proto/*.proto"
var Handshake = plugin.HandshakeConfig{
ProtocolVersion: sn.ProtoVersion,
MagicCookieKey: magicKey,
MagicCookieValue: magicValue,
}
var PluginMap = func(timeout time.Duration) map[string]plugin.Plugin {
return map[string]plugin.Plugin{
"grpc": &PluginGRPCImpl{
SkynetTimeout: timeout,
},
}
}
type PluginError struct {
Code proto.ErrorCode
}
type PluginHelper interface {
Eval(str string) (string, error)
}
type PluginAPI interface {
Enable(h PluginHelper) (*PluginError, error)
Disable(h PluginHelper) (*PluginError, error)
}
type PluginGRPCImpl struct {
plugin.NetRPCUnsupportedPlugin
Impl PluginAPI
SkynetTimeout time.Duration
PluginTimeout time.Duration
}
func (p *PluginGRPCImpl) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
proto.RegisterPluginServer(s, &GRPCServer{
impl: p.Impl,
broker: broker,
timeout: p.PluginTimeout,
})
return nil
}
func (p *PluginGRPCImpl) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
return &GRPCClient{
client: proto.NewPluginClient(c),
broker: broker,
timeout: p.SkynetTimeout,
}, nil
}