-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
212 lines (182 loc) · 4.95 KB
/
plugin.go
File metadata and controls
212 lines (182 loc) · 4.95 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package prefab
import (
"context"
"fmt"
)
// The base plugin interface.
type Plugin interface {
// Name of the plugin, used for querying and dependency resolution.
Name() string
}
// Implemented if plugin depends on other plugins.
type DependentPlugin interface {
// Deps returns the names for plugins which this plugin depends on.
Deps() []string
}
// Implemented if plugin has optional dependencies, which should be initialized
// before the plugin, but are not required.
type OptionalDependentPlugin interface {
// OptDeps returns the names for plugins which this plugin optionally depends on.
OptDeps() []string
}
// Implemented if the plugin needs to be initialized outside construction.
type InitializablePlugin interface {
// Init the plugin. Will be called in dependency order.
Init(ctx context.Context, r *Registry) error
}
// Implemented if the plugin needs to be shutdown.
type ShutdownPlugin interface {
// Shutdown the plugin.
Shutdown(ctx context.Context) error
}
// Registry manages plugins and their dependencies.
type Registry struct {
plugins map[string]Plugin
keys []string
initOrder []string // Track initialization order for proper shutdown
}
// Get a plugin.
func (r *Registry) Get(key string) Plugin {
if p, ok := r.plugins[key]; ok {
return p
}
return nil
}
// GetPlugin retrieves a plugin by type.
// Returns the typed plugin and a boolean indicating success.
//
// Example:
//
// if store, ok := GetPlugin[*storage.StoragePlugin](r); ok {
// // use store
// }
func GetPlugin[T Plugin](r *Registry) (T, bool) {
var zero T
if r.plugins == nil {
return zero, false
}
for _, p := range r.plugins {
if typed, ok := p.(T); ok {
return typed, true
}
}
return zero, false
}
// Register a plugin.
func (r *Registry) Register(plugin Plugin) {
if r.plugins == nil {
r.plugins = map[string]Plugin{}
}
n := plugin.Name()
r.plugins[n] = plugin
r.keys = append(r.keys, n)
}
// Init all plugins in the Registry. Plugins will be visited in dependency order.
func (r *Registry) Init(ctx context.Context) error {
if r.plugins == nil {
return nil
}
// Validate dependency graph first.
visiting := make(map[string]bool)
for _, key := range r.keys {
if err := r.validateDeps(key, visiting, true); err != nil {
return err
}
}
// Initialize plugins if graph is valid.
initialized := make(map[string]bool)
for _, key := range r.keys {
if err := r.initPlugin(ctx, key, initialized); err != nil {
return err
}
}
return nil
}
// Shutdown any plugins that implement the shutdown interface.
// Plugins are shut down in reverse initialization order to ensure that
// dependencies are still available when a plugin shuts down.
func (r *Registry) Shutdown(ctx context.Context) error {
if r.plugins == nil {
return nil
}
// Iterate in reverse initialization order
for i := len(r.initOrder) - 1; i >= 0; i-- {
key := r.initOrder[i]
if p, ok := r.plugins[key].(ShutdownPlugin); ok {
if err := p.Shutdown(ctx); err != nil {
return err
}
}
}
return nil
}
// Walks the plugin dependency graph and ensures that deps are registered and that
// there are no cycles.
func (r *Registry) validateDeps(key string, visiting map[string]bool, required bool) error {
if visiting[key] {
return fmt.Errorf("plugin: dependency cycle detected involving '%v'", key)
}
plugin, ok := r.plugins[key]
if !ok {
if !required {
return nil
}
// TODO: Add call graph to error message.
return fmt.Errorf("plugin: missing dependency, '%v' not registered", key)
}
if d, ok := plugin.(DependentPlugin); ok {
visiting[key] = true
for _, dep := range d.Deps() {
if err := r.validateDeps(dep, visiting, true); err != nil {
return err
}
}
delete(visiting, key)
}
if d, ok := plugin.(OptionalDependentPlugin); ok {
visiting[key] = true
for _, dep := range d.OptDeps() {
if err := r.validateDeps(dep, visiting, false); err != nil {
return err
}
}
delete(visiting, key)
}
return nil
}
// Ensures plugins are initialized in dependency order.
func (r *Registry) initPlugin(ctx context.Context, key string, initialized map[string]bool) error {
if initialized[key] {
return nil
}
plugin, ok := r.plugins[key]
if !ok {
return fmt.Errorf("plugin '%v' not registered", key)
}
// Initialize required dependencies first
if d, ok := plugin.(DependentPlugin); ok {
for _, dep := range d.Deps() {
if err := r.initPlugin(ctx, dep, initialized); err != nil {
return err
}
}
}
// Initialize optional dependencies if they are registered
if d, ok := plugin.(OptionalDependentPlugin); ok {
for _, dep := range d.OptDeps() {
if _, exists := r.plugins[dep]; exists {
if err := r.initPlugin(ctx, dep, initialized); err != nil {
return err
}
}
}
}
if p, ok := plugin.(InitializablePlugin); ok {
if err := p.Init(ctx, r); err != nil {
return fmt.Errorf("plugin: failed to initialize '%v': %w", key, err)
}
}
initialized[key] = true
r.initOrder = append(r.initOrder, key)
return nil
}