-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathmessage_pipe.go
More file actions
211 lines (170 loc) · 4.38 KB
/
message_pipe.go
File metadata and controls
211 lines (170 loc) · 4.38 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
// Copyright (c) F5, Inc.
//
// This source code is licensed under the Apache License, Version 2.0 license found in the
// LICENSE file in the root directory of this source tree.
package bus
import (
"context"
"log/slog"
"sync"
messagebus "github.com/vardius/message-bus"
)
type (
Payload interface{}
Message struct {
Data Payload
Topic string
}
MessageWithContext struct {
ctx context.Context
message *Message
}
Info struct {
Name string
}
MessagePipeInterface interface {
Register(size int, plugins []Plugin) error
DeRegister(ctx context.Context, plugins []string) error
Process(ctx context.Context, messages ...*Message)
Run(ctx context.Context)
Plugins() []Plugin
IsPluginRegistered(pluginName string) bool
}
Plugin interface {
Init(ctx context.Context, messagePipe MessagePipeInterface) error
Close(ctx context.Context) error
Info() *Info
Process(ctx context.Context, msg *Message)
Subscriptions() []string
}
MessagePipe struct {
bus messagebus.MessageBus
messageChannel chan *MessageWithContext
plugins []Plugin
pluginsMutex sync.Mutex
}
)
func NewMessagePipe(size int) *MessagePipe {
return &MessagePipe{
messageChannel: make(chan *MessageWithContext, size),
pluginsMutex: sync.Mutex{},
}
}
func (p *MessagePipe) Register(size int, plugins []Plugin) error {
p.pluginsMutex.Lock()
defer p.pluginsMutex.Unlock()
p.plugins = append(p.plugins, plugins...)
p.bus = messagebus.New(size)
pluginsRegistered := []string{}
for _, plugin := range p.plugins {
for _, subscription := range plugin.Subscriptions() {
err := p.bus.Subscribe(subscription, plugin.Process)
if err != nil {
return err
}
}
pluginsRegistered = append(pluginsRegistered, plugin.Info().Name)
}
slog.Info("Finished registering plugins", "plugins", pluginsRegistered)
return nil
}
func (p *MessagePipe) DeRegister(ctx context.Context, pluginNames []string) error {
p.pluginsMutex.Lock()
defer p.pluginsMutex.Unlock()
plugins := p.findPlugins(pluginNames)
for _, plugin := range plugins {
index := p.Index(plugin.Info().Name, p.plugins)
err := p.unsubscribePlugin(ctx, index, plugin)
if err != nil {
return err
}
}
return nil
}
func (p *MessagePipe) Process(ctx context.Context, messages ...*Message) {
for _, message := range messages {
p.messageChannel <- &MessageWithContext{ctx, message}
}
}
func (p *MessagePipe) Run(ctx context.Context) {
p.pluginsMutex.Lock()
p.initPlugins(ctx)
p.pluginsMutex.Unlock()
for {
select {
case <-ctx.Done():
p.pluginsMutex.Lock()
for _, r := range p.plugins {
r.Close(ctx)
}
p.pluginsMutex.Unlock()
return
case m := <-p.messageChannel:
p.bus.Publish(m.message.Topic, m.ctx, m.message)
}
}
}
func (p *MessagePipe) Plugins() []Plugin {
return p.plugins
}
func (p *MessagePipe) IsPluginRegistered(pluginName string) bool {
isPluginRegistered := false
for _, plugin := range p.Plugins() {
if plugin.Info().Name == pluginName {
isPluginRegistered = true
}
}
return isPluginRegistered
}
func (p *MessagePipe) Index(pluginName string, plugins []Plugin) int {
for index, plugin := range plugins {
if pluginName == plugin.Info().Name {
return index
}
}
return -1
}
func (p *MessagePipe) unsubscribePlugin(ctx context.Context, index int, plugin Plugin) error {
if index != -1 {
p.plugins = append(p.plugins[:index], p.plugins[index+1:]...)
err := plugin.Close(ctx)
if err != nil {
return err
}
for _, subscription := range plugin.Subscriptions() {
unsubErr := p.bus.Unsubscribe(subscription, plugin.Process)
if unsubErr != nil {
return unsubErr
}
}
}
return nil
}
func (p *MessagePipe) findPlugins(pluginNames []string) []Plugin {
var plugins []Plugin
for _, name := range pluginNames {
for _, plugin := range p.plugins {
if plugin.Info().Name == name {
plugins = append(plugins, plugin)
}
}
}
return plugins
}
func (p *MessagePipe) initPlugins(ctx context.Context) {
for index, plugin := range p.plugins {
err := plugin.Init(ctx, p)
if err != nil {
slog.ErrorContext(ctx, "Failed to initialize plugin", "plugin", plugin.Info().Name, "error", err)
unsubscribeError := p.unsubscribePlugin(ctx, index, plugin)
if unsubscribeError != nil {
slog.ErrorContext(
ctx,
"Failed to unsubscribe plugin",
"plugin", plugin.Info().Name,
"error", unsubscribeError,
)
}
}
}
}