-
Notifications
You must be signed in to change notification settings - Fork 173
/
registry.go
86 lines (70 loc) · 1.97 KB
/
registry.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
package registry
import (
"fmt"
"reflect"
"sync"
"github.com/juju/errors"
log "github.com/sirupsen/logrus"
)
type PluginType string
const (
InputPlugin PluginType = "input"
PositionRepo PluginType = "positionRepo"
OutputPlugin PluginType = "output"
FilterPlugin PluginType = "filters"
MatcherPlugin PluginType = "matcher"
SchedulerPlugin PluginType = "scheduler"
SQLExecutionEnginePlugin PluginType = "sqlExecutionEngine"
)
type Plugin interface {
Configure(pipelineName string, data map[string]interface{}) error
}
type PluginFactory func() Plugin
var registry map[PluginType]map[string]PluginFactory
var mutex sync.Mutex
func RegisterPluginFactory(pluginType PluginType, name string, v PluginFactory) {
mutex.Lock()
defer mutex.Unlock()
log.Debugf("[RegisterPlugin] type: %v, name: %v", pluginType, name)
if registry == nil {
registry = make(map[PluginType]map[string]PluginFactory)
}
_, ok := registry[pluginType]
if !ok {
registry[pluginType] = make(map[string]PluginFactory)
}
_, ok = registry[pluginType][name]
if ok {
panic(fmt.Sprintf("plugin already exists, type: %v, name: %v", pluginType, name))
}
registry[pluginType][name] = v
}
func RegisterPlugin(pluginType PluginType, name string, v Plugin, singleton bool) {
var pf PluginFactory
if singleton {
pf = func() Plugin {
return v
}
} else {
pf = func() Plugin {
return reflect.New(reflect.TypeOf(v).Elem()).Interface().(Plugin)
}
}
RegisterPluginFactory(pluginType, name, pf)
}
func GetPlugin(pluginType PluginType, name string) (Plugin, error) {
mutex.Lock()
defer mutex.Unlock()
if registry == nil {
return nil, errors.Errorf("empty registry")
}
plugins, ok := registry[pluginType]
if !ok {
return nil, errors.Errorf("empty plugin type: %v, name: %v", pluginType, name)
}
p, ok := plugins[name]
if !ok {
return nil, errors.Errorf("empty plugin, type: %v, name: %v", pluginType, name)
}
return p(), nil
}