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
|
package events
import (
"fmt"
"strings"
"sync"
"github.com/spiral/errors"
)
type sub struct {
pattern string
w *wildcard
events chan<- Event
}
type eventsBus struct {
sync.RWMutex
subscribers sync.Map
internalEvCh chan Event
stop chan struct{}
}
func newEventsBus() *eventsBus {
return &eventsBus{
internalEvCh: make(chan Event, 100),
stop: make(chan struct{}),
}
}
/*
http.* <-
*/
// SubscribeAll for all RR events
// returns subscriptionID
func (eb *eventsBus) SubscribeAll(subID string, ch chan<- Event) error {
if ch == nil {
return errors.Str("nil channel provided")
}
subIDTr := strings.Trim(subID, " ")
if subIDTr == "" {
return errors.Str("subscriberID can't be empty")
}
return eb.subscribe(subID, "*", ch)
}
// SubscribeP pattern like "pluginName.EventType"
func (eb *eventsBus) SubscribeP(subID string, pattern string, ch chan<- Event) error {
if ch == nil {
return errors.Str("nil channel provided")
}
subIDTr := strings.Trim(subID, " ")
patternTr := strings.Trim(pattern, " ")
if subIDTr == "" || patternTr == "" {
return errors.Str("subscriberID or pattern can't be empty")
}
return eb.subscribe(subID, pattern, ch)
}
func (eb *eventsBus) Unsubscribe(subID string) {
eb.subscribers.Delete(subID)
}
func (eb *eventsBus) UnsubscribeP(subID, pattern string) {
if sb, ok := eb.subscribers.Load(subID); ok {
eb.Lock()
defer eb.Unlock()
sbArr := sb.([]*sub)
for i := 0; i < len(sbArr); i++ {
if sbArr[i].pattern == pattern {
sbArr[i] = sbArr[len(sbArr)-1]
sbArr = sbArr[:len(sbArr)-1]
// replace with new array
eb.subscribers.Store(subID, sbArr)
return
}
}
}
}
// Send sends event to the events bus
func (eb *eventsBus) Send(ev Event) {
// do not accept nil events
if ev == nil {
return
}
eb.internalEvCh <- ev
}
func (eb *eventsBus) Len() uint {
var ln uint
eb.subscribers.Range(func(key, value interface{}) bool {
ln++
return true
})
return ln
}
func (eb *eventsBus) subscribe(subID string, pattern string, ch chan<- Event) error {
eb.Lock()
defer eb.Unlock()
w, err := newWildcard(pattern)
if err != nil {
return err
}
if sb, ok := eb.subscribers.Load(subID); ok {
// at this point we are confident that sb is a []*sub type
subArr := sb.([]*sub)
subArr = append(subArr, &sub{
pattern: pattern,
w: w,
events: ch,
})
eb.subscribers.Store(subID, subArr)
return nil
}
subArr := make([]*sub, 0, 5)
subArr = append(subArr, &sub{
pattern: pattern,
w: w,
events: ch,
})
eb.subscribers.Store(subID, subArr)
return nil
}
func (eb *eventsBus) handleEvents() {
for { //nolint:gosimple
select {
case ev := <-eb.internalEvCh:
// http.WorkerError for example
wc := fmt.Sprintf("%s.%s", ev.Plugin(), ev.Type().String())
eb.subscribers.Range(func(key, value interface{}) bool {
vsub := value.([]*sub)
for i := 0; i < len(vsub); i++ {
if vsub[i].w.match(wc) {
select {
case vsub[i].events <- ev:
return true
default:
return true
}
}
}
return true
})
}
}
}
|