-
Notifications
You must be signed in to change notification settings - Fork 19
/
processor.go
312 lines (265 loc) · 9.36 KB
/
processor.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
/****************************************************************************
* Copyright 2019, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/
// Package event //
package event
import (
"errors"
"fmt"
"sync"
"time"
"github.com/optimizely/go-sdk/pkg/logging"
"github.com/optimizely/go-sdk/pkg/notification"
"github.com/optimizely/go-sdk/pkg/registry"
"github.com/optimizely/go-sdk/pkg/utils"
)
// Processor processes events
type Processor interface {
ProcessEvent(event UserEvent)
}
// BatchEventProcessor is used out of the box by the SDK
type BatchEventProcessor struct {
sdkKey string
MaxQueueSize int // max size of the queue before flush
FlushInterval time.Duration // in milliseconds
BatchSize int
Q Queue
Mux sync.Mutex
Ticker *time.Ticker
EventDispatcher Dispatcher
}
// DefaultBatchSize holds the default value for the batch size
const DefaultBatchSize = 10
// DefaultEventQueueSize holds the default value for the event queue size
const DefaultEventQueueSize = 100
// DefaultEventFlushInterval holds the default value for the event flush interval
const DefaultEventFlushInterval = 30 * time.Second
var pLogger = logging.GetLogger("EventProcessor")
// BPOptionConfig is the BatchProcessor options that give you the ability to add one more more options before the processor is initialized.
type BPOptionConfig func(qp *BatchEventProcessor)
// WithBatchSize sets the batch size as a config option to be passed into the NewProcessor method
func WithBatchSize(bsize int) BPOptionConfig {
return func(qp *BatchEventProcessor) {
qp.BatchSize = bsize
}
}
// WithQueueSize sets the queue size as a config option to be passed into the NewProcessor method
func WithQueueSize(qsize int) BPOptionConfig {
return func(qp *BatchEventProcessor) {
qp.MaxQueueSize = qsize
}
}
// WithFlushInterval sets the flush interval as a config option to be passed into the NewProcessor method
func WithFlushInterval(flushInterval time.Duration) BPOptionConfig {
return func(qp *BatchEventProcessor) {
qp.FlushInterval = flushInterval
}
}
// WithQueue sets the Processor Queue as a config option to be passed into the NewProcessor method
func WithQueue(q Queue) BPOptionConfig {
return func(qp *BatchEventProcessor) {
qp.Q = q
}
}
// WithEventDispatcher sets the Processor Dispatcher as a config option to be passed into the NewProcessor method
func WithEventDispatcher(d Dispatcher) BPOptionConfig {
return func(qp *BatchEventProcessor) {
qp.EventDispatcher = d
}
}
// WithSDKKey sets the SDKKey used to register for notifications. This should be removed when the project
// config supports sdk key.
func WithSDKKey(sdkKey string) BPOptionConfig {
return func(qp *BatchEventProcessor) {
qp.sdkKey = sdkKey
}
}
// NewBatchEventProcessor returns a new instance of BatchEventProcessor with queueSize and flushInterval
func NewBatchEventProcessor(options ...BPOptionConfig) *BatchEventProcessor {
p := &BatchEventProcessor{}
for _, opt := range options {
opt(p)
}
if p.MaxQueueSize == 0 {
p.MaxQueueSize = defaultQueueSize
}
if p.FlushInterval == 0 {
p.FlushInterval = DefaultEventFlushInterval
}
if p.BatchSize == 0 {
p.BatchSize = DefaultBatchSize
}
if p.Q == nil {
p.Q = NewInMemoryQueue(p.MaxQueueSize)
}
return p
}
// Start initializes the event processor
func (p *BatchEventProcessor) Start(exeCtx utils.ExecutionCtx) {
if p.EventDispatcher == nil {
p.EventDispatcher = NewQueueEventDispatcher(exeCtx.GetContext())
}
p.startTicker(exeCtx)
pLogger.Debug("Batch event processor started")
}
// ProcessEvent processes the given impression event
func (p *BatchEventProcessor) ProcessEvent(event UserEvent) {
p.Q.Add(event)
pLogger.Debug(fmt.Sprintf("ProcessEvent: %s", event.UUID))
pLogger.Debug(fmt.Sprintf("Current Size: %d, BatchSize: %d, MaxQueueSize: %d", p.Q.Size(), p.BatchSize, p.MaxQueueSize))
if p.Q.Size() >= p.BatchSize {
go func() {
p.FlushEvents()
}()
}
}
// EventsCount returns size of an event queue
func (p *BatchEventProcessor) EventsCount() int {
return p.Q.Size()
}
// GetEvents returns events from event queue for count
func (p *BatchEventProcessor) GetEvents(count int) []interface{} {
return p.Q.Get(count)
}
// Remove removes events from queue for count
func (p *BatchEventProcessor) Remove(count int) []interface{} {
return p.Q.Remove(count)
}
// StartTicker starts new ticker for flushing events
func (p *BatchEventProcessor) startTicker(exeCtx utils.ExecutionCtx) {
if p.Ticker != nil {
return
}
p.Ticker = time.NewTicker(p.FlushInterval)
wg := exeCtx.GetWaitSync()
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-p.Ticker.C:
pLogger.Debug("Ticker stuff.")
p.FlushEvents()
case <-exeCtx.GetContext().Done():
pLogger.Debug("Event processor stopped, flushing events.")
p.FlushEvents()
d, ok := p.EventDispatcher.(*QueueEventDispatcher)
if ok {
d.flushEvents()
}
return
}
}
}()
}
// check if user event can be batched in the current batch
func (p *BatchEventProcessor) canBatch(current *Batch, user UserEvent) bool {
if current.ProjectID == user.EventContext.ProjectID &&
current.Revision == user.EventContext.Revision {
return true
}
return false
}
// add the visitor to the current batch
func (p *BatchEventProcessor) addToBatch(current *Batch, visitor Visitor) {
visitors := append(current.Visitors, visitor)
current.Visitors = visitors
}
// FlushEvents flushes events in queue
func (p *BatchEventProcessor) FlushEvents() {
// we flush when queue size is reached.
// however, if there is a ticker cycle already processing, we should wait
p.Mux.Lock()
defer p.Mux.Unlock()
pLogger.Debug(fmt.Sprintf("Flushing events. Size: %d", p.EventsCount()))
var batchEvent Batch
var batchEventCount = 0
var failedToSend = false
for p.EventsCount() > 0 {
if failedToSend {
pLogger.Error("last Event Batch failed to send; retry on next flush", errors.New("dispatcher failed"))
break
}
events := p.GetEvents(p.BatchSize)
if len(events) > 0 {
for i := 0; i < len(events); i++ {
userEvent, ok := events[i].(UserEvent)
if ok {
if batchEventCount == 0 {
batchEvent = createBatchEvent(userEvent, createVisitorFromUserEvent(userEvent))
batchEventCount = 1
} else {
if !p.canBatch(&batchEvent, userEvent) {
// this could happen if the project config was updated for instance.
pLogger.Info("Can't batch last event. Sending current batch.")
break
} else {
p.addToBatch(&batchEvent, createVisitorFromUserEvent(userEvent))
batchEventCount++
}
}
if batchEventCount >= p.BatchSize {
// the batch size is reached so take the current batchEvent and send it.
break
}
}
}
}
if batchEventCount > 0 {
// TODO: figure out what to do with the error
logEvent := createLogEvent(batchEvent)
notificationCenter := registry.GetNotificationCenter(p.sdkKey)
err := notificationCenter.Send(notification.LogEvent, logEvent)
if err != nil {
pLogger.Error("Send Log Event notification failed.", err)
}
if success, _ := p.EventDispatcher.DispatchEvent(logEvent); success {
pLogger.Debug("Dispatched event successfully")
p.Remove(batchEventCount)
batchEventCount = 0
batchEvent = Batch{}
} else {
pLogger.Warning("Failed to dispatch event successfully")
failedToSend = true
}
}
}
}
// OnEventDispatch registers a handler for LogEvent notifications
func (p *BatchEventProcessor) OnEventDispatch(callback func(logEvent LogEvent)) (int, error) {
notificationCenter := registry.GetNotificationCenter(p.sdkKey)
handler := func(payload interface{}) {
if ev, ok := payload.(LogEvent); ok {
callback(ev)
} else {
pLogger.Warning(fmt.Sprintf("Unable to convert notification payload %v into LogEventNotification", payload))
}
}
id, err := notificationCenter.AddHandler(notification.LogEvent, handler)
if err != nil {
pLogger.Error("Problem with adding notification handler.", err)
return 0, err
}
return id, nil
}
// RemoveOnEventDispatch removes handler for LogEvent notification with given id
func (p *BatchEventProcessor) RemoveOnEventDispatch(id int) error {
notificationCenter := registry.GetNotificationCenter(p.sdkKey)
if err := notificationCenter.RemoveHandler(id, notification.LogEvent); err != nil {
pLogger.Warning("Problem with removing notification handler.")
return err
}
return nil
}