Skip to content

Commit 7431ccf

Browse files
author
Michael Ng
authored
refac(ep): Rename options and EP implementation. (#141)
1 parent 3607db6 commit 7431ccf

File tree

6 files changed

+90
-85
lines changed

6 files changed

+90
-85
lines changed

pkg/client/factory.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ func (f OptimizelyFactory) Client(clientOptions ...OptionFunc) (*OptimizelyClien
4545
appClient := &OptimizelyClient{
4646
executionCtx: executionCtx,
4747
DecisionService: decision.NewCompositeService(f.SDKKey),
48-
EventProcessor: event.NewEventProcessor(event.BatchSize(event.DefaultBatchSize),
49-
event.QueueSize(event.DefaultEventQueueSize), event.FlushInterval(event.DefaultEventFlushInterval),
50-
event.SDKKey(f.SDKKey)),
48+
EventProcessor: event.NewBatchEventProcessor(event.WithBatchSize(event.DefaultBatchSize),
49+
event.WithQueueSize(event.DefaultEventQueueSize), event.WithFlushInterval(event.DefaultEventFlushInterval),
50+
event.WithSDKKey(f.SDKKey)),
5151
}
5252

5353
for _, opt := range clientOptions {
@@ -68,8 +68,8 @@ func (f OptimizelyFactory) Client(clientOptions ...OptionFunc) (*OptimizelyClien
6868
pollingConfigManager.Start(appClient.executionCtx)
6969
}
7070

71-
if queueingProcessor, ok := appClient.EventProcessor.(*event.QueueingEventProcessor); ok {
72-
queueingProcessor.Start(appClient.executionCtx)
71+
if batchProcessor, ok := appClient.EventProcessor.(*event.BatchEventProcessor); ok {
72+
batchProcessor.Start(appClient.executionCtx)
7373
}
7474

7575
return appClient, nil
@@ -115,8 +115,8 @@ func WithDecisionService(decisionService decision.Service) OptionFunc {
115115
// WithBatchEventProcessor sets event processor on a client
116116
func WithBatchEventProcessor(batchSize, queueSize int, flushInterval time.Duration) OptionFunc {
117117
return func(f *OptimizelyClient) {
118-
f.EventProcessor = event.NewEventProcessor(event.BatchSize(batchSize),
119-
event.QueueSize(queueSize), event.FlushInterval(flushInterval))
118+
f.EventProcessor = event.NewBatchEventProcessor(event.WithBatchSize(batchSize),
119+
event.WithQueueSize(queueSize), event.WithFlushInterval(flushInterval))
120120
}
121121
}
122122

pkg/client/factory_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func TestClientWithDecisionServiceAndEventProcessorInOptions(t *testing.T) {
107107
projectConfig := datafileprojectconfig.DatafileProjectConfig{}
108108
configManager := config.NewStaticProjectConfigManager(projectConfig)
109109
decisionService := new(MockDecisionService)
110-
processor := &event.QueueingEventProcessor{
110+
processor := &event.BatchEventProcessor{
111111
MaxQueueSize: 100,
112112
FlushInterval: 100,
113113
Q: event.NewInMemoryQueue(100),

pkg/event/factory_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func TestCreateAndSendImpressionEvent(t *testing.T) {
121121

122122
impressionUserEvent := BuildTestImpressionEvent()
123123

124-
processor := NewEventProcessor(BatchSize(10), QueueSize(100), FlushInterval(100))
124+
processor := NewBatchEventProcessor(WithBatchSize(10), WithQueueSize(100), WithFlushInterval(100))
125125

126126
processor.Start(utils.NewCancelableExecutionCtx())
127127

@@ -138,7 +138,7 @@ func TestCreateAndSendConversionEvent(t *testing.T) {
138138

139139
conversionUserEvent := BuildTestConversionEvent()
140140

141-
processor := NewEventProcessor(FlushInterval(100))
141+
processor := NewBatchEventProcessor(WithFlushInterval(100))
142142

143143
processor.Start(utils.NewCancelableExecutionCtx())
144144

pkg/event/processor.go

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,23 @@ package event
2020
import (
2121
"errors"
2222
"fmt"
23+
"sync"
24+
"time"
25+
2326
"github.com/optimizely/go-sdk/pkg/logging"
2427
"github.com/optimizely/go-sdk/pkg/notification"
2528
"github.com/optimizely/go-sdk/pkg/registry"
2629
"github.com/optimizely/go-sdk/pkg/utils"
27-
"sync"
28-
"time"
2930
)
3031

3132
// Processor processes events
3233
type Processor interface {
3334
ProcessEvent(event UserEvent)
3435
}
3536

36-
// QueueingEventProcessor is used out of the box by the SDK
37-
type QueueingEventProcessor struct {
38-
sdkKey string
37+
// BatchEventProcessor is used out of the box by the SDK
38+
type BatchEventProcessor struct {
39+
sdkKey string
3940
MaxQueueSize int // max size of the queue before flush
4041
FlushInterval time.Duration // in milliseconds
4142
BatchSize int
@@ -56,55 +57,55 @@ const DefaultEventFlushInterval = 30 * time.Second
5657

5758
var pLogger = logging.GetLogger("EventProcessor")
5859

59-
// QPConfigOption is the QueuingProcessor options that give you the ability to add one more more options before the processor is initialized.
60-
type QPConfigOption func(qp *QueueingEventProcessor)
60+
// BPOptionConfig is the BatchProcessor options that give you the ability to add one more more options before the processor is initialized.
61+
type BPOptionConfig func(qp *BatchEventProcessor)
6162

62-
// BatchSize sets the batch size as a config option to be passed into the NewProcessor method
63-
func BatchSize(bsize int) QPConfigOption {
64-
return func(qp *QueueingEventProcessor) {
63+
// WithBatchSize sets the batch size as a config option to be passed into the NewProcessor method
64+
func WithBatchSize(bsize int) BPOptionConfig {
65+
return func(qp *BatchEventProcessor) {
6566
qp.BatchSize = bsize
6667
}
6768
}
6869

69-
// QueueSize sets the queue size as a config option to be passed into the NewProcessor method
70-
func QueueSize(qsize int) QPConfigOption {
71-
return func(qp *QueueingEventProcessor) {
70+
// WithQueueSize sets the queue size as a config option to be passed into the NewProcessor method
71+
func WithQueueSize(qsize int) BPOptionConfig {
72+
return func(qp *BatchEventProcessor) {
7273
qp.MaxQueueSize = qsize
7374
}
7475
}
7576

76-
// FlushInterval sets the flush interval as a config option to be passed into the NewProcessor method
77-
func FlushInterval(flushInterval time.Duration) QPConfigOption {
78-
return func(qp *QueueingEventProcessor) {
77+
// WithFlushInterval sets the flush interval as a config option to be passed into the NewProcessor method
78+
func WithFlushInterval(flushInterval time.Duration) BPOptionConfig {
79+
return func(qp *BatchEventProcessor) {
7980
qp.FlushInterval = flushInterval
8081
}
8182
}
8283

83-
// PQ sets the Processor Queue as a config option to be passed into the NewProcessor method
84-
func PQ(q Queue) QPConfigOption {
85-
return func(qp *QueueingEventProcessor) {
84+
// WithQueue sets the Processor Queue as a config option to be passed into the NewProcessor method
85+
func WithQueue(q Queue) BPOptionConfig {
86+
return func(qp *BatchEventProcessor) {
8687
qp.Q = q
8788
}
8889
}
8990

90-
// PDispatcher sets the Processor Dispatcher as a config option to be passed into the NewProcessor method
91-
func PDispatcher(d Dispatcher) QPConfigOption {
92-
return func(qp *QueueingEventProcessor) {
91+
// WithEventDispatcher sets the Processor Dispatcher as a config option to be passed into the NewProcessor method
92+
func WithEventDispatcher(d Dispatcher) BPOptionConfig {
93+
return func(qp *BatchEventProcessor) {
9394
qp.EventDispatcher = d
9495
}
9596
}
9697

97-
// SDKKey sets the SDKKey used to register for notifications. This should be removed when the project
98+
// WithSDKKey sets the SDKKey used to register for notifications. This should be removed when the project
9899
// config supports sdk key.
99-
func SDKKey(sdkKey string) QPConfigOption {
100-
return func(qp *QueueingEventProcessor) {
100+
func WithSDKKey(sdkKey string) BPOptionConfig {
101+
return func(qp *BatchEventProcessor) {
101102
qp.sdkKey = sdkKey
102103
}
103104
}
104105

105-
// NewEventProcessor returns a new instance of QueueingEventProcessor with queueSize and flushInterval
106-
func NewEventProcessor(options ...QPConfigOption) *QueueingEventProcessor {
107-
p := &QueueingEventProcessor{}
106+
// NewBatchEventProcessor returns a new instance of BatchEventProcessor with queueSize and flushInterval
107+
func NewBatchEventProcessor(options ...BPOptionConfig) *BatchEventProcessor {
108+
p := &BatchEventProcessor{}
108109

109110
for _, opt := range options {
110111
opt(p)
@@ -130,7 +131,7 @@ func NewEventProcessor(options ...QPConfigOption) *QueueingEventProcessor {
130131
}
131132

132133
// Start initializes the event processor
133-
func (p *QueueingEventProcessor) Start(exeCtx utils.ExecutionCtx) {
134+
func (p *BatchEventProcessor) Start(exeCtx utils.ExecutionCtx) {
134135
if p.EventDispatcher == nil {
135136
p.EventDispatcher = NewQueueEventDispatcher(exeCtx.GetContext())
136137
}
@@ -140,7 +141,7 @@ func (p *QueueingEventProcessor) Start(exeCtx utils.ExecutionCtx) {
140141
}
141142

142143
// ProcessEvent processes the given impression event
143-
func (p *QueueingEventProcessor) ProcessEvent(event UserEvent) {
144+
func (p *BatchEventProcessor) ProcessEvent(event UserEvent) {
144145
p.Q.Add(event)
145146

146147
if p.Q.Size() >= p.MaxQueueSize {
@@ -151,22 +152,22 @@ func (p *QueueingEventProcessor) ProcessEvent(event UserEvent) {
151152
}
152153

153154
// EventsCount returns size of an event queue
154-
func (p *QueueingEventProcessor) EventsCount() int {
155+
func (p *BatchEventProcessor) EventsCount() int {
155156
return p.Q.Size()
156157
}
157158

158159
// GetEvents returns events from event queue for count
159-
func (p *QueueingEventProcessor) GetEvents(count int) []interface{} {
160+
func (p *BatchEventProcessor) GetEvents(count int) []interface{} {
160161
return p.Q.Get(count)
161162
}
162163

163164
// Remove removes events from queue for count
164-
func (p *QueueingEventProcessor) Remove(count int) []interface{} {
165+
func (p *BatchEventProcessor) Remove(count int) []interface{} {
165166
return p.Q.Remove(count)
166167
}
167168

168169
// StartTicker starts new ticker for flushing events
169-
func (p *QueueingEventProcessor) startTicker(exeCtx utils.ExecutionCtx) {
170+
func (p *BatchEventProcessor) startTicker(exeCtx utils.ExecutionCtx) {
170171
if p.Ticker != nil {
171172
return
172173
}
@@ -194,7 +195,7 @@ func (p *QueueingEventProcessor) startTicker(exeCtx utils.ExecutionCtx) {
194195
}
195196

196197
// check if user event can be batched in the current batch
197-
func (p *QueueingEventProcessor) canBatch(current *Batch, user UserEvent) bool {
198+
func (p *BatchEventProcessor) canBatch(current *Batch, user UserEvent) bool {
198199
if current.ProjectID == user.EventContext.ProjectID &&
199200
current.Revision == user.EventContext.Revision {
200201
return true
@@ -204,13 +205,13 @@ func (p *QueueingEventProcessor) canBatch(current *Batch, user UserEvent) bool {
204205
}
205206

206207
// add the visitor to the current batch
207-
func (p *QueueingEventProcessor) addToBatch(current *Batch, visitor Visitor) {
208+
func (p *BatchEventProcessor) addToBatch(current *Batch, visitor Visitor) {
208209
visitors := append(current.Visitors, visitor)
209210
current.Visitors = visitors
210211
}
211212

212213
// FlushEvents flushes events in queue
213-
func (p *QueueingEventProcessor) FlushEvents() {
214+
func (p *BatchEventProcessor) FlushEvents() {
214215
// we flush when queue size is reached.
215216
// however, if there is a ticker cycle already processing, we should wait
216217
p.Mux.Lock()
@@ -275,7 +276,7 @@ func (p *QueueingEventProcessor) FlushEvents() {
275276
}
276277

277278
// OnEventDispatch registers a handler for LogEvent notifications
278-
func (p *QueueingEventProcessor) OnEventDispatch(callback func(logEvent LogEvent)) (int, error) {
279+
func (p *BatchEventProcessor) OnEventDispatch(callback func(logEvent LogEvent)) (int, error) {
279280
notificationCenter := registry.GetNotificationCenter(p.sdkKey)
280281

281282
handler := func(payload interface{}) {
@@ -294,7 +295,7 @@ func (p *QueueingEventProcessor) OnEventDispatch(callback func(logEvent LogEvent
294295
}
295296

296297
// RemoveOnEventDispatch removes handler for LogEvent notification with given id
297-
func (p *QueueingEventProcessor) RemoveOnEventDispatch(id int) error {
298+
func (p *BatchEventProcessor) RemoveOnEventDispatch(id int) error {
298299
notificationCenter := registry.GetNotificationCenter(p.sdkKey)
299300

300301
if err := notificationCenter.RemoveHandler(id, notification.LogEvent); err != nil {

0 commit comments

Comments
 (0)