forked from baron-chain/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreaming.go
79 lines (67 loc) · 1.86 KB
/
streaming.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
package streaming
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
streamingabci "cosmossdk.io/store/streaming/abci"
)
const pluginEnvKeyPrefix = "COSMOS_SDK"
// HandshakeMap contains a map of each supported streaming's handshake config
var HandshakeMap = map[string]plugin.HandshakeConfig{
"abci": streamingabci.Handshake,
}
// PluginMap contains a map of supported gRPC plugins
var PluginMap = map[string]plugin.Plugin{
"abci": &streamingabci.ListenerGRPCPlugin{},
}
func GetPluginEnvKey(name string) string {
return fmt.Sprintf("%s_%s", pluginEnvKeyPrefix, strings.ToUpper(name))
}
func NewStreamingPlugin(name, logLevel string) (interface{}, error) {
logger := hclog.New(&hclog.LoggerOptions{
Output: hclog.DefaultOutput,
Level: toHclogLevel(logLevel),
Name: fmt.Sprintf("plugin.%s", name),
})
// We're a host. Start by launching the streaming process.
env := os.Getenv(GetPluginEnvKey(name))
client := plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: HandshakeMap[name],
Managed: true,
Plugins: PluginMap,
// For verifying the integrity of executables see SecureConfig documentation
// https://pkg.go.dev/github.com/hashicorp/go-plugin#SecureConfig
//#nosec G204 -- Required to load plugins
Cmd: exec.Command("sh", "-c", env),
Logger: logger,
AllowedProtocols: []plugin.Protocol{
plugin.ProtocolNetRPC, plugin.ProtocolGRPC,
},
})
// Connect via RPC
rpcClient, err := client.Client()
if err != nil {
return nil, err
}
// Request streaming plugin
return rpcClient.Dispense(name)
}
func toHclogLevel(s string) hclog.Level {
switch s {
case "trace":
return hclog.Trace
case "debug":
return hclog.Debug
case "info":
return hclog.Info
case "warn":
return hclog.Warn
case "error":
return hclog.Error
default:
return hclog.DefaultLevel
}
}