-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
206 lines (175 loc) · 6.07 KB
/
memory.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
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
package emitter
import (
"fmt"
"sync"
"sync/atomic"
)
// MemoryEmitter is an in-memory implementation of the Emitter interface. It provides
// facilities for adding and removing listeners, emitting events, and configuring
// the behavior of event handling within the application.
type MemoryEmitter struct {
topics sync.Map // Stores topics with concurrent access support.
errorHandler func(Event, error) error // Handles errors that occur during event handling.
idGenerator func() string // Generates unique IDs for listeners.
panicHandler PanicHandler // Handles panics that occur during event handling.
Pool Pool // Manages concurrent execution of event handlers.
closed atomic.Value // Indicates whether the emitter is closed.
errChanBufferSize int // Size of the buffer for the error channel in Emit.
}
// NewMemoryEmitter initializes a new MemoryEmitter with optional configuration options.
// Default configurations are applied, which can be overridden by the provided options.
func NewMemoryEmitter(opts ...EmitterOption) *MemoryEmitter {
m := &MemoryEmitter{
topics: sync.Map{},
errorHandler: DefaultErrorHandler,
idGenerator: DefaultIDGenerator,
panicHandler: DefaultPanicHandler,
errChanBufferSize: 10,
}
m.closed.Store(false)
// Apply each provided option to the emitter to configure it.
for _, opt := range opts {
opt(m)
}
return m
}
// On subscribes a listener to a topic with the given name. Listener options can be specified
// to configure the listener's behavior. It returns a unique ID for the listener and an error, if any.
func (m *MemoryEmitter) On(topicName string, listener Listener, opts ...ListenerOption) (string, error) {
if listener == nil {
return "", ErrNilListener
}
if !isValidTopicName(topicName) {
return "", ErrInvalidTopicName
}
topic := m.EnsureTopic(topicName)
listenerID := m.idGenerator()
topic.AddListener(listenerID, listener, opts...)
return listenerID, nil
}
// Off unsubscribes a listener from a topic using the listener's unique ID. It returns
// an error if the listener cannot be found or if there is a problem with unsubscribing.
func (m *MemoryEmitter) Off(topicName string, listenerID string) error {
topic, err := m.GetTopic(topicName)
if err != nil {
return err
}
return topic.RemoveListener(listenerID)
}
// Emit asynchronously dispatches an event to all the subscribers of the event's topic.
// It returns a channel that will receive any errors encountered during event handling.
func (m *MemoryEmitter) Emit(eventName string, payload interface{}) <-chan error {
errChan := make(chan error, m.errChanBufferSize)
// Before starting new goroutine, check if Emitter is closed
if m.closed.Load().(bool) {
errChan <- ErrEmitterClosed
close(errChan)
return errChan
}
if m.Pool != nil {
m.Pool.Submit(func() {
defer close(errChan)
m.handleEvents(eventName, payload, func(err error) {
errChan <- err
})
})
} else {
go func() {
defer close(errChan)
m.handleEvents(eventName, payload, func(err error) {
errChan <- err
})
}()
}
return errChan
}
// EmitSync dispatches an event synchronously to all subscribers of the event's topic and
// collects any errors that occurred. This method will block until all notifications are completed.
func (m *MemoryEmitter) EmitSync(eventName string, payload interface{}) []error {
if m.closed.Load().(bool) {
return []error{ErrEmitterClosed}
}
var errs []error
m.handleEvents(eventName, payload, func(err error) {
errs = append(errs, err)
})
return errs
}
// handleEvents is an internal method that processes an event and notifies all
// registered listeners. It takes care of error handling and panic recovery.
func (m *MemoryEmitter) handleEvents(topicName string, payload interface{}, errorHandler func(error)) {
defer func() {
if r := recover(); r != nil && m.panicHandler != nil {
m.panicHandler(r)
}
}()
event := NewBaseEvent(topicName, payload)
m.topics.Range(func(key, value interface{}) bool {
topicPattern := key.(string)
if matchTopicPattern(topicPattern, topicName) {
topic := value.(*Topic)
topicErrors := topic.Trigger(event)
for _, err := range topicErrors {
if m.errorHandler != nil {
err = m.errorHandler(event, err)
}
if err != nil {
errorHandler(err)
}
}
}
return true
})
}
// GetTopic retrieves a topic by its name. If the topic does not exist, it returns an error.
func (m *MemoryEmitter) GetTopic(topicName string) (*Topic, error) {
topic, ok := m.topics.Load(topicName)
if !ok {
return nil, fmt.Errorf("%w: unable to find topic '%s'", ErrTopicNotFound, topicName)
}
return topic.(*Topic), nil
}
// EnsureTopic retrieves or creates a new topic by its name. If the topic does not
// exist, it is created and returned. This ensures that a topic is always available.
func (m *MemoryEmitter) EnsureTopic(topicName string) *Topic {
topic, _ := m.topics.LoadOrStore(topicName, NewTopic())
return topic.(*Topic)
}
func (m *MemoryEmitter) SetErrorHandler(handler func(Event, error) error) {
if handler != nil {
m.errorHandler = handler
}
}
func (m *MemoryEmitter) SetIDGenerator(generator func() string) {
if generator != nil {
m.idGenerator = generator
}
}
func (m *MemoryEmitter) SetPool(pool Pool) {
m.Pool = pool
}
func (m *MemoryEmitter) SetPanicHandler(panicHandler PanicHandler) {
if panicHandler != nil {
m.panicHandler = panicHandler
}
}
func (m *MemoryEmitter) SetErrChanBufferSize(size int) {
m.errChanBufferSize = size
}
// Close terminates the emitter, ensuring all pending events are processed. It performs cleanup
// and releases resources. Calling Close on an already closed emitter will result in an error.
func (m *MemoryEmitter) Close() error {
if m.closed.Load().(bool) {
return ErrEmitterAlreadyClosed
}
m.closed.Store(true)
// Perform cleanup operations
m.topics.Range(func(key, value interface{}) bool {
m.topics.Delete(key)
return true
})
if m.Pool != nil {
m.Pool.Release()
}
return nil
}