-
Notifications
You must be signed in to change notification settings - Fork 33
/
event.go
336 lines (287 loc) · 11.3 KB
/
event.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// Source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package aah
import (
"sort"
"sync"
"aahframe.work/essentials"
"aahframe.work/internal/util"
)
const (
// EventOnInit is published once the aah.AppConfig() is loaded. At this stage,
// only aah.conf config is initialized. App Variables, Routes, i18n, Security,
// View Engine, Logs and so on will be initialized after this event.
EventOnInit = "OnInit"
// EventOnStart is published just before the start of aah Server.
// The application is completely initialized at this stage. The server
// is yet to be started.
EventOnStart = "OnStart"
// EventOnPreShutdown is published when application receives OS Signals
// `SIGINT` or `SIGTERM` and before the triggering graceful shutdown. After this
// event, aah triggers graceful shutdown with config value of
// `server.timeout.grace_shutdown`.
EventOnPreShutdown = "OnPreShutdown"
// EventOnPostShutdown is published just after the successful grace shutdown
// of aah server and then application does clean exit.
EventOnPostShutdown = "OnPostShutdown"
// EventOnConfigHotReload is published just after aah application internal config
// hot-reload and re-initialize completes without an error otherwise it won't be
// published. It happens when application receives the signal based on
// config `runtime.config_hotreload.signal`.
EventOnConfigHotReload = "OnConfigHotReload"
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// HTTP Engine events
//______________________________________________________________________________
// EventOnRequest is published on each incoming request to the aah server.
EventOnRequest = "OnRequest"
// EventOnPreReply is published just before writing a reply/response on the wire.
// At this point, the response writer is clean. i.e. Headers, Cookies, Redirects,
// Status Code and Response Body are not written. event is published when
// before server writes the reply on the wire.
//
// Except when
//
// 1) `Reply().Done()`,
//
// 2) `Reply().Redirect(...)` is called.
//
// Refer `aah.Reply().Done()` godoc for more info.
EventOnPreReply = "OnPreReply"
// EventOnHeaderReply is published before writing HTTP header Status.
// At this point, all the headers except the header Status get written on
// the http.ResponseWriter.
//
// Except when
//
// 1) `Reply().Done()`,
//
// 2) `Reply().Redirect(...)` is called.
//
// Refer `aah.Reply().Done()` godoc for more info.
EventOnHeaderReply = "OnHeaderReply"
// EventOnPostReply is published right after the response gets written on the
// wire. We can do nothing about the response, however the context has valuable
// information such as response bytes size, response status code, etc.
//
// Except when
//
// 1) `Reply().Done()`,
//
// 2) `Reply().Redirect(...)` is called.
//
// Refer `aah.Reply().Done()` godoc for more info.
EventOnPostReply = "OnPostReply"
// EventOnPreAuth is published just before the Authentication and Authorization.
EventOnPreAuth = "OnPreAuth"
// EventOnPostAuth is published once the Authentication and Authorization
// info gets populated into Subject.
EventOnPostAuth = "OnPostAuth"
)
type (
// Event type holds the details of single event.
Event struct {
Name string
Data interface{}
}
// EventCallback type is store particular callback in priority for calling sequance.
EventCallback struct {
Callback EventCallbackFunc
CallOnce bool
published bool
priority int
}
// EventCallbacks type is slice of `EventCallback` type.
EventCallbacks []EventCallback
// EventCallbackFunc is signature of event callback function.
EventCallbackFunc func(e *Event)
)
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// app event methods
//______________________________________________________________________________
// OnInit method is to subscribe to aah application `OnInit` event. `OnInit`
// event published right after the aah application configuration `aah.conf`
// initialized.
func (a *Application) OnInit(ecb EventCallbackFunc, priority ...int) {
a.subcribeAppEvent(EventOnInit, ecb, priority)
}
// OnStart method is to subscribe to aah application `OnStart` event. `OnStart`
// event pubished right before the aah server starts listening to the request.
func (a *Application) OnStart(ecb EventCallbackFunc, priority ...int) {
a.subcribeAppEvent(EventOnStart, ecb, priority)
}
// OnPreShutdown method is to subscribe to aah application `OnPreShutdown` event.
// `OnPreShutdown` event pubished right before the triggering aah server graceful
// shutdown.
func (a *Application) OnPreShutdown(ecb EventCallbackFunc, priority ...int) {
a.subcribeAppEvent(EventOnPreShutdown, ecb, priority)
}
// OnPostShutdown method is to subscribe to aah application `OnPostShutdown` event.
// `OnPostShutdown` event pubished right the successful grace shutdown
// of aah server.
func (a *Application) OnPostShutdown(ecb EventCallbackFunc, priority ...int) {
a.subcribeAppEvent(EventOnPostShutdown, ecb, priority)
}
// OnConfigHotReload method is to subscribe to aah application `OnConfigHotReload` event.
// `OnConfigHotReload` is published just after aah application internal config
// hot-reload and re-initialize completes without an error otherwise it won't be
// published.
func (a *Application) OnConfigHotReload(ecb EventCallbackFunc, priority ...int) {
a.SubscribeEvent(EventOnConfigHotReload, EventCallback{
Callback: ecb,
priority: parsePriority(priority),
})
}
func (a *Application) subcribeAppEvent(eventName string, ecb EventCallbackFunc, priority []int) {
a.SubscribeEvent(eventName, EventCallback{
Callback: ecb,
CallOnce: true,
priority: parsePriority(priority),
})
}
// PublishEvent method publishes events to subscribed callbacks asynchronously.
// It means each subscribed callback executed via goroutine.
func (a *Application) PublishEvent(eventName string, data interface{}) {
a.eventStore.Publish(&Event{Name: eventName, Data: data})
}
// PublishEventSync method publishes events to subscribed callbacks
// synchronously.
func (a *Application) PublishEventSync(eventName string, data interface{}) {
a.eventStore.PublishSync(&Event{Name: eventName, Data: data})
}
// SubscribeEvent method is to subscribe to new or existing event.
func (a *Application) SubscribeEvent(eventName string, ec EventCallback) {
a.eventStore.Subscribe(eventName, ec)
}
// SubscribeEventFunc method is to subscribe to new or existing event
// by `EventCallbackFunc`.
func (a *Application) SubscribeEventFunc(eventName string, ecf EventCallbackFunc) {
a.eventStore.Subscribe(eventName, EventCallback{Callback: ecf})
}
// UnsubscribeEvent method is to unsubscribe by event name and `EventCallback`
// from app event store.
func (a *Application) UnsubscribeEvent(eventName string, ec EventCallback) {
a.UnsubscribeEventFunc(eventName, ec.Callback)
}
// UnsubscribeEventFunc method is to unsubscribe by event name and
// `EventCallbackFunc` from app event store.
func (a *Application) UnsubscribeEventFunc(eventName string, ecf EventCallbackFunc) {
a.eventStore.Unsubscribe(eventName, ecf)
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// EventStore
//______________________________________________________________________________
// EventStore type holds all the events belongs to aah application.
type EventStore struct {
a *Application
mu sync.RWMutex
subscribers map[string]EventCallbacks
}
// IsEventExists method returns true if given event is exists in the event store
// otherwise false.
func (es *EventStore) IsEventExists(eventName string) bool {
_, found := es.subscribers[eventName]
return found
}
// Publish method publishes events to subscribed callbacks asynchronously. It
// means each subscribed callback executed via goroutine.
func (es *EventStore) Publish(e *Event) {
if !es.IsEventExists(e.Name) {
return
}
es.a.Log().Debugf("Publishing event '%s' in asynchronous mode", e.Name)
wg := sync.WaitGroup{}
for idx, ec := range es.subscribers[e.Name] {
if ec.CallOnce {
if !ec.published {
wg.Add(1)
go func(w *sync.WaitGroup, event *Event, ecb EventCallbackFunc) {
defer w.Done()
ecb(event)
}(&wg, e, ec.Callback)
es.mu.Lock()
es.subscribers[e.Name][idx].published = true
es.mu.Unlock()
}
continue
}
wg.Add(1)
go func(w *sync.WaitGroup, event *Event, ecb EventCallbackFunc) {
defer w.Done()
ecb(event)
}(&wg, e, ec.Callback)
}
wg.Wait()
}
// PublishSync method publishes events to subscribed callbacks synchronously.
func (es *EventStore) PublishSync(e *Event) {
if !es.IsEventExists(e.Name) {
return
}
es.a.Log().Debugf("Publishing event '%s' in synchronous mode", e.Name)
for idx, ec := range es.subscribers[e.Name] {
if ec.CallOnce {
if !ec.published {
ec.Callback(e)
es.mu.Lock()
es.subscribers[e.Name][idx].published = true
es.mu.Unlock()
}
continue
}
ec.Callback(e)
}
}
// Subscribe method is to subscribe any event with event callback info.
func (es *EventStore) Subscribe(event string, ec EventCallback) {
es.mu.Lock()
defer es.mu.Unlock()
if es.IsEventExists(event) {
es.subscribers[event] = append(es.subscribers[event], ec)
return
}
es.subscribers[event] = EventCallbacks{}
es.subscribers[event] = append(es.subscribers[event], ec)
}
// Unsubscribe method is to unsubscribe any callback from event store by event.
func (es *EventStore) Unsubscribe(event string, callback EventCallbackFunc) {
es.mu.Lock()
defer es.mu.Unlock()
if !es.IsEventExists(event) {
es.a.Log().Warnf("Subscribers not exists for event: %s", event)
return
}
for idx := len(es.subscribers[event]) - 1; idx >= 0; idx-- {
ec := es.subscribers[event][idx]
if util.FuncEqual(ec.Callback, callback) {
es.subscribers[event] = append(es.subscribers[event][:idx], es.subscribers[event][idx+1:]...)
es.a.Log().Debugf("Callback: %s, unsubscribed from event: %s", ess.GetFunctionInfo(callback).QualifiedName, event)
return
}
}
es.a.Log().Warnf("Given callback: %s, not found in eventStore for event: %s", ess.GetFunctionInfo(callback).QualifiedName, event)
}
// SubscriberCount method returns subscriber count for given event name.
func (es *EventStore) SubscriberCount(eventName string) int {
if subs, found := es.subscribers[eventName]; found {
return len(subs)
}
return 0
}
func (es *EventStore) sortEventSubscribers(eventName string) {
if es.IsEventExists(eventName) {
ec := es.subscribers[eventName]
sort.Slice(ec, func(i, j int) bool { return ec[i].priority < ec[j].priority })
}
}
func (es *EventStore) sortAndPublishSync(e *Event) {
es.sortEventSubscribers(e.Name)
es.PublishSync(e)
}
func parsePriority(priority []int) int {
pr := 1 // default priority is 1
if len(priority) > 0 && priority[0] > 0 {
pr = priority[0]
}
return pr
}